/
StandardReconConfig.java
636 lines (547 loc) · 21.8 KB
/
StandardReconConfig.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.model.recon;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.refine.expr.ExpressionUtils;
import com.google.refine.model.Cell;
import com.google.refine.model.Project;
import com.google.refine.model.Recon;
import com.google.refine.model.Recon.Judgment;
import com.google.refine.model.ReconCandidate;
import com.google.refine.model.ReconType;
import com.google.refine.model.RecordModel.RowDependency;
import com.google.refine.model.Row;
import com.google.refine.util.HttpClient;
import com.google.refine.util.ParsingUtilities;
public class StandardReconConfig extends ReconConfig {
final static Logger logger = LoggerFactory.getLogger("refine-standard-recon");
private static final String DEFAULT_SCHEMA_SPACE = "http://localhost/schema";
private static final String DEFAULT_IDENTIFIER_SPACE = "http://localhost/identifier";
static public class ColumnDetail {
@JsonProperty("column")
final public String columnName;
@JsonProperty("propertyName")
final public String propertyName;
@JsonProperty("propertyID")
final public String propertyID;
/**
* Unfortunately the format of ColumnDetail
* is inconsistent in the UI and the backend
* so we need to support two deserialization formats.
* See the tests for that.
*/
@JsonCreator
public ColumnDetail(
@JsonProperty("column")
String columnName,
@JsonProperty("propertyName")
String propertyName,
@JsonProperty("propertyID")
String propertyID,
@JsonProperty("property")
ReconType property) {
this.columnName = columnName;
this.propertyName = property == null ? propertyName : property.name;
this.propertyID = property == null ? propertyID : property.id;
}
@Override
public String toString() {
try {
return ParsingUtilities.mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
static public StandardReconConfig reconstruct(String json) throws IOException {
return ParsingUtilities.mapper.readValue(json, StandardReconConfig.class);
}
static protected class StandardReconJob extends ReconJob {
String text;
String code;
@Override
public int getKey() {
return code.hashCode();
}
@Override
public String toString() {
return code;
}
}
@JsonProperty("service")
final public String service;
@JsonProperty("identifierSpace")
final public String identifierSpace;
@JsonProperty("schemaSpace")
final public String schemaSpace;
@JsonIgnore
final public String typeID;
@JsonIgnore
final public String typeName;
@JsonProperty("autoMatch")
final public boolean autoMatch;
@JsonProperty("columnDetails")
final public List<ColumnDetail> columnDetails;
@JsonProperty("limit")
final private int limit;
// initialized lazily
private HttpClient httpClient = null;
@JsonCreator
public StandardReconConfig(
@JsonProperty("service")
String service,
@JsonProperty("identifierSpace")
String identifierSpace,
@JsonProperty("schemaSpace")
String schemaSpace,
@JsonProperty("type")
ReconType type,
@JsonProperty("autoMatch")
boolean autoMatch,
@JsonProperty("columnDetails")
List<ColumnDetail> columnDetails,
@JsonProperty("limit")
int limit) {
this(service, identifierSpace, schemaSpace,
type != null ? type.id : null,
type != null ? type.name : null,
autoMatch, columnDetails, limit);
}
public StandardReconConfig(
String service,
String identifierSpace,
String schemaSpace,
String typeID,
String typeName,
boolean autoMatch,
List<ColumnDetail> columnDetails
) {
this(service, identifierSpace, schemaSpace, typeID, typeName, autoMatch, columnDetails, 0);
}
/**
* @param service
* @param identifierSpace
* @param schemaSpace
* @param typeID
* @param typeName
* @param autoMatch
* @param columnDetails
* @param limit maximum number of results to return (0 = default)
*/
public StandardReconConfig(
String service,
String identifierSpace,
String schemaSpace,
String typeID,
String typeName,
boolean autoMatch,
List<ColumnDetail> columnDetails,
int limit
) {
this.service = service;
this.identifierSpace = identifierSpace != null ? identifierSpace : DEFAULT_IDENTIFIER_SPACE;
this.schemaSpace = schemaSpace != null ? schemaSpace : DEFAULT_SCHEMA_SPACE;
this.typeID = typeID;
this.typeName = typeName;
this.autoMatch = autoMatch;
this.columnDetails = columnDetails;
this.limit = limit;
}
@JsonProperty("type")
@JsonInclude(Include.NON_NULL)
public ReconType getReconType() {
if (typeID != null) {
return new ReconType(typeID, typeName);
}
return null;
}
@Override
@JsonIgnore
public int getBatchSize() {
return 10;
}
@Override
public String getBriefDescription(Project project, String columnName) {
return "Reconcile cells in column " + columnName + " to type " + typeID;
}
public ReconJob createSimpleJob(String query) {
/* Same as createJob, but for simpler queries
* without any properties. This is much easier
* to generate as there is no need for a Project,
* Row and Cell: this means the job can be created
* outside the usual context of reconciliation (e.g.
* in an importer).
*/
StandardReconJob job = new StandardReconJob();
try {
String queryJson = ParsingUtilities.defaultWriter.writeValueAsString(
Collections.singletonMap("query", query));
job.text = query;
job.code = queryJson;
return job;
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
protected static class QueryProperty {
@JsonProperty("pid")
String pid;
@JsonProperty("v")
Object v;
protected QueryProperty(
String pid,
Object v) {
this.pid = pid;
this.v = v;
}
@Override
public String toString() {
try {
return ParsingUtilities.mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
protected static class ReconQuery {
@JsonProperty("query")
protected String query;
@JsonProperty("type")
@JsonInclude(Include.NON_NULL)
protected String typeID;
@JsonProperty("type_strict")
@JsonInclude(Include.NON_NULL)
public String isTypeStrict() {
if(typeID != null) {
return "should";
}
return null;
}
@JsonProperty("properties")
@JsonInclude(Include.NON_EMPTY)
protected List<QueryProperty> properties;
// Only send limit if it's non-default (default = 0) to preserve backward
// compatibility with services which might choke on this (pre-2013)
@JsonProperty("limit")
@JsonInclude(Include.NON_DEFAULT)
protected int limit;
public ReconQuery() {
super();
this.query = "";
this.typeID = null;
this.properties = null;
this.limit = 0;
}
@JsonCreator
public ReconQuery(
String query,
String typeID,
List<QueryProperty> properties,
int limit) {
this.query = query;
this.typeID = typeID;
this.properties = properties;
this.limit = limit;
}
@Override
public String toString() {
try {
return ParsingUtilities.mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
public static class ReconResult {
@JsonProperty("name")
public String name;
@JsonProperty("id")
public String id;
@JsonProperty("type")
public List<ReconType> types = Collections.emptyList();
@JsonProperty("score")
public double score;
@JsonProperty("match")
public boolean match = false;
@JsonIgnore
public ReconCandidate toCandidate() {
String[] bareTypes = new String[types.size()];
for (int i = 0; i != types.size(); i++) {
bareTypes[i] = types.get(i).id;
}
ReconCandidate result = new ReconCandidate(id, name, bareTypes, score);
return result;
}
@Override
public String toString() {
try {
return ParsingUtilities.mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
@Override
public ReconJob createJob(Project project, int rowIndex, Row row,
String columnName, Cell cell) {
StandardReconJob job = new StandardReconJob();
List<QueryProperty> properties = new ArrayList<>();
for (ColumnDetail c : columnDetails) {
int detailCellIndex = project.columnModel.getColumnByName(c.columnName).getCellIndex();
Cell cell2 = row.getCell(detailCellIndex);
if (cell2 == null || !ExpressionUtils.isNonBlankData(cell2.value)) {
int cellIndex = project.columnModel.getColumnByName(columnName).getCellIndex();
RowDependency rd = project.recordModel.getRowDependency(rowIndex);
if (rd != null && rd.cellDependencies != null) {
int contextRowIndex = rd.cellDependencies[cellIndex].rowIndex;
if (contextRowIndex >= 0 && contextRowIndex < project.rows.size()) {
Row row2 = project.rows.get(contextRowIndex);
cell2 = row2.getCell(detailCellIndex);
}
}
}
if (cell2 != null && ExpressionUtils.isNonBlankData(cell2.value)) {
Object v = null;
if (cell2.recon != null && cell2.recon.match != null) {
Map<String, String> recon = new HashMap<>();
recon.put("id", cell2.recon.match.id);
recon.put("name", cell2.recon.match.name);
v = recon;
} else {
v = cell2.value;
}
properties.add(new QueryProperty(c.propertyID, v));
}
}
ReconQuery query = new ReconQuery(cell.value.toString(), typeID, properties, limit);
job.text = cell.value.toString();
try {
job.code = ParsingUtilities.defaultWriter.writeValueAsString(query);
} catch (JsonProcessingException e) {
// FIXME: This error will get lost
e.printStackTrace();
return null; // TODO: Throw exception instead?
}
return job;
}
private HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new HttpClient();
}
return httpClient;
}
private String postQueries(String url, String queriesString) throws IOException {
try {
return getHttpClient().postNameValue(url, "queries", queriesString);
} catch (IOException e) {
throw new IOException("Failed to batch recon with load:\n" + queriesString, e);
}
}
@Override
public List<Recon> batchRecon(List<ReconJob> jobs, long historyEntryID) {
List<Recon> recons = new ArrayList<Recon>(jobs.size());
StringWriter stringWriter = new StringWriter();
stringWriter.write("{");
for (int i = 0; i < jobs.size(); i++) {
StandardReconJob job = (StandardReconJob) jobs.get(i);
if (i > 0) {
stringWriter.write(",");
}
stringWriter.write("\"q" + i + "\":");
stringWriter.write(job.code);
}
stringWriter.write("}");
String queriesString = stringWriter.toString();
try {
String responseString = postQueries(service, queriesString);
ObjectNode o = ParsingUtilities.evaluateJsonStringToObjectNode(responseString);
if (o == null) { // utility method returns null instead of throwing
logger.error("Failed to parse string as JSON: " + responseString);
} else {
for (int i = 0; i < jobs.size(); i++) {
StandardReconJob job = (StandardReconJob) jobs.get(i);
Recon recon = null;
String text = job.text;
String key = "q" + i;
if (o.has(key) && o.get(key) instanceof ObjectNode) {
ObjectNode o2 = (ObjectNode) o.get(key);
if (o2.has("result") && o2.get("result") instanceof ArrayNode) {
ArrayNode results = (ArrayNode) o2.get("result");
recon = createReconServiceResults(text, results, historyEntryID);
} else {
// TODO: better error reporting
logger.warn("Service error for text: " + text + "\n Job code: " + job.code + "\n Response: " + o2.toString());
}
} else {
// TODO: better error reporting
logger.warn("Service error for text: " + text + "\n Job code: " + job.code);
}
if (recon != null) {
recon.service = service;
}
recons.add(recon);
}
}
} catch (IOException e) {
logger.error("Failed to batch recon with load:\n" + queriesString, e);
}
// TODO: This code prevents the retry mechanism in ReconOperation from working
while (recons.size() < jobs.size()) {
Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace);
recon.service = service;
recon.identifierSpace = identifierSpace;
recon.schemaSpace = schemaSpace;
recons.add(recon);
}
return recons;
}
@Override
public Recon createNewRecon(long historyEntryID) {
Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace);
recon.service = service;
return recon;
}
protected Recon createReconServiceResults(String text, ArrayNode resultsList, long historyEntryID) {
Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace);
List<ReconResult> results = ParsingUtilities.mapper.convertValue(resultsList, new TypeReference<List<ReconResult>>() {});
// Sort results by decreasing score
results.sort(new Comparator<ReconResult>() {
@Override
public int compare(ReconResult a, ReconResult b) {
return Double.compare(b.score, a.score);
}
});
int length = results.size();
for (int i = 0; i < length; i++) {
ReconResult result = results.get(i);
ReconCandidate candidate = result.toCandidate();
if (autoMatch && i == 0 && result.match) {
recon.match = candidate;
recon.matchRank = 0;
recon.judgment = Judgment.Matched;
recon.judgmentAction = "auto";
}
recon.addCandidate(candidate);
}
computeFeatures(recon, text);
return recon;
}
/**
* Recomputes the features associated with this reconciliation
* object (only if we have at least one candidate).
*
* @param text the cell value to compare the reconciliation data to
*/
public void computeFeatures(Recon recon, String text) {
if (recon.candidates != null && !recon.candidates.isEmpty() && text != null) {
ReconCandidate candidate = recon.candidates.get(0);
if (candidate.name != null) {
recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.name));
recon.setFeature(Recon.Feature_nameLevenshtein,
StringUtils.getLevenshteinDistance(StringUtils.lowerCase(text), StringUtils.lowerCase(candidate.name)));
recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.name));
}
recon.setFeature(Recon.Feature_typeMatch, false);
if (this.typeID != null) {
for (String typeID : candidate.types) {
if (this.typeID.equals(typeID)) {
recon.setFeature(Recon.Feature_typeMatch, true);
break;
}
}
}
} else {
recon.features = new Object[Recon.Feature_max];
}
}
static protected double wordDistance(String s1, String s2) {
Set<String> words1 = breakWords(s1);
Set<String> words2 = breakWords(s2);
return words1.size() >= words2.size() ? wordDistance(words1, words2) : wordDistance(words2, words1);
}
static protected double wordDistance(Set<String> longWords, Set<String> shortWords) {
if (longWords.size() == 0) {
return 0.0;
}
double common = 0;
for (String word : shortWords) {
if (longWords.contains(word)) {
common++;
}
}
return common / longWords.size();
}
static final protected Set<String> s_stopWords = new HashSet<String>();
static {
// FIXME: This is English specific
s_stopWords.add("the");
s_stopWords.add("a");
s_stopWords.add("and");
s_stopWords.add("of");
s_stopWords.add("on");
s_stopWords.add("in");
s_stopWords.add("at");
s_stopWords.add("by");
}
static protected Set<String> breakWords(String s) {
String[] words = s.toLowerCase().split("\\s+");
Set<String> set = new HashSet<String>(words.length);
for (String word : words) {
if (!s_stopWords.contains(word)) {
set.add(word);
}
}
return set;
}
@Override
public String getMode() {
return "standard-service";
}
}