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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/runestone/web_search.rb', line 122
def synonymize_part(part)
pending_matches = []
matches = []
part.each_with_index do |token, i|
pending_matches.select! do |match|
if match.end_index + 1 == i && match.substitution[token.value]
match.substitution[token.value].map do |nm|
if nm.is_a?(Hash)
match.end_index = i
match.alts = nm
true
else
matches << Match.new(match.start_index..i, Phrase.new(Array(nm), distance: 1))
false
end
end
else
false
end
end
if match = Runestone.synonyms[token.value]
match.each do |m|
if m.is_a?(Hash)
pending_matches << PartialMatch.new(i, i, m)
else
matches << Match.new(i, Phrase.new(m.split(/\s+/), distance: 1))
end
end
end
end
matches.select! do |match|
if match.index.is_a?(Integer)
case part[match.index]
when Or
part[match.index].values << match.substitution
else
part[match.index] = Or.new([part[match.index], match.substitution])
end
false
else
true
end
end
groups = matches.inject([]) do |memo, match|
if memo.empty?
memo << [match]
elsif i = memo.index { |k| k.none? { |j| j.index.overlaps?(match.index) } }
memo[i] << match
else
memo << [match]
end
memo
end
if groups.empty?
And.new(part)
else
orrs = Or.new([])
groups.each do |g|
p = []
p << And.new(part[0..g.first.index.begin-1]) if g.first.index.begin > 0
g.each do |m|
p << Or.new([And.new(part[m.index]), m.substitution])
end
p << And.new(part[g.last.index.end+1..-1]) if g.last.index.end < part.size
orrs.values << And.new(p)
end
orrs
end
end
|