/
Consolidation.java
executable file
·722 lines (641 loc) · 32.1 KB
/
Consolidation.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package org.grobid.core.utilities;
import com.rockymadden.stringmetric.similarity.RatcliffObershelpMetric;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.grobid.core.data.BibDataSet;
import org.grobid.core.data.BiblioItem;
import org.grobid.core.data.Funder;
import org.grobid.core.utilities.counters.CntManager;
import org.grobid.core.utilities.crossref.CrossrefClient;
import org.grobid.core.utilities.crossref.CrossrefRequestListener;
import org.grobid.core.utilities.crossref.WorkDeserializer;
import org.grobid.core.utilities.crossref.FunderDeserializer;
import org.grobid.core.utilities.glutton.GluttonClient;
import org.grobid.core.utilities.TextUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Option;
import java.util.*;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.grobid.core.data.BiblioItem.cleanDOI;
/**
* Singleton class for managing the extraction of bibliographical information from pdf documents.
* When consolidation operations are realized, be sure to call the close() method
* to ensure that all Executors are terminated.
*
*/
public class Consolidation {
private static final Logger LOGGER = LoggerFactory.getLogger(Consolidation.class);
private static volatile Consolidation instance;
private CrossrefClient client = null;
private WorkDeserializer workDeserializer = null;
private FunderDeserializer funderDeserializer = null;
private CntManager cntManager = null;
public enum GrobidConsolidationService {
CROSSREF("crossref"),
GLUTTON("glutton");
private final String ext;
GrobidConsolidationService(String ext) {
this.ext = ext;
}
public String getExt() {
return ext;
}
public static GrobidConsolidationService get(String name) {
if (name == null) {
throw new IllegalArgumentException("Name of consolidation service must not be null");
}
String n = name.toLowerCase();
for (GrobidConsolidationService e : values()) {
if (e.name().toLowerCase().equals(n)) {
return e;
}
}
throw new IllegalArgumentException("No consolidation service with name '" + name +
"', possible values are: " + Arrays.toString(values()));
}
}
public static Consolidation getInstance() {
if (instance == null) {
getNewInstance();
}
return instance;
}
/**
* Creates a new instance.
*/
private static synchronized void getNewInstance() {
LOGGER.debug("Get new instance of Consolidation");
instance = new Consolidation();
}
/**
* Hidden constructor
*/
private Consolidation() {
if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.GLUTTON)
client = GluttonClient.getInstance();
else
client = CrossrefClient.getInstance();
workDeserializer = new WorkDeserializer();
funderDeserializer = new FunderDeserializer();
}
public void setCntManager(CntManager cntManager) {
this.cntManager = cntManager;
}
public CntManager getCntManager() {
return this.cntManager;
}
/**
* After consolidation operations, this need to be called to ensure that all
* involved Executors are shut down immediatly, otherwise non terminated thread
* could prevent the JVM from exiting
*/
public void close() {
//client.close();
}
/**
* Try to consolidate one bibliographical object with crossref metadata lookup web services based on
* core metadata. In practice, this method is used for consolidating header metadata.
*/
public BiblioItem consolidate(BiblioItem bib, String rawCitation, int consolidateMode) throws Exception {
final List<BiblioItem> results = new ArrayList<>();
String theDOI = bib.getDOI();
if (StringUtils.isNotBlank(theDOI)) {
theDOI = cleanDOI(theDOI);
}
final String doi = theDOI;
String halId = bib.getHalId();
String aut = bib.getFirstAuthorSurname();
String title = bib.getTitle();
String journalTitle = bib.getJournal();
String volume = bib.getVolume();
if (StringUtils.isBlank(volume))
volume = bib.getVolumeBlock();
String firstPage = null;
String pageRange = bib.getPageRange();
int beginPage = bib.getBeginPage();
if (beginPage != -1) {
firstPage = "" + beginPage;
} else if (pageRange != null) {
StringTokenizer st = new StringTokenizer(pageRange, "--");
if (st.countTokens() == 2) {
firstPage = st.nextToken();
} else if (st.countTokens() == 1)
firstPage = pageRange;
}
String year = null;
if ( bib.getNormalizedPublicationDate() != null ) {
year = "" + bib.getNormalizedPublicationDate().getYear();
}
if (year == null)
year = bib.getYear();
if (cntManager != null)
cntManager.i(ConsolidationCounters.CONSOLIDATION);
long threadId = Thread.currentThread().getId();
Map<String, String> arguments = null;
if (StringUtils.isNotBlank(doi)) {
// call based on the identified DOI
arguments = new HashMap<String,String>();
arguments.put("doi", doi);
} else if (consolidateMode != 3) {
if (StringUtils.isNotBlank(rawCitation)) {
// call with full raw string
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("query.bibliographic", rawCitation);
}
}
if (StringUtils.isNotBlank(halId)) {
// call based on the identified HAL ID
if (arguments == null)
arguments = new HashMap<String,String>();
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF)
arguments.put("halid", halId);
}
if (StringUtils.isNotBlank(aut)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("query.author", aut);
}
}
if (StringUtils.isNotBlank(title)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("query.title", title);
}
}
if (StringUtils.isNotBlank(journalTitle)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("query.container-title", journalTitle);
}
}
if (StringUtils.isNotBlank(volume)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("volume", volume);
}
}
if (StringUtils.isNotBlank(firstPage)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("firstPage", firstPage);
}
}
if (StringUtils.isNotBlank(year)) {
// publication year metadata, CrossRef has no year query field, they are supported by the query.bibliographic
// field and filter
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("year", year);
}
}
}
if (arguments == null || arguments.size() == 0) {
return null;
}
if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF) {
if (StringUtils.isBlank(doi) && StringUtils.isBlank(rawCitation) &&
(StringUtils.isBlank(aut) || StringUtils.isBlank(title)) ) {
// there's not enough information for a crossref request, which might always return a result
return null;
}
}
if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF) {
arguments.put("rows", "1"); // we just request the top-one result
} else if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.GLUTTON) {
// GROBID has already parsed the reference, so no need to redo this in glutton
arguments.put("parseReference", "false");
}
final boolean doiQuery;
try {
//CrossrefRequestListener<BiblioItem> requestListener = new CrossrefRequestListener<BiblioItem>();
if (cntManager != null) {
cntManager.i(ConsolidationCounters.CONSOLIDATION);
}
if ( StringUtils.isNotBlank(doi) && (cntManager != null) ) {
cntManager.i(ConsolidationCounters.CONSOLIDATION_PER_DOI);
doiQuery = true;
} else {
doiQuery = false;
}
client.pushRequest("works", arguments, workDeserializer, threadId, new CrossrefRequestListener<BiblioItem>(0) {
@Override
public void onSuccess(List<BiblioItem> res) {
if ((res != null) && (res.size() > 0) ) {
// we need here to post-check that the found item corresponds
// correctly to the one requested in order to avoid false positive
for(BiblioItem oneRes : res) {
/*
Glutton integrates its own post-validation, so we can skip post-validation in GROBID when it is used as
consolidation service.
In case of crossref REST API, for single bib. ref. consolidation (this case comes only for header extraction),
having an extracted DOI matching is considered safe enough, and we don't require further post-validation.
For all the other case of matching with CrossRef, we require a post-validation.
*/
if ((GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.GLUTTON)
||
( (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF) &&
doiQuery )
||
( (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF) &&
postValidation(bib, oneRes))
) {
results.add(oneRes);
if (cntManager != null) {
cntManager.i(ConsolidationCounters.CONSOLIDATION_SUCCESS);
if (doiQuery)
cntManager.i(ConsolidationCounters.CONSOLIDATION_PER_DOI_SUCCESS);
}
break;
}
}
}
}
@Override
public void onError(int status, String message, Exception exception) {
LOGGER.info("Consolidation service returns error ("+status+") : "+message, exception);
}
});
} catch(Exception e) {
LOGGER.info("Consolidation error - ", e);
}
client.finish(threadId);
if (results.size() == 0)
return null;
else
return results.get(0);
}
/**
* Try tp consolidate a list of bibliographical objects in one operation with consolidation services.
* In practice this method is used for consolidating the metadata of all the extracted bibliographical
* references.
*/
public Map<Integer,BiblioItem> consolidate(List<BibDataSet> biblios) {
if (CollectionUtils.isEmpty(biblios))
return null;
final Map<Integer,BiblioItem> results = new HashMap<Integer,BiblioItem>();
// init the results
int n = 0;
for(n=0; n<biblios.size(); n++) {
results.put(Integer.valueOf(n), null);
}
n = 0;
long threadId = Thread.currentThread().getId();
for(BibDataSet bibDataSet : biblios) {
final BiblioItem theBiblio = bibDataSet.getResBib();
if (cntManager != null)
cntManager.i(ConsolidationCounters.TOTAL_BIB_REF);
// first we get the exploitable metadata
String doi = theBiblio.getDOI();
if (StringUtils.isNotBlank(doi)) {
doi = BiblioItem.cleanDOI(doi);
}
// first we get the exploitable metadata
String halId = theBiblio.getHalId();
String aut = theBiblio.getFirstAuthorSurname();
String title = theBiblio.getTitle();
String journalTitle = theBiblio.getJournal();
// and the row string
String rawCitation = bibDataSet.getRawBib();
Map<String, String> arguments = null;
String volume = theBiblio.getVolume();
if (StringUtils.isBlank(volume))
volume = theBiblio.getVolumeBlock();
String firstPage = null;
String pageRange = theBiblio.getPageRange();
int beginPage = theBiblio.getBeginPage();
if (beginPage != -1) {
firstPage = "" + beginPage;
} else if (pageRange != null) {
StringTokenizer st = new StringTokenizer(pageRange, "--");
if (st.countTokens() == 2) {
firstPage = st.nextToken();
} else if (st.countTokens() == 1)
firstPage = pageRange;
}
String year = null;
if ( theBiblio.getNormalizedPublicationDate() != null ) {
year = "" + theBiblio.getNormalizedPublicationDate().getYear();
}
if (year == null)
year = theBiblio.getYear();
/*if (aut != null) {
aut = TextUtilities.removeAccents(aut);
}
if (title != null) {
title = TextUtilities.removeAccents(title);
}
if (journalTitle != null) {
journalTitle = TextUtilities.removeAccents(journalTitle);
}*/
if (StringUtils.isNotBlank(doi)) {
// call based on the identified DOI
arguments = new HashMap<String,String>();
arguments.put("doi", doi);
}
if (StringUtils.isNotBlank(halId)) {
// call based on the identified HAL ID
if (arguments == null)
arguments = new HashMap<String,String>();
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF)
arguments.put("halid", halId);
}
if (StringUtils.isNotBlank(rawCitation)) {
// call with full raw string
if (arguments == null)
arguments = new HashMap<String,String>();
if ( (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) ||
StringUtils.isBlank(doi) )
arguments.put("query.bibliographic", rawCitation);
}
if (StringUtils.isNotBlank(title)) {
// call based on partial metadata
if (arguments == null)
arguments = new HashMap<String,String>();
if ( (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) ||
(StringUtils.isBlank(rawCitation) && StringUtils.isBlank(doi)) )
arguments.put("query.title", title);
}
if (StringUtils.isNotBlank(aut)) {
// call based on partial metadata
if (arguments == null)
arguments = new HashMap<String,String>();
if ( (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) ||
(StringUtils.isBlank(rawCitation) && StringUtils.isBlank(doi)) )
arguments.put("query.author", aut);
}
if (StringUtils.isNotBlank(journalTitle)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("query.container-title", journalTitle);
}
}
if (StringUtils.isNotBlank(volume)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("volume", volume);
}
}
if (StringUtils.isNotBlank(firstPage)) {
// call based on partial metadata
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("firstPage", firstPage);
}
}
if (StringUtils.isNotBlank(year)) {
// publication year metadata, CrossRef has no year query field, they are supported by the query.bibliographic
// field and filter
if (GrobidProperties.getInstance().getConsolidationService() != GrobidConsolidationService.CROSSREF) {
if (arguments == null)
arguments = new HashMap<String,String>();
arguments.put("year", year);
}
}
if (arguments == null || arguments.size() == 0) {
n++;
continue;
}
if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF) {
if (StringUtils.isBlank(doi) && StringUtils.isBlank(rawCitation) &&
(StringUtils.isBlank(aut) || StringUtils.isBlank(title)) ) {
// there's not enough information for a crossref request, which might always return a result
n++;
continue;
}
}
if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.CROSSREF)
arguments.put("rows", "1"); // we just request the top-one result
else if (GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.GLUTTON) {
// GROBID has already parsed the reference, so no need to redo this in glutton
arguments.put("parseReference", "false");
}
final boolean doiQuery;
try {
//CrossrefRequestListener<BiblioItem> requestListener = new CrossrefRequestListener<BiblioItem>();
if (cntManager != null) {
cntManager.i(ConsolidationCounters.CONSOLIDATION);
}
if ( StringUtils.isNotBlank(doi) && (cntManager != null) ) {
cntManager.i(ConsolidationCounters.CONSOLIDATION_PER_DOI);
doiQuery = true;
} else {
doiQuery = false;
}
client.<BiblioItem>pushRequest("works", arguments, workDeserializer, threadId, new CrossrefRequestListener<BiblioItem>(n) {
@Override
public void onSuccess(List<BiblioItem> res) {
if ((res != null) && (res.size() > 0) ) {
// for CrossRef API we need here to post-validate if the found item corresponds
// to the one requested in order to avoid false positive
// Glutton has its own validation mechanisms
for(BiblioItem oneRes : res) {
if ((GrobidProperties.getInstance().getConsolidationService() == GrobidConsolidationService.GLUTTON) ||
postValidation(theBiblio, oneRes)) {
oneRes.setLabeledTokens(theBiblio.getLabeledTokens());
results.put(Integer.valueOf(getRank()), oneRes);
if (cntManager != null) {
cntManager.i(ConsolidationCounters.CONSOLIDATION_SUCCESS);
if (doiQuery)
cntManager.i(ConsolidationCounters.CONSOLIDATION_PER_DOI_SUCCESS);
}
break;
}
}
}
}
@Override
public void onError(int status, String message, Exception exception) {
LOGGER.info("Consolidation service returns error ("+status+") : "+message);
}
});
} catch(Exception e) {
LOGGER.info("Consolidation error - ", e);
}
n++;
}
client.finish(threadId);
return results;
}
/**
* The public CrossRef API is a search API, and thus returns
* many false positives. It is necessary to validate return results
* against the (incomplete) source bibliographic item to block
* inconsistent results.
*/
private boolean postValidation(BiblioItem source, BiblioItem result) {
boolean valid = true;
// check main metadata available in source with fuzzy matching
/*if (!StringUtils.isBlank(source.getTitle()) && !StringUtils.isBlank(source.getTitle())) {
//System.out.println(source.getTitle() + " / " + result.getTitle() + " = " + ratcliffObershelpDistance(source.getTitle(), result.getTitle(), false));
if (ratcliffObershelpDistance(source.getTitle(), result.getTitle(), false) < 0.8)
return false;
}*/
if (!StringUtils.isBlank(source.getFirstAuthorSurname()) &&
!StringUtils.isBlank(result.getFirstAuthorSurname())) {
//System.out.println(source.getFirstAuthorSurname() + " / " + result.getFirstAuthorSurname() + " = " +
// ratcliffObershelpDistance(source.getFirstAuthorSurname(), result.getFirstAuthorSurname(), false));
if (ratcliffObershelpDistance(source.getFirstAuthorSurname(),result.getFirstAuthorSurname(), false) < 0.8)
return false;
}
/*if (!StringUtils.isBlank(source.getPublicationDate()) &&
!StringUtils.isBlank(result.getPublicationDate())) {
if (!source.getPublicationDate().equals(result.getPublicationDate()))
valid = false;
}*/
return valid;
}
private double ratcliffObershelpDistance(String string1, String string2, boolean caseDependent) {
if ( StringUtils.isBlank(string1) || StringUtils.isBlank(string2) )
return 0.0;
Double similarity = 0.0;
if (!caseDependent) {
string1 = string1.toLowerCase();
string2 = string2.toLowerCase();
}
if (string1.equals(string2)) {
similarity = 1.0;
}
if ( isNotEmpty(string1) && isNotEmpty(string2) ) {
Option<Object> similarityObject =
RatcliffObershelpMetric.compare(string1, string2);
if (similarityObject.isDefined()) {
similarity = (Double) similarityObject.get();
}
}
return similarity;
}
public Funder consolidateFunder(Funder funder) {
final List<Funder> results = new ArrayList<>();
Map<String, String> arguments = new HashMap<String,String>();
// CrossRef does not manage stopwords in funder search and has no usable term frequency, so we need
// to remove basic stopwords in the query to have something manageable from CrossRef
String funderNameString = funder.getFullName();
if (funderNameString == null || funderNameString.length() == 0)
return null;
funderNameString = TextUtilities.removeFieldStopwords(funderNameString);
arguments.put("query", funderNameString);
arguments.put("rows", "10"); // we request the top-10 results, because there are a lot of noise
// and we need many candidates in the pairwise comparison step
long threadId = Thread.currentThread().getId();
try {
client.pushRequest("funders", arguments, funderDeserializer, threadId, new CrossrefRequestListener<Funder>(0) {
@Override
public void onSuccess(List<Funder> res) {
if ((res != null) && (res.size() > 0) ) {
// we need here to post-check the candidates in a pairwise comparison
// in order to avoid false positive
for(Funder oneRes : res) {
/*
Glutton integrates its own post-validation, so we can skip post-validation in GROBID when it is used as
consolidation service. However, with CrossRef, post-validation is mandatory to control false positives.
*/
if (oneRes.getFullName() != null) {
String localFullName = oneRes.getFullName();
localFullName = TextUtilities.removeFieldStopwords(localFullName);
//if (localFullName.toLowerCase().equals(arguments.get("query").toLowerCase())) {
if (ratcliffObershelpDistance(localFullName, arguments.get("query"), false)>0.9) {
results.add(oneRes);
}
}
}
}
}
@Override
public void onError(int status, String message, Exception exception) {
LOGGER.info("Funder consolidation service returns error ("+status+") : "+message, exception);
}
});
} catch(Exception e) {
LOGGER.info("Funder consolidation error - ", e);
}
client.finish(threadId);
if (results.size() == 0)
return null;
else
return results.get(0);
}
public Map<Integer,Funder> consolidateFunders(List<Funder> funders) {
if (CollectionUtils.isEmpty(funders))
return null;
final Map<Integer,Funder> results = new HashMap<Integer,Funder>();
// init the results
int n = 0;
for(n=0; n<funders.size(); n++) {
results.put(n, null);
}
n = 0;
long threadId = Thread.currentThread().getId();
for(Funder funder : funders) {
//final List<Funder> results = new ArrayList<>();
Map<String, String> arguments = new HashMap<String,String>();
// CrossRef does not manage stopwords in funder search and has no usable term frequency, so we need
// to remove basic stopwords in the query to have something manageable from CrossRef
String funderNameString = funder.getFullName();
if (funderNameString == null || funderNameString.length() == 0)
return null;
funderNameString = TextUtilities.removeFieldStopwords(funderNameString);
arguments.put("query", funderNameString);
arguments.put("rows", "10"); // we request the top-10 results, because there are a lot of noise
// and we need many candidates in the pairwise comparison step
try {
client.pushRequest("funders", arguments, funderDeserializer, threadId, new CrossrefRequestListener<Funder>(n) {
@Override
public void onSuccess(List<Funder> res) {
List<Funder> localResults = new ArrayList<>();
if ((res != null) && (res.size() > 0) ) {
// we need here to post-check the candidates in a pairwise comparison
// in order to avoid false positive
for(Funder oneRes : res) {
/*
Glutton integrates its own post-validation, so we can skip post-validation in GROBID when it is used as
consolidation service. However, with CrossRef, post-validation is mandatory to control false positives.
*/
if (oneRes.getFullName() != null) {
String localFullName = oneRes.getFullName();
localFullName = TextUtilities.removeFieldStopwords(localFullName);
if (localFullName.toLowerCase().equals(arguments.get("query").toLowerCase())) {
localResults.add(oneRes);
break;
} else if (ratcliffObershelpDistance(localFullName, arguments.get("query"), false)>0.9) {
localResults.add(oneRes);
}
}
}
if (localResults.size() >0)
results.put(Integer.valueOf(getRank()), localResults.get(0));
}
}
@Override
public void onError(int status, String message, Exception exception) {
LOGGER.info("Funder consolidation service returns error ("+status+") : "+message, exception);
}
});
} catch(Exception e) {
LOGGER.info("Funder consolidation error - ", e);
}
n++;
}
client.finish(threadId);
return results;
}
}