-
Notifications
You must be signed in to change notification settings - Fork 428
/
Engine.java
executable file
·1074 lines (978 loc) · 46.2 KB
/
Engine.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grobid.core.engines;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.grobid.core.data.Affiliation;
import org.grobid.core.data.BibDataSet;
import org.grobid.core.data.BiblioItem;
import org.grobid.core.data.BiblioSet;
import org.grobid.core.data.ChemicalEntity;
import org.grobid.core.data.PatentItem;
import org.grobid.core.data.Person;
import org.grobid.core.document.Document;
import org.grobid.core.document.DocumentSource;
import org.grobid.core.engines.config.GrobidAnalysisConfig;
import org.grobid.core.engines.label.SegmentationLabels;
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.exceptions.GrobidResourceException;
import org.grobid.core.factory.GrobidFactory;
import org.grobid.core.factory.GrobidPoolingFactory;
import org.grobid.core.lang.Language;
import org.grobid.core.utilities.Consolidation;
import org.grobid.core.utilities.GrobidProperties;
import org.grobid.core.utilities.LanguageUtilities;
import org.grobid.core.utilities.Utilities;
import org.grobid.core.utilities.counters.CntManager;
import org.grobid.core.utilities.counters.impl.CntManagerFactory;
import org.grobid.core.utilities.crossref.CrossrefClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.*;
/**
* Class for managing the extraction of bibliographical information from PDF
* documents or raw text.
*
* @author Patrice Lopez
*/
public class Engine implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(Engine.class);
private final EngineParsers parsers = new EngineParsers();
//TODO: when using one instance of Engine in e.g. grobid-service, then make this field not static
private static CntManager cntManager = CntManagerFactory.getCntManager();
// The list of accepted languages
// the languages are encoded in ISO 3166
// if null, all languages are accepted.
private List<String> acceptedLanguages = null;
/**
* Parse a sequence of authors from a header, i.e. containing possibly
* reference markers.
*
* @param authorSequence - the string corresponding to a raw sequence of names
* @return the list of structured author object
*/
public List<Person> processAuthorsHeader(String authorSequence) throws Exception {
List<Person> result = parsers.getAuthorParser().processingHeader(authorSequence);
return result;
}
/**
* Parse a sequence of authors from a citation, i.e. containing no reference
* markers.
*
* @param authorSequence - the string corresponding to a raw sequence of names
* @return the list of structured author object
*/
public List<Person> processAuthorsCitation(String authorSequence) throws Exception {
List<Person> result = parsers.getAuthorParser().processingCitation(authorSequence);
return result;
}
/**
* Parse a list of independent sequences of authors from citations.
*
* @param authorSequences - the list of strings corresponding each to a raw sequence of
* names.
* @return the list of all recognized structured author objects for each
* sequence of authors.
*/
public List<List<Person>> processAuthorsCitationLists(List<String> authorSequences) throws Exception {
return null;
}
/**
* Parse a text block corresponding to an affiliation+address.
*
* @param addressBlock - the string corresponding to a raw affiliation+address
* @return the list of all recognized structured affiliation objects.
* @throws IOException
*/
public List<Affiliation> processAffiliation(String addressBlock) throws IOException {
return parsers.getAffiliationAddressParser().processing(addressBlock);
}
/**
* Parse a list of text blocks corresponding to an affiliation+address.
*
* @param addressBlocks - the list of strings corresponding each to a raw
* affiliation+address.
* @return the list of all recognized structured affiliation objects for
* each sequence of affiliation + address block.
*/
public List<List<Affiliation>> processAffiliations(List<String> addressBlocks) throws Exception {
List<List<Affiliation>> results = null;
for (String addressBlock : addressBlocks) {
List<Affiliation> localRes = parsers.getAffiliationAddressParser().processing(addressBlock);
if (results == null) {
results = new ArrayList<List<Affiliation>>();
}
results.add(localRes);
}
return results;
}
/**
* Parse a raw string containing dates.
*
* @param dateBlock - the string containing raw dates.
* @return the list of all structured date objects recognized in the string.
* @throws IOException
*/
public List<org.grobid.core.data.Date> processDate(String dateBlock) throws IOException {
List<org.grobid.core.data.Date> result = parsers.getDateParser().processing(dateBlock);
return result;
}
/**
* Parse a list of raw dates.
*
* @param dateBlocks - the list of strings each containing raw dates.
* @return the list of all structured date objects recognized in the string
* for each inputed string.
*/
/*public List<List<org.grobid.core.data.Date>> processDates(List<String> dateBlocks) {
return null;
}*/
/**
* Apply a parsing model for a given single raw reference string based on CRF
*
* @param reference the reference string to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @return the recognized bibliographical object
*/
public BiblioItem processRawReference(String reference, int consolidate) {
if (reference != null) {
reference = reference.replaceAll("\\\\", "");
}
return parsers.getCitationParser().processingString(reference, consolidate);
}
/**
* Apply a parsing model for a set of raw reference text based on CRF
*
* @param references the list of raw reference strings to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of recognized bibliographical objects
*/
public List<BiblioItem> processRawReferences(List<String> references, int consolidate) throws Exception {
List<BibDataSet> results = new ArrayList<BibDataSet>();
List<BiblioItem> finalResults = new ArrayList<BiblioItem>();
if (references == null || references.size() == 0)
return finalResults;
for (String reference : references) {
BiblioItem bib = parsers.getCitationParser().processingString(reference, 0);
//if ((bib != null) && !bib.rejectAsReference())
{
BibDataSet bds = new BibDataSet();
bds.setResBib(bib);
bds.setRawBib(reference);
results.add(bds);
}
}
if (results.size() == 0)
return finalResults;
// consolidation in a second stage to take advantage of parallel calls
if (consolidate != 0) {
Consolidation consolidator = Consolidation.getInstance();
if (consolidator.getCntManager() == null)
consolidator.setCntManager(cntManager);
Map<Integer,BiblioItem> resConsolidation = null;
try {
resConsolidation = consolidator.consolidate(results);
} catch(Exception e) {
throw new GrobidException(
"An exception occured while running consolidation on bibliographical references.", e);
}
if (resConsolidation != null) {
for(int i=0; i<results.size(); i++) {
BiblioItem resCitation = results.get(i).getResBib();
BiblioItem bibo = resConsolidation.get(Integer.valueOf(i));
if (bibo != null) {
if (consolidate == 1)
BiblioItem.correct(resCitation, bibo);
else if (consolidate == 2)
BiblioItem.injectDOI(resCitation, bibo);
}
finalResults.add(resCitation);
}
}
}
return finalResults;
}
/**
* Constructor for the Grobid engine instance.
*/
public Engine(boolean loadModels) {
/*
* Runtime.getRuntime().addShutdownHook(new Thread() {
*
* @Override public void run() { try { close(); } catch (IOException e)
* { LOGGER.error("Failed to close all resources: " + e); } } });
*/
if (loadModels)
parsers.initAll();
}
/**
* Apply a parsing model to the reference block of a PDF file based on CRF
*
* @param inputFile the path of the PDF file to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of parsed references as bibliographical objects enriched
* with citation contexts
*/
public List<BibDataSet> processReferences(File inputFile, int consolidate) {
return parsers.getCitationParser()
.processingReferenceSection(inputFile, null, parsers.getReferenceSegmenterParser(), consolidate);
}
/**
* Apply a parsing model to the reference block of a PDF file based on CRF
*
* @param inputFile the path of the PDF file to be processed
* @param md5Str MD5 digest of the PDF file to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of parsed references as bibliographical objects enriched
* with citation contexts
*/
public List<BibDataSet> processReferences(File inputFile, String md5Str, int consolidate) {
return parsers.getCitationParser()
.processingReferenceSection(inputFile, md5Str, parsers.getReferenceSegmenterParser(), consolidate);
}
/**
* Download a PDF file.
*
* @param url URL of the PDF to download
* @param dirName directory where to store the downloaded PDF
* @param name file name
*/
public String downloadPDF(String url, String dirName, String name) {
return Utilities.uploadFile(url, dirName, name);
}
/**
* Give the list of languages for which an extraction is allowed. If null,
* any languages will be processed
*
* @return the list of languages to be processed coded in ISO 3166.
*/
public List<String> getAcceptedLanguages() {
return acceptedLanguages;
}
/**
* Add a language to the list of accepted languages.
*
* @param lang the language in ISO 3166 to be added
*/
public void addAcceptedLanguages(String lang) {
if (acceptedLanguages == null) {
acceptedLanguages = new ArrayList<String>();
}
acceptedLanguages.add(lang);
}
/**
* Perform a language identification
*
* @param ext part
* @return language
*/
public Language runLanguageId(String filePath, String ext) {
try {
// we just skip the 50 first lines and get the next approx. 5000
// first characters,
// which should give a close to ~100% accuracy for the supported languages
String text = "";
FileInputStream fileIn = new FileInputStream(filePath.substring(0, filePath.length() - 3) + ext);
InputStreamReader reader = new InputStreamReader(fileIn, "UTF-8");
BufferedReader bufReader = new BufferedReader(reader);
String line;
int nbChar = 0;
while (((line = bufReader.readLine()) != null) && (nbChar < 5000)) {
if (line.length() == 0)
continue;
text += " " + line;
nbChar += line.length();
}
bufReader.close();
LanguageUtilities languageUtilities = LanguageUtilities.getInstance();
return languageUtilities.runLanguageId(text);
} catch (IOException e) {
throw new GrobidException("An exception occurred while running Grobid.", e);
}
}
/**
* Basic run for language identification, default is on the body of the
* current document.
*
* @return language id
*/
public Language runLanguageId(String filePath) {
return runLanguageId(filePath, "body");
}
/**
* Apply a parsing model for the header of a PDF file based on CRF, using
* first three pages of the PDF
*
* @param inputFile the path of the PDF file to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @param result bib result
* @return the TEI representation of the extracted bibliographical
* information
*/
public String processHeader(
String inputFile,
int consolidate,
boolean includeRawAffiliations,
BiblioItem result
) {
GrobidAnalysisConfig config = new GrobidAnalysisConfig.GrobidAnalysisConfigBuilder()
.startPage(0)
.endPage(2)
.consolidateHeader(consolidate)
.includeRawAffiliations(includeRawAffiliations)
.build();
return processHeader(inputFile, null, config, result);
}
/**
* Apply a parsing model for the header of a PDF file based on CRF, using
* first three pages of the PDF
*
* @param inputFile the path of the PDF file to be processed
* @param md5Str MD5 digest of the processed file
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @param result bib result
* @return the TEI representation of the extracted bibliographical
* information
*/
public String processHeader(
String inputFile,
String md5Str,
int consolidate,
boolean includeRawAffiliations,
BiblioItem result
) {
GrobidAnalysisConfig config = new GrobidAnalysisConfig.GrobidAnalysisConfigBuilder()
.startPage(0)
.endPage(2)
.consolidateHeader(consolidate)
.includeRawAffiliations(includeRawAffiliations)
.build();
return processHeader(inputFile, md5Str, config, result);
}
/**
* Apply a parsing model for the header of a PDF file based on CRF, using
* dynamic range of pages as header
*
* @param inputFile : the path of the PDF file to be processed
* @param consolidate the consolidation option allows GROBID to exploit Crossref web services for improving header
* information. 0 (no consolidation, default value), 1 (consolidate the citation and inject extra
* metadata) or 2 (consolidate the citation and inject DOI only)
* @param result bib result
*
* @return the TEI representation of the extracted bibliographical
* information
*/
public String processHeader(String inputFile, int consolidate, BiblioItem result) {
return processHeader(inputFile, null, GrobidAnalysisConfig.defaultInstance(), result);
}
public String processHeader(String inputFile, GrobidAnalysisConfig config, BiblioItem result) {
return processHeader(inputFile, null, config, result);
}
public String processHeader(String inputFile, String md5Str, GrobidAnalysisConfig config, BiblioItem result) {
// normally the BiblioItem reference must not be null, but if it is the
// case, we still continue
// with a new instance, so that the resulting TEI string is still
// delivered
if (result == null) {
result = new BiblioItem();
}
Pair<String, Document> resultTEI = parsers.getHeaderParser().processing(new File(inputFile), md5Str, result, config);
return resultTEI.getLeft();
}
/**
* Create training data for the monograph model based on the application of
* the current monograph text model on a new PDF
*
* @param inputFile : the path of the PDF file to be processed
* @param pathRaw : the path where to put the CRF feature file
* @param pathTEI : the path where to put the annotated TEI representation (the
* file to be corrected for gold-level training data)
* @param id : an optional ID to be used in the TEI file and the full text
* file, -1 if not used
*/
public void createTrainingMonograph(File inputFile, String pathRaw, String pathTEI, int id) {
Document doc = parsers.getMonographParser().createTrainingFromPDF(inputFile, pathRaw, pathTEI, id);
}
/**
* Generate blank training data from provided directory of PDF documents, i.e. where TEI files are text only
* without tags. This can be used to start from scratch any new model.
*
* @param inputFile : the path of the PDF file to be processed
* @param pathRaw : the path where to put the CRF feature file
* @param pathTEI : the path where to put the annotated TEI representation (the
* file to be annotated for "from scratch" training data)
* @param id : an optional ID to be used in the TEI file and the full text
* file, -1 if not used
*/
public void createTrainingBlank(File inputFile, String pathRaw, String pathTEI, int id) {
parsers.getSegmentationParser().createBlankTrainingData(inputFile, pathRaw, pathTEI, id);
}
/**
* Create training data for all models based on the application of
* the current full text model on a new PDF
*
* @param inputFile : the path of the PDF file to be processed
* @param pathRaw : the path where to put the CRF feature file
* @param pathTEI : the path where to put the annotated TEI representation (the
* file to be corrected for gold-level training data)
* @param id : an optional ID to be used in the TEI file, -1 if not used
*/
public void createTraining(File inputFile, String pathRaw, String pathTEI, int id) {
System.out.println(inputFile.getPath());
Document doc = parsers.getFullTextParser().createTraining(inputFile, pathRaw, pathTEI, id);
}
/**
*
* //TODO: remove invalid JavaDoc once refactoring is done and tested (left for easier reference)
* Parse and convert the current article into TEI, this method performs the
* whole parsing and conversion process. If onlyHeader is true, than only
* the tei header data will be created.
*
* @param inputFile - absolute path to the pdf to be processed
* @param config - Grobid config
* @return the resulting structured document as a TEI string.
*/
public String fullTextToTEI(File inputFile,
GrobidAnalysisConfig config) throws Exception {
return fullTextToTEIDoc(inputFile, null, config).getTei();
}
/**
*
* //TODO: remove invalid JavaDoc once refactoring is done and tested (left for easier reference)
* Parse and convert the current article into TEI, this method performs the
* whole parsing and conversion process. If onlyHeader is true, than only
* the tei header data will be created.
*
* @param inputFile - absolute path to the pdf to be processed
* @param md5Str - MD5 digest of the PDF file to be processed
* @param config - Grobid config
* @return the resulting structured document as a TEI string.
*/
public String fullTextToTEI(File inputFile,
String md5Str,
GrobidAnalysisConfig config) throws Exception {
return fullTextToTEIDoc(inputFile, md5Str, config).getTei();
}
public Document fullTextToTEIDoc(File inputFile,
String md5Str,
GrobidAnalysisConfig config) throws Exception {
FullTextParser fullTextParser = parsers.getFullTextParser();
Document resultDoc;
LOGGER.debug("Starting processing fullTextToTEI on " + inputFile);
long time = System.currentTimeMillis();
resultDoc = fullTextParser.processing(inputFile, md5Str, config);
LOGGER.debug("Ending processing fullTextToTEI on " + inputFile + ". Time to process: "
+ (System.currentTimeMillis() - time) + "ms");
return resultDoc;
}
public Document fullTextToTEIDoc(File inputFile,
GrobidAnalysisConfig config) throws Exception {
return fullTextToTEIDoc(inputFile, null, config);
}
public Document fullTextToTEIDoc(DocumentSource documentSource,
GrobidAnalysisConfig config) throws Exception {
FullTextParser fullTextParser = parsers.getFullTextParser();
Document resultDoc;
LOGGER.debug("Starting processing fullTextToTEI on " + documentSource);
long time = System.currentTimeMillis();
resultDoc = fullTextParser.processing(documentSource, config);
LOGGER.debug("Ending processing fullTextToTEI on " + documentSource + ". Time to process: "
+ (System.currentTimeMillis() - time) + "ms");
return resultDoc;
}
/**
* Process all the PDF in a given directory with a segmentation process and
* produce the corresponding training data format files for manual
* correction. The goal of this method is to help to produce additional
* traning data based on an existing model.
*
* @param directoryPath - the path to the directory containing PDF to be processed.
* @param resultPath - the path to the directory where the results as XML files
* shall be written.
* @param ind - identifier integer to be included in the resulting files to
* identify the training case. This is optional: no identifier
* will be included if ind = -1
* @return the number of processed files.
*/
public int batchCreateTraining(String directoryPath, String resultPath, int ind) {
try {
File path = new File(directoryPath);
// we process all pdf files in the directory
File[] refFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
System.out.println(name);
return name.endsWith(".pdf") || name.endsWith(".PDF");
}
});
if (refFiles == null)
return 0;
System.out.println(refFiles.length + " files to be processed.");
int n = 0;
if (ind == -1) {
// for undefined identifier (value at -1), we initialize it to 0
n = 1;
}
for (final File pdfFile : refFiles) {
try {
createTraining(pdfFile, resultPath, resultPath, ind + n);
} catch (final Exception exp) {
LOGGER.error("An error occured while processing the following pdf: "
+ pdfFile.getPath(), exp);
}
if (ind != -1)
n++;
}
return refFiles.length;
} catch (final Exception exp) {
throw new GrobidException("An exception occured while running Grobid batch.", exp);
}
}
/**
* Process all the PDF in a given directory with a monograph process and
* produce the corresponding training data format files for manual
* correction. The goal of this method is to help to produce additional
* traning data based on an existing model.
*
* @param directoryPath - the path to the directory containing PDF to be processed.
* @param resultPath - the path to the directory where the results as XML files
* and CRF feature files shall be written.
* @param ind - identifier integer to be included in the resulting files to
* identify the training case. This is optional: no identifier
* will be included if ind = -1
* @return the number of processed files.
*/
public int batchCreateTrainingMonograph(String directoryPath, String resultPath, int ind) {
try {
File path = new File(directoryPath);
// we process all pdf files in the directory
File[] refFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
System.out.println(name);
return name.endsWith(".pdf") || name.endsWith(".PDF");
}
});
if (refFiles == null)
return 0;
System.out.println(refFiles.length + " files to be processed.");
int n = 0;
if (ind == -1) {
// for undefined identifier (value at -1), we initialize it to 0
n = 1;
}
for (final File pdfFile : refFiles) {
try {
createTrainingMonograph(pdfFile, resultPath, resultPath, ind + n);
} catch (final Exception exp) {
LOGGER.error("An error occured while processing the following pdf: "
+ pdfFile.getPath(), exp);
}
if (ind != -1)
n++;
}
return refFiles.length;
} catch (final Exception exp) {
throw new GrobidException("An exception occured while running Grobid batch.", exp);
}
}
/**
* Process all the PDF in a given directory with a pdf extraction and
* produce blank training data, i.e. TEI files with text only
* without tags. This can be used to start from scratch any new model.
*
* @param directoryPath - the path to the directory containing PDF to be processed.
* @param resultPath - the path to the directory where the results as XML files
* and default CRF feature files shall be written.
* @param ind - identifier integer to be included in the resulting files to
* identify the training case. This is optional: no identifier
* will be included if ind = -1
* @return the number of processed files.
*/
public int batchCreateTrainingBlank(String directoryPath, String resultPath, int ind) {
try {
File path = new File(directoryPath);
// we process all pdf files in the directory
File[] refFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
System.out.println(name);
return name.endsWith(".pdf") || name.endsWith(".PDF");
}
});
if (refFiles == null)
return 0;
System.out.println(refFiles.length + " files to be processed.");
int n = 0;
if (ind == -1) {
// for undefined identifier (value at -1), we initialize it to 0
n = 1;
}
for (final File pdfFile : refFiles) {
try {
createTrainingBlank(pdfFile, resultPath, resultPath, ind + n);
} catch (final Exception exp) {
LOGGER.error("An error occured while processing the following pdf: "
+ pdfFile.getPath(), exp);
}
if (ind != -1)
n++;
}
return refFiles.length;
} catch (final Exception exp) {
throw new GrobidException("An exception occured while running Grobid batch.", exp);
}
}
/**
* Get the TEI XML string corresponding to the recognized header text
*/
public static String header2TEI(BiblioItem resHeader) {
return resHeader.toTEI(0);
}
/**
* Get the BibTeX string corresponding to the recognized header text
*/
public static String header2BibTeX(BiblioItem resHeader) {
return resHeader.toBibTeX();
}
/**
* Get the TEI XML string corresponding to the recognized citation section
*/
public static String references2TEI2(String path, List<BibDataSet> resBib) {
StringBuilder result = new StringBuilder();
result.append("<tei>\n");
BiblioSet bs = new BiblioSet();
for (BibDataSet bib : resBib) {
BiblioItem bit = bib.getResBib();
bit.buildBiblioSet(bs, path);
}
result.append(bs.toTEI());
result.append("<listbibl>\n");
for (BibDataSet bib : resBib) {
BiblioItem bit = bib.getResBib();
result.append("\n").append(bit.toTEI2(bs));
}
result.append("\n</listbibl>\n</tei>\n");
return result.toString();
}
/**
* Get the TEI XML string corresponding to the recognized citation section,
* with pointers and advanced structuring
*/
public static String references2TEI(String path, List<BibDataSet> resBib) {
StringBuilder result = new StringBuilder();
result.append("<listbibl>\n");
int p = 0;
for (BibDataSet bib : resBib) {
BiblioItem bit = bib.getResBib();
bit.setPath(path);
result.append("\n").append(bit.toTEI(p));
p++;
}
result.append("\n</listbibl>\n");
return result.toString();
}
/**
* Get the BibTeX string corresponding to the recognized citation section
*/
public String references2BibTeX(String path, List<BibDataSet> resBib) {
StringBuilder result = new StringBuilder();
for (BibDataSet bib : resBib) {
BiblioItem bit = bib.getResBib();
bit.setPath(path);
result.append("\n").append(bit.toBibTeX());
}
return result.toString();
}
/**
* Get the TEI XML string corresponding to the recognized citation section
* for a particular citation
*/
public static String reference2TEI(String path, List<BibDataSet> resBib, int i) {
StringBuilder result = new StringBuilder();
if (resBib != null) {
if (i <= resBib.size()) {
BibDataSet bib = resBib.get(i);
BiblioItem bit = bib.getResBib();
bit.setPath(path);
result.append(bit.toTEI(i));
}
}
return result.toString();
}
/**
* Get the BibTeX string corresponding to the recognized citation section
* for a given citation
*/
public static String reference2BibTeX(String path, List<BibDataSet> resBib, int i) {
StringBuilder result = new StringBuilder();
if (resBib != null) {
if (i <= resBib.size()) {
BibDataSet bib = resBib.get(i);
BiblioItem bit = bib.getResBib();
bit.setPath(path);
result.append(bit.toBibTeX());
}
}
return result.toString();
}
/**
* Extract and parse both patent and non patent references within a patent text. Result are provided as a BibDataSet
* with offset position instanciated relative to input text and as PatentItem containing both "WISIWIG" results (the
* patent reference attributes as they appear in the text) and the attributes in DOCDB format (format according to
* WIPO and ISO standards). Patent references' offset positions are also given in the PatentItem object.
*
* @param text the string corresponding to the text body of the patent.
* @param nplResults the list of extracted and parsed non patent references as BiblioItem object. This
* list must be instantiated before calling the method for receiving the results.
* @param patentResults the list of extracted and parsed patent references as PatentItem object. This list
* must be instantiated before calling the method for receiving the results.
* @param consolidateCitations the consolidation option allows GROBID to exploit Crossref web services for improving
* header information. 0 (no consolidation, default value), 1 (consolidate the citation
* and inject extra metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of extracted and parserd patent and non-patent references encoded in TEI.
*/
public String processAllCitationsInPatent(String text,
List<BibDataSet> nplResults,
List<PatentItem> patentResults,
int consolidateCitations,
boolean includeRawCitations) throws Exception {
if ((nplResults == null) && (patentResults == null)) {
return null;
}
// we initialize the attribute individually for readability...
boolean filterDuplicate = false;
return parsers.getReferenceExtractor().extractAllReferencesString(text, filterDuplicate,
consolidateCitations, includeRawCitations, patentResults, nplResults);
}
/**
* Extract and parse both patent and non patent references within a patent in ST.36 format. Result are provided as a
* BibDataSet with offset position instantiated relative to input text and as PatentItem containing both "WISIWIG"
* results (the patent reference attributes as they appear in the text) and the attributes in DOCDB format (format
* according to WIPO and ISO standards). Patent references' offset positions are also given in the PatentItem
* object.
*
* @param nplResults the list of extracted and parsed non patent references as BiblioItem object. This
* list must be instanciated before calling the method for receiving the results.
* @param patentResults the list of extracted and parsed patent references as PatentItem object. This list
* must be instanciated before calling the method for receiving the results.
* @param consolidateCitations the consolidation option allows GROBID to exploit Crossref web services for improving
* header information. 0 (no consolidation, default value), 1 (consolidate the citation
* and inject extra metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of extracted and parserd patent and non-patent references encoded in TEI.
* @throws Exception if sth. went wrong
*/
public String processAllCitationsInXMLPatent(String xmlPath, List<BibDataSet> nplResults,
List<PatentItem> patentResults,
int consolidateCitations,
boolean includeRawCitations) throws Exception {
if ((nplResults == null) && (patentResults == null)) {
return null;
}
// we initialize the attribute individually for readability...
boolean filterDuplicate = false;
return parsers.getReferenceExtractor().extractAllReferencesXMLFile(xmlPath, filterDuplicate,
consolidateCitations, includeRawCitations, patentResults, nplResults);
}
/**
* Extract and parse both patent and non patent references within a patent
* in PDF format. Result are provided as a BibDataSet with offset position
* instanciated relative to input text and as PatentItem containing both
* "WISIWIG" results (the patent reference attributes as they appear in the
* text) and the attributes in DOCDB format (format according to WIPO and
* ISO standards). Patent references' offset positions are also given in the
* PatentItem object.
*
* @param pdfPath pdf path
* @param nplResults the list of extracted and parsed non patent references as
* BiblioItem object. This list must be instanciated before
* calling the method for receiving the results.
* @param patentResults the list of extracted and parsed patent references as
* PatentItem object. This list must be instanciated before
* calling the method for receiving the results.
* @param consolidateCitations the consolidation option allows GROBID to exploit Crossref web services for improving
* header information. 0 (no consolidation, default value), 1 (consolidate the citation
* and inject extra metadata) or 2 (consolidate the citation and inject DOI only)
* @return the list of extracted and parserd patent and non-patent references
* encoded in TEI.
* @throws Exception if sth. went wrong
*/
public String processAllCitationsInPDFPatent(String pdfPath, List<BibDataSet> nplResults,
List<PatentItem> patentResults,
int consolidateCitations,
boolean includeRawCitations) throws Exception {
if ((nplResults == null) && (patentResults == null)) {
return null;
}
// we initialize the attribute individually for readability...
boolean filterDuplicate = false;
return parsers.getReferenceExtractor().extractAllReferencesPDFFile(pdfPath, filterDuplicate,
consolidateCitations, includeRawCitations, patentResults, nplResults);
}
/**
* Extract and parse both patent and non patent references within a patent
* in PDF format. Results are provided as JSON annotations with coordinates
* of the annotations in the orignal PDF and reference informations in DOCDB
* format (format according to WIPO and ISO standards).
*
* @param pdfPath pdf path
* @param consolidateCitations the consolidation option allows GROBID to exploit Crossref web services for improving
* header information. 0 (no consolidation, default value), 1 (consolidate the citation
* and inject extra metadata) or 2 (consolidate the citation and inject DOI only)
*
* @return JSON annotations with extracted and parsed patent and non-patent references
* together with coordinates in the original PDF.
*/
public String annotateAllCitationsInPDFPatent(String pdfPath,
int consolidateCitations,
boolean includeRawCitations) throws Exception {
List<BibDataSet> nplResults = new ArrayList<BibDataSet>();
List<PatentItem> patentResults = new ArrayList<PatentItem>();
// we initialize the attribute individually for readability...
boolean filterDuplicate = false;
return parsers.getReferenceExtractor().annotateAllReferencesPDFFile(pdfPath, filterDuplicate,
consolidateCitations, includeRawCitations, patentResults, nplResults);
}
/*public void processCitationPatentTEI(String teiPath, String outTeiPath,
int consolidateCitations) throws Exception {
try {
InputStream inputStream = new FileInputStream(new File(teiPath));
OutputStream output = new FileOutputStream(new File(outTeiPath));
final TeiStAXParser parser = new TeiStAXParser(inputStream, output, false,
consolidateCitations);
parser.parse();
inputStream.close();
output.close();
} catch (Exception e) {
throw new GrobidException("An exception occured while running Grobid.", e);
}
}*/
/**
* Process an XML patent document with a patent citation extraction and
* produce the corresponding training data format files for manual
* correction. The goal of this method is to help to produce additional
* traning data based on an existing model.
*
* @param pathXML - the path to the XML patent document to be processed.
* @param resultPath - the path to the directory where the results as XML files
* shall be written.
*/
public void createTrainingPatentCitations(String pathXML, String resultPath)
throws Exception {
parsers.getReferenceExtractor().generateTrainingData(pathXML, resultPath);
}
/**
* Process all the XML patent documents in a given directory with a patent
* citation extraction and produce the corresponding training data format
* files for manual correction. The goal of this method is to help to
* produce additional traning data based on an existing model.
*
* @param directoryPath - the path to the directory containing XML files to be
* processed.
* @param resultPath - the path to the directory where the results as XML files
* shall be written.
* @return the number of processed files.
*/
public int batchCreateTrainingPatentcitations(String directoryPath, String resultPath)
throws Exception {
try {
File path = new File(directoryPath);
// we process all xml files in the directory
File[] refFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".xml") || name.endsWith(".XML") ||
name.endsWith(".xml.gz") || name.endsWith(".XML.gz");
}
});
if (refFiles == null)
return 0;
// System.out.println(refFiles.length + " files to be processed.");
int n = 0;
for (; n < refFiles.length; n++) {
File xmlFile = refFiles[n];