-
Notifications
You must be signed in to change notification settings - Fork 12
/
Finder.java
365 lines (338 loc) · 11.6 KB
/
Finder.java
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* MIT License
*
* Copyright (c) 2005-2020 by Anton Kolonin, Aigents®
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.webstructor.peer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import net.webstructor.al.AL;
import net.webstructor.al.Parser;
import net.webstructor.al.Set;
import net.webstructor.al.Writer;
import net.webstructor.core.Mistake;
import net.webstructor.core.Query;
import net.webstructor.core.Storager;
import net.webstructor.core.Thing;
import net.webstructor.data.Counter;
import net.webstructor.data.LangPack;
import net.webstructor.data.Translator;
import net.webstructor.util.Array;
class FinderContext {
Collection res = new HashSet();
HashMap<String,Integer> attributes = new HashMap<String,Integer>();
String keyAttribute = null;
java.util.Set options = null;
String question = null;
int retries = 0;
}
class Finder implements Intenter {
@Override
public String name() {
return "objective";
}
public boolean searchable(String name){
return AL.name.equals(name) || !(Array.contains(AL.foundation, name)
|| AL.sentiment.equals(name) || AL.image.equals(name));//TODO: exclude it in less hacky way?
}
@Override
public boolean handleIntent(final Session session){
try {
Thing peer = session.getStoredPeer();
if (peer == null)//TODO make working anonymously?
return false;
//Storager storager = session.getStorager();
LangPack languages = session.getBody().languages;
Translator trans = session.getBody().translator(session.language());
String yes = trans.loc("yes");
String no = trans.loc("no");
String input = session.input().trim().toLowerCase();
//TODO: keep this in session or context object/array
FinderContext fc = (FinderContext)session.context(name());
if (fc == null)
session.context(name(), fc = new FinderContext());
///////////////// start finding /////////////////
if (AL.empty(fc.res)) {
//TODO: if not found, fallback to fuzzy search based on letter bigrams or levenstain difference
if (!initialSearch(session, trans, fc, input, peer)) {
return false;
} else if (fc.res.size() == 1) {
askConfirmation(session,(Thing)fc.res.iterator().next());
} else {
if (!askQuestion(session,trans,fc)) {
//TODO: handle!
reset(session);
return false;
}
}
///////////////// keep finding /////////////////
} else if (fc.res.size() > 1) {
if (input.equals(no)) {
session.output(session.so());
reset(session);
return true;
} else
if (fc.options != null && fc.options.size() == 1 && input.equals(yes)) {//use confirmed prompted option
input = fc.options.iterator().next().toString();
}
//search for other attributes spec
HashSet<Thing> textMatches = new HashSet<Thing>();
HashSet<String> values = new HashSet<String>();
Counter words = new Counter();
LangPack.countWords(languages, words, input, null, 1);
words.normalizeBy(languages.words(), 1);
int maxMatches = 0;
for (Object r : fc.res) {
Thing t = (Thing)r;
String s = t.getString(fc.keyAttribute);//TODO: other types!?
if (AL.empty(s))
continue;
Set tokens = Parser.parse(s,AL.punctuation+AL.spaces,false,true,false,true);
Counter textWords = new Counter();
for (int i = 0; i < tokens.size(); i++) {
String token = (String)tokens.get(i);
Number n = words.value(token);
if (n != null)
textWords.count(token, n.doubleValue());
}
if (textWords.size() == 0 || textWords.size() < maxMatches)//have better matches already
continue;
if (maxMatches < textWords.size()) {//just found better matches
maxMatches = textWords.size();
textMatches.clear();
values.clear();
}
textMatches.add(t);//keep counting matches
values.add(s);
}
if (values.size() > 0)
fc.attributes.put(fc.keyAttribute, values.size());
if (textMatches.size() == 1) {//single match
fc.res.retainAll(textMatches);
askConfirmation(session,(Thing)fc.res.iterator().next());
} else
if (textMatches.size() > 1) {//multiple matches
fc.res.retainAll(textMatches);
if (!askQuestion(session,trans,fc)) {
session.output(session.so());
}
} else {//no matches
if (!askQuestion(session,trans,fc)) {
session.output(session.so());
}
}
} else {//res.size() == 1
//TODO: start over
if (input.startsWith(yes)) {
session.output("Ok.");
reset(session);
} else
if (input.startsWith(no)) {
session.output(session.so());
reset(session);
} else {
if (fc.retries++ <= 1)
askConfirmation(session,(Thing)fc.res.iterator().next());
else {
reset(session);
return false;
}
}
}
} catch (Throwable e) {
session.output(session.no()+" "+Responser.statement(e));
if (!(e instanceof Mistake))
session.sessioner.body.error("Finder error " + e.toString(), e);
reset(session);
}
return true;
}
void reset(Session session) {
session.context(name(),null);
}
boolean initialSearch(Session session, Translator trans, FinderContext fc, String input, Thing peer) throws Exception {
LangPack languages = session.getBody().languages;
Storager storager = session.getStorager();
//TODO: extra search for "is" class being searched and keep it in context?
//TODO: get searchable attributes only, have them app-configured before
//TODO: explicitly say "name is text" to identify Full-Text-Searchable attributes only?
String[] names = storager.getNames();
//String[] has =storager.getNames(AL.has);
//find matches in attributed strings
Counter words = new Counter();
LangPack.countWords(languages, words, input, null);
words.normalizeBy(languages.words(), 1);
HashMap<String,Counter> textMatches = new HashMap<String,Counter>();
int maxMatches = 0;
for (String name : names) {
if (!searchable(name))//skip special attributes
continue;
Object values[] = storager.getObjects(name);
if (!AL.empty(values)) for (Object v : values) {
//TODO dates, numbers, Things with names
if (!(v instanceof String))
continue;
String s = (String)v;
if (AL.empty(s))
continue;
Collection coll = storager.getByName(name, s);
if (AL.empty(coll))
continue;
HashSet things = new HashSet();
for (Object c : coll)
if (c instanceof Thing)
if (((Thing)c).get(AL.has) == null && ((Thing)c).get(AL.is) != null)//if pure instance
if (Query.accessible((Thing)c, (Thing)session.sessioner.body.getSelf(), peer, null, false))
things.addAll(coll);
if (things.size() == 0)
continue;
//Set tokens = Parser.parse(s,AL.punctuation+AL.spaces,false,true,false,true);//no quoting
Set tokens = Parser.parse(s,AL.punctuation+AL.spaces,false,true,true,true);//quoting to avoid searching patterns
Counter textWords = new Counter();
for (int i = 0; i < tokens.size(); i++) {
String token = (String)tokens.get(i);
Number n = words.value(token);
if (n != null)
textWords.count(token, n.doubleValue());
}
if (textWords.size() == 0 || textWords.size() < maxMatches)//have better matches already
continue;
if (maxMatches < textWords.size()) {//just found better matches
maxMatches = textWords.size();
textMatches.clear();
fc.attributes.clear();
fc.res.clear();
}
fc.res.addAll(things);
textMatches.put(s, textWords);//keep counting matches
//TODO: use things, not value
//fc.attributes.put(name, things.size());
fc.attributes.put(name, values.length);
}
}
return !AL.empty(fc.res);
}
boolean askQuestion(Session session, Translator trans, FinderContext fc) {
if (askQuestionInternal(session, trans, fc))
return true;
for (;;) {
String specific = null;
int min = Integer.MAX_VALUE;
for (String attr : fc.attributes.keySet()) {
int cardinality = fc.attributes.get(attr);
if (cardinality > 1 && min > cardinality) {
specific = attr;
min = cardinality;
}
}
if (specific == null)
break;
fc.attributes.remove(specific);
if (askQuestionInternal(session, trans, fc))
return true;
}
//reset(session);
//return false;
//fallback to use the single element
while (fc.res.size() > 1)
fc.res.remove(fc.res.iterator().next());
askConfirmation(session,(Thing)fc.res.iterator().next());
return true;
}
boolean askQuestionInternal(Session session, Translator trans, FinderContext fc) {
//find the most cardinal attribute
HashMap<String,java.util.Set> nameValues = new HashMap<String,java.util.Set>();
Counter thingsByAttr = new Counter();
for (Object r : fc.res) {
if (r instanceof Thing) {
Thing t = (Thing)r;
String[] t_names = t.getNamesAvailable();
for (String name : t_names) {
if (fc.attributes.containsKey(name))//skip resolved attribute
continue;
if (!searchable(name))//skip special attributes
continue;
String v = t.getString(name);
if (!AL.empty(v)) {
java.util.Set s = nameValues.get(name);
if (s == null) {
nameValues.put(name, s = new java.util.TreeSet());//need them sorted
}
s.add(v);//count unique value;
thingsByAttr.count(name);
}
}
}
}
thingsByAttr.normalize();//normalize to see which one covers more objects of the question
fc.keyAttribute = null;
int min = 0;
fc.options = null;
for (String n : nameValues.keySet()) {
if (thingsByAttr.value(n).intValue() != 100)
continue;
java.util.Set s = nameValues.get(n);
if (s.size() == 1)
fc.attributes.put(fc.keyAttribute,1);
else
if (min == 0 || s.size() < min) {
fc.keyAttribute = n;
min = s.size();
fc.options = s;
}
}
if (!AL.empty(fc.options)) {
String or = trans.loc("or");
StringBuilder sb = new StringBuilder();
sb.append(fc.keyAttribute).append(" ");
int c = 0;
for (Object o : fc.options) {
if (c > 0)
sb.append(" ").append(or).append(" ");
c++;
sb.append(o);
}
sb.append("?");
session.output(Writer.capitalize(sb.toString()));
return true;
}
return false;
}
void askConfirmation(Session session, Thing thing) {
StringBuilder sb = new StringBuilder();
String[] t_names = thing.getNamesAvailable();
for (String name : t_names) {
if (!searchable(name))//skip special attributes
continue;
String v = thing.getString(name);
if (!AL.empty(v)) {
if (sb.length() > 0)
sb.append(", ");
sb.append(name).append(' ').append(v);
}
}
//TODO sb.length() == 0
sb.append('?');
session.output(Writer.capitalize(sb.toString()));
//expect yes/да or no/нет
}
}