-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_constraints.pl
155 lines (137 loc) · 6.36 KB
/
string_constraints.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
:- module(string_constraints, [
equality_constraint/5,
substring_constraint/5,
levenshtein_constraint/6,
subsequence_constraint/5,
regex_constraint/5,
similarity_constraint/5,
sub_similarity_constraint/5,
fuzzy_substr_constraint/5,
add_string_constraint/5
]).
:- use_module(compat, []).
:- use_module(constraints, [no_constraint/3, and_constraint/5]).
:- use_module(sequence_ops).
:- use_module(nlp).
:- meta_predicate wrap_core(2, 2, ?, -, -).
:- meta_predicate equality_core(2, +, ?, -, -).
:- meta_predicate substring_constraint(2, +, ?, -, -).
:- meta_predicate levenshtein_constraint(2, +, +, +, -, -).
:- meta_predicate subsequence_constraint(2, +, +, -, -).
:- meta_predicate regex_constraint(2, +, ?, -, -).
:- meta_predicate similarity_constraint(2, +, ?, -, -).
:- meta_predicate sub_similarity_constraint(2, +, ?, -, -).
:- meta_predicate fuzzy_substr_constraint(2, +, ?, -, -).
:- meta_predicate add_string_constraint(2, +, ?, +, -).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Wrappers for string comparison methods
wrapper(Constraint, Getter, Item, Cost, NewConstraint) :-
wrap_core(Constraint, Getter, Item, Cost, NewConstraint).
wrap_core(Constraint, Getter, Item, Cost, NewConstraint) :-
call(Getter, Item, Value),
(
%% We do it like this to avoid having two cases of this function,
%% - due to backtracking, we might also include the opposite case,
%% and we explicitly don't want to evaluate these functions twice (eg. using \+),
%% as this is expensive (eg. NLP parsing, Levenshtein for large strings).
call(Constraint, Value, Cost0) ->
(
Cost = Cost0,
NewConstraint = no_constraint
) ;
(
Cost = 1.0,
NewConstraint = string_constraints:wrapper(Constraint, Getter)
)
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% String comparison methods
% x_core: Args*, Value, Cost
% x_constraint: Args*, Func, Cost, NewConstraint
equality_core(Target, Target, 0.0).
equality_constraint(Getter, Target, Func, Cost, NewConstraint) :-
wrap_core(equality_core(Target), Getter, Func, Cost, NewConstraint).
sub_core(Needle, String, 0.0) :-
sub_string(String, _, _, _, Needle).
substring_constraint(Getter, Needle, Func, Cost, NewConstraint) :-
wrap_core(sub_core(Needle), Getter, Func, Cost, NewConstraint).
lev_core(Target, MaxDistance, String, Distance) :-
levenshtein_distance(Target, String, LevDistance),
LevDistance =< MaxDistance,
Distance is LevDistance / MaxDistance.
levenshtein_constraint(Getter, Target, MaxDistance, Func, Cost, NewConstraint) :-
wrap_core(lev_core(Target, MaxDistance), Getter, Func, Cost, NewConstraint).
seq_core(Sequence, String, 0.0) :-
sequence_match(Sequence, String).
subsequence_constraint(Getter, Sequence, Func, Cost, NewConstraint) :-
wrap_core(seq_core(Sequence), Getter, Func, Cost, NewConstraint).
:- if(can_use_regex).
regex_core(Regex, String, 0.0) :-
re_match(Regex, String).
:- else.
regex_core(_, _, _) :-
throw(
error(
unsupported_error,
context(
regex_core/3, "Regex not supported for current version"
)
)
).
:- endif.
regex_constraint(Getter, Regex, Func, Cost, NewConstraint) :-
wrap_core(regex_core(Regex), Getter, Func, Cost, NewConstraint).
similarity_core(Needle, Source, Cost) :-
similarity(Source, Needle, Similarity),
Similarity > 0.8,
Cost is 1.0 - Similarity.
similarity_constraint(Getter, Sequence, Func, Cost, NewConstraint) :-
wrap_core(similarity_core(Sequence), Getter, Func, Cost, NewConstraint).
sub_similarity_core(Needle, Source, Cost) :-
sub_similarity(Source, Needle, Similarity),
Similarity > 0.8,
Cost is 1.0 - Similarity.
sub_similarity_constraint(Getter, Sequence, Func, Cost, NewConstraint) :-
wrap_core(sub_similarity_core(Sequence), Getter, Func, Cost, NewConstraint).
fuzzy_substr_core(Source, Needle, Cost) :-
fuzzy_substr(Source, Needle, Similarity),
Similarity > 0.8,
Cost is 1.0 - Similarity.
fuzzy_substr_constraint(Getter, Sequence, Func, Cost, NewConstraint) :-
wrap_core(fuzzy_substr_core(Sequence), Getter, Func, Cost, NewConstraint).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helper functions for building searches from user input.
match_eq(Eq) :- member(Eq, ["eq", "exact", eq, exact]).
match_lev(Lev) :- member(Lev, ["lev", lev]).
match_substr(Substr) :- member(Substr, ["substr", substr]).
match_subseq(Subseq) :- member(Subseq, ["subseq", subseq]).
match_re(Re) :- member(Re, ["re", re]).
match_sim(Sim) :- member(Sim, ["sim", sim]).
match_sub_sim(Sim) :- member(Sim, ["subsim", subsim]).
match_fuzzy_substr(Substr) :- member(Substr, ["fsubstr", fsubstr]).
%! Get the constraint for the field and method
string_constraint(Getter, String, Eq, string_constraints:equality_constraint(Getter, String)) :-
match_eq(Eq), !.
string_constraint(Getter, String, Lev, string_constraints:levenshtein_constraint(Getter, String, MaxDis)) :-
match_lev(Lev), !,
string_length(String, StrLen),
MaxDis is round(sqrt(StrLen)).
string_constraint(Getter, String, Substr, string_constraints:substring_constraint(Getter, String)) :-
match_substr(Substr), !.
string_constraint(Getter, String, Subseq, string_constraints:subsequence_constraint(Getter, String)) :-
match_subseq(Subseq), !.
:- if(can_use_regex).
string_constraint(Getter, String, Re, string_constraints:regex_constraint(Getter, String)) :-
match_re(Re), !.
:- endif.
string_constraint(Getter, String, Subseq, string_constraints:similarity_constraint(Getter, String)) :-
match_sim(Subseq), !.
string_constraint(Getter, String, Subseq, string_constraints:sub_similarity_constraint(Getter, String)) :-
match_sub_sim(Subseq), !.
string_constraint(Getter, String, Subseq, string_constraints:fuzzy_substr_constraint(Getter, String)) :-
match_fuzzy_substr(Subseq), !.
%% add_string_constraint(+Field, +String, +Method, +OldConstraints, -NewConstraints).
%% none is used to denote constraints which are not provided
add_string_constraint(_, none, _, Constraint, Constraint) :- !.
add_string_constraint(Getter, String, Method, Rhs, and_constraint(Rhs, Constraint)) :-
string_constraint(Getter, String, Method, Constraint), !.