/
NERFeatureFactory.java
2230 lines (1988 loc) · 101 KB
/
NERFeatureFactory.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
// NERFeatureFactory -- features for a probabilistic Named Entity Recognizer
// Copyright (c) 2002-2008 Leland Stanford Junior University
// Additional features (c) 2003 The University of Edinburgh
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information, bug reports, fixes, contact:
// Christopher Manning
// Dept of Computer Science, Gates 1A
// Stanford CA 94305-9010
// USA
// Support/Questions: java-nlp-user@lists.stanford.edu
// Licensing: java-nlp-support@lists.stanford.edu
// http://nlp.stanford.edu/downloads/crf-classifier.shtml
package edu.stanford.nlp.ie;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.ling.CoreAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.CoreLabel.GenericAnnotation;
import edu.stanford.nlp.objectbank.ObjectBank;
import edu.stanford.nlp.process.WordShapeClassifier;
import edu.stanford.nlp.sequences.Clique;
import edu.stanford.nlp.sequences.CoNLLDocumentReaderAndWriter;
import edu.stanford.nlp.sequences.FeatureFactory;
import edu.stanford.nlp.sequences.SeqClassifierFlags;
import edu.stanford.nlp.trees.TreeCoreAnnotations;
import edu.stanford.nlp.trees.international.pennchinese.RadicalMap;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.PaddedList;
import edu.stanford.nlp.util.Timing;
/**
* Features for Named Entity Recognition. The code here creates the features
* by processing Lists of CoreLabels.
* Look at {@link SeqClassifierFlags} to see where the flags are set for
* what options to use for what flags.
* <p>
* To add a new feature extractor, you should do the following:
* <ol>
* <li>Add a variable (boolean, int, String, etc. as appropriate) to
* SeqClassifierFlags to mark if the new extractor is turned on or
* its value, etc. Add it at the <i>bottom</i> of the list of variables
* currently in the class (this avoids problems with older serialized
* files breaking). Make the default value of the variable false/null/0
* (this is again for backwards compatibility).</li>
* <li>Add a clause to the big if/then/else of setProperties(Properties) in
* SeqClassifierFlags. Unless it is a macro option, make the option name
* the same as the variable name used in step 1.</li>
* <li>Add code to NERFeatureFactory for this feature. First decide which
* classes (hidden states) are involved in the feature. If only the
* current class, you add the feature extractor to the
* <code>featuresC</code> code, if both the current and previous class,
* then <code>featuresCpC</code>, etc.</li>
* </ol>
* <p> Parameters can be defined using a Properties file
* (specified on the command-line with <code>-prop</code> <i>propFile</i>),
* or directly on the command line. The following properties are recognized:
* </p>
* <table border="1">
* <tr><td><b>Property Name</b></td><td><b>Type</b></td><td><b>Default Value</b></td><td><b>Description</b></td></tr>
* <tr><td> loadClassifier </td><td>String</td><td>n/a</td><td>Path to serialized classifier to load</td></tr>
* <tr><td> loadAuxClassifier </td><td>String</td><td>n/a</td><td>Path to auxiliary classifier to load.</td></tr>
* <tr><td> serializeTo</td><td>String</td><td>n/a</td><td>Path to serialize classifier to</td></tr>
* <tr><td> trainFile</td><td>String</td><td>n/a</td><td>Path of file to use as training data</td></tr>
* <tr><td> testFile</td><td>String</td><td>n/a</td><td>Path of file to use as training data</td></tr>
* <tr><td> map</td><td>String</td><td>see below</td><td>This applies at training time or if testing on tab-separated column data. It says what is in each column. It doesn't apply when running on plain text data. The simplest scenario for training is having words and classes in two column. word=0,answer=1 is the default if conllNoTags is specified; otherwise word=0,tag=1,answer=2 is the default. But you can add other columns, such as for a part-of-speech tag, presences in a lexicon, etc. That would only be useful at runtime if you have part-of-speech information or whatever available and are passing it in with the tokens (that is, you can pass to classify CoreLabel tokens with additional fields stored in them).</td></tr>
* <tr><td> useWord</td><td>boolean</td><td>true</td><td>Gives you feature for w</td></tr>
* <tr><td> useBinnedLength</td><td>String</td><td>null</td><td>If non-null, treat as a sequence of comma separated integer bounds, where items above the previous bound up to the next bound are binned Len-<i>range</i></td></tr>
* <tr><td> useNGrams</td><td>boolean</td><td>false</td><td>Make features from letter n-grams, i.e., substrings of the word</td></tr>
* <tr><td> lowercaseNGrams</td><td>boolean</td><td>false</td><td>Make features from letter n-grams only lowercase</td></tr>
* <tr><td> dehyphenateNGrams</td><td>boolean</td><td>false</td><td>Remove hyphens before making features from letter n-grams</td></tr>
* <tr><td> conjoinShapeNGrams</td><td>boolean</td><td>false</td><td>Conjoin word shape and n-gram features</td></tr>
* <tr><td> useNeighborNGrams</td><td>boolean</td><td>false</td><td>Use letter n-grams for the previous and current words in the CpC clique. This feature helps languages such as Chinese, but not so much for English</td></tr>
* <tr><td> usePrev</td><td>boolean</td><td>false</td><td>Gives you feature for (pw,c), and together with other options enables other previous features, such as (pt,c) [with useTags)</td></tr>
* <tr><td> useNext</td><td>boolean</td><td>false</td><td>Gives you feature for (nw,c), and together with other options enables other next features, such as (nt,c) [with useTags)</td></tr>
* <tr><td> useTags</td><td>boolean</td><td>false</td><td>Gives you features for (t,c), (pt,c) [if usePrev], (nt,c) [if useNext]</td></tr>
* <tr><td> useWordPairs</td><td>boolean</td><td>false</td><td>Gives you
* features for (pw, w, c) and (w, nw, c)</td></tr>
* <p>
* <tr><td> useGazettes</td><td>boolean</td><td>false</td><td>If true, use gazette features (defined by other flags)</td></tr>
* <tr><td> gazette</td><td>String</td><td>null</td><td>The value can be one or more filenames (names separated by a comma, semicolon or space).
* If provided gazettes are loaded from these files. Each line should be an entity class name, followed by whitespace followed by an entity (which might be a phrase of several tokens with a single space between words).
* Giving this property turns on useGazettes, so you normally don't need to specify it (but can use it to turn off gazettes specified in a properties file).</td></tr>
* <tr><td> sloppyGazette</td><td>boolean</td><td>false</td><td>If true, a gazette feature fires when any token of a gazette entry matches</td></tr>
* <tr><td> cleanGazette</td><td>boolean</td><td>false</td><td></td>If true, a gazette feature fires when all tokens of a gazette entry match</tr>
* <p>
* <tr><td> wordShape</td><td>String</td><td>none</td><td>Either "none" for no wordShape use, or the name of a word shape function recognized by {@link WordShapeClassifier#lookupShaper(String)}</td></tr>
* <tr><td> useSequences</td><td>boolean</td><td>true</td><td>Does not use any class combination features if this is false</td></tr>
* <tr><td> usePrevSequences</td><td>boolean</td><td>false</td><td>Does not use any class combination features using previous classes if this is false</td></tr>
* <tr><td> useNextSequences</td><td>boolean</td><td>false</td><td>Does not use any class combination features using next classes if this is false</td></tr>
* <tr><td> useLongSequences</td><td>boolean</td><td>false</td><td>Use plain higher-order state sequences out to minimum of length or maxLeft</td></tr>
* <tr><td> useBoundarySequences</td><td>boolean</td><td>false</td><td>Use extra second order class sequence features when previous is CoNLL boundary, so entity knows it can span boundary.</td></tr>
* <tr><td> useTaggySequences</td><td>boolean</td><td>false</td><td>Use first, second, and third order class and tag sequence interaction features</td></tr>
* <tr><td> useExtraTaggySequences</td><td>boolean</td><td>false</td><td>Add in sequences of tags with just current class features</td></tr>
* <tr><td> useTaggySequencesShapeInteraction</td><td>boolean</td><td>false</td><td>Add in terms that join sequences of 2 or 3 tags with the current shape</td></tr>
* <tr><td> strictlyFirstOrder</td><td>boolean</td><td>false</td><td>As an override to whatever other options are in effect, deletes all features other than C and CpC clique features when building the classifier</td></tr>
* <tr><td> entitySubclassification</td><td>String</td><td>"IO"</td><td>If
* set, convert the labeling of classes (but not the background) into
* one of several alternate encodings (IO, IOB1, IOB2, IOE1, IOE2, SBIEO, with
* a S(ingle), B(eginning),
* E(nding), I(nside) 4-way classification for each class. By default, we
* either do no re-encoding, or the CoNLLDocumentIteratorFactory does a
* lossy encoding as IO. Note that this is all CoNLL-specific, and depends on
* their way of prefix encoding classes, and is only implemented by
* the CoNLLDocumentIteratorFactory. </td></tr>
* <p/>
* <tr><td> useSum</td><td>boolean</td><td>false</td><td></td></tr>
* <tr><td> tolerance</td><td>double</td><td>1e-4</td><td>Convergence tolerance in optimization</td></tr>
* <tr><td> printFeatures</td><td>String</td><td>null</td><td>print out all the features generated by the classifier for a dataset to a file based on this name (starting with "features-", suffixed "-1" and "-2" for train and test). This simply prints the feature names, one per line.</td></tr>
* <tr><td> printFeaturesUpto</td><td>int</td><td>-1</td><td>Print out features for only the first this many datums, if the value is positive. </td></tr>
* <p/>
* <tr><td> useSymTags</td><td>boolean</td><td>false</td><td>Gives you
* features (pt, t, nt, c), (t, nt, c), (pt, t, c)</td></tr>
* <tr><td> useSymWordPairs</td><td>boolean</td><td>false</td><td>Gives you
* features (pw, nw, c)</td></tr>
* <p/>
* <tr><td> printClassifier</td><td>String</td><td>null</td><td>Style in which to print the classifier. One of: HighWeight, HighMagnitude, Collection, AllWeights, WeightHistogram</td></tr>
* <tr><td> printClassifierParam</td><td>int</td><td>100</td><td>A parameter
* to the printing style, which may give, for example the number of parameters
* to print</td></tr>
* <tr><td> intern</td><td>boolean</td><td>false</td><td>If true,
* (String) intern read in data and classes and feature (pre-)names such
* as substring features</td></tr>
* <tr><td> intern2</td><td>boolean</td><td>false</td><td>If true, intern all (final) feature names (if only current word and ngram features are used, these will already have been interned by intern, and this is an unnecessary no-op)</td></tr>
* <tr><td> cacheNGrams</td><td>boolean</td><td>false</td><td>If true,
* record the NGram features that correspond to a String (under the current
* option settings) and reuse rather than recalculating if the String is seen
* again.</td></tr>
* <tr><td> selfTest</td><td>boolean</td><td>false</td><td></td></tr>
* <p/>
* <tr><td> noMidNGrams</td><td>boolean</td><td>false</td><td>Do not include character n-gram features for n-grams that contain neither the beginning or end of the word</td></tr>
* <tr><td> maxNGramLeng</td><td>int</td><td>-1</td><td>If this number is
* positive, n-grams above this size will not be used in the model</td></tr>
* <tr><td> useReverse</td><td>boolean</td><td>false</td><td></td></tr>
* <tr><td> retainEntitySubclassification</td><td>boolean</td><td>false</td><td>If true, rather than undoing a recoding of entity tag subtypes (such as BIO variants), just leave them in the output.</td></tr>
* <tr><td> useLemmas</td><td>boolean</td><td>false</td><td>Include the lemma of a word as a feature.</td></tr>
* <tr><td> usePrevNextLemmas</td><td>boolean</td><td>false</td><td>Include the previous/next lemma of a word as a feature.</td></tr>
* <tr><td> useLemmaAsWord</td><td>boolean</td><td>false</td><td>Include the lemma of a word as a feature.</td></tr>
* <tr><td> normalizeTerms</td><td>boolean</td><td>false</td><td>If this is true, some words are normalized: day and month names are lowercased (as for normalizeTimex) and some British spellings are mapped to American English spellings (e.g., -our/-or, etc.).</td></tr>
* <tr><td> normalizeTimex</td><td>boolean</td><td>false</td><td>If this is true, capitalization of day and month names is normalized to lowercase</td></tr>
* <tr><td> useNB</td><td>boolean</td><td>false</td><td></td></tr>
* <tr><td> useTypeSeqs</td><td>boolean</td><td>false</td><td>Use basic zeroeth order word shape features.</td></tr>
* <tr><td> useTypeSeqs2</td><td>boolean</td><td>false</td><td>Add additional first and second order word shape features</td></tr>
* <tr><td> useTypeSeqs3</td><td>boolean</td><td>false</td><td>Adds one more first order shape sequence</td></tr>
* <tr><td> useDisjunctive</td><td>boolean</td><td>false</td><td>Include in features giving disjunctions of words anywhere in the left or right disjunctionWidth words (preserving direction but not position)</td></tr>
* <tr><td> disjunctionWidth</td><td>int</td><td>4</td><td>The number of words on each side of the current word that are included in the disjunction features</td></tr>
* <tr><td> useDisjunctiveShapeInteraction</td><td>boolean</td><td>false</td><td>Include in features giving disjunctions of words anywhere in the left or right disjunctionWidth words (preserving direction but not position) interacting with the word shape of the current word</td></tr>
* <tr><td> useWideDisjunctive</td><td>boolean</td><td>false</td><td>Include in features giving disjunctions of words anywhere in the left or right wideDisjunctionWidth words (preserving direction but not position)</td></tr>
* <tr><td> wideDisjunctionWidth</td><td>int</td><td>4</td><td>The number of words on each side of the current word that are included in the disjunction features</td></tr>
* <tr><td> usePosition</td><td>boolean</td><td>false</td><td>Use combination of position in sentence and class as a feature</td></tr>
* <tr><td> useBeginSent</td><td>boolean</td><td>false</td><td>Use combination of initial position in sentence and class (and word shape) as a feature. (Doesn't seem to help.)</td></tr>
* <tr><td> useDisjShape</td><td>boolean</td><td>false</td><td>Include features giving disjunctions of word shapes anywhere in the left or right disjunctionWidth words (preserving direction but not position)</td></tr>
* <tr><td> useClassFeature</td><td>boolean</td><td>false</td><td>Include a feature for the class (as a class marginal). Puts a prior on the classes which is equivalent to how often the feature appeared in the training data.</td></tr>
* <tr><td> useShapeConjunctions</td><td>boolean</td><td>false</td><td>Conjoin shape with tag or position</td></tr>
* <tr><td> useWordTag</td><td>boolean</td><td>false</td><td>Include word and tag pair features</td></tr>
* <tr><td> useLastRealWord</td><td>boolean</td><td>false</td><td>Iff the prev word is of length 3 or less, add an extra feature that combines the word two back and the current word's shape. <i>Weird!</i></td></tr>
* <tr><td> useNextRealWord</td><td>boolean</td><td>false</td><td>Iff the next word is of length 3 or less, add an extra feature that combines the word after next and the current word's shape. <i>Weird!</i></td></tr>
* <tr><td> useTitle</td><td>boolean</td><td>false</td><td>Match a word against a list of name titles (Mr, Mrs, etc.)</td></tr>
* <tr><td> useOccurrencePatterns</td><td>boolean</td><td>false</td><td>This is a very engineered feature designed to capture multiple references to names. If the current word isn't capitalized, followed by a non-capitalized word, and preceded by a word with alphabetic characters, it returns NO-OCCURRENCE-PATTERN. Otherwise, if the previous word is a capitalized NNP, then if in the next 150 words you find this PW-W sequence, you get XY-NEXT-OCCURRENCE-XY, else if you find W you get XY-NEXT-OCCURRENCE-Y. Similarly for backwards and XY-PREV-OCCURRENCE-XY and XY-PREV-OCCURRENCE-Y. Else (if the previous word isn't a capitalized NNP), under analogous rules you get one or more of X-NEXT-OCCURRENCE-YX, X-NEXT-OCCURRENCE-XY, X-NEXT-OCCURRENCE-X, X-PREV-OCCURRENCE-YX, X-PREV-OCCURRENCE-XY, X-PREV-OCCURRENCE-X.</td></tr>
* <tr><td> useTypeySequences</td><td>boolean</td><td>false</td><td>Some first order word shape patterns.</td></tr>
* <tr><td> useGenericFeatures</td><td>boolean</td><td>false</td><td>If true, any features you include in the map will be incorporated into the model with values equal to those given in the file; values are treated as strings unless you use the "realValued" option (described below)</td></tr>
* <tr><td> justify</td><td>boolean</td><td>false</td><td>Print out all
* feature/class pairs and their weight, and then for each input data
* point, print justification (weights) for active features</td></tr>
* <tr><td> normalize</td><td>boolean</td><td>false</td><td>For the CMMClassifier (only) if this is true then the Scorer normalizes scores as probabilities.</td></tr>
* <tr><td> useHuber</td><td>boolean</td><td>false</td><td>Use a Huber loss prior rather than the default quadratic loss.</td></tr>
* <tr><td> useQuartic</td><td>boolean</td><td>false</td><td>Use a Quartic prior rather than the default quadratic loss.</td></tr>
* <tr><td> sigma</td><td>double</td><td>1.0</td><td></td></tr>
* <tr><td> epsilon</td><td>double</td><td>0.01</td><td>Used only as a parameter in the Huber loss: this is the distance from 0 at which the loss changes from quadratic to linear</td></tr>
* <tr><td> beamSize</td><td>int</td><td>30</td><td></td></tr>
* <tr><td> maxLeft</td><td>int</td><td>2</td><td>The number of things to the left that have to be cached to run the Viterbi algorithm: the maximum context of class features used.</td></tr>
* <tr><td> maxRight</td><td>int</td><td>2</td><td>The number of things to the right that have to be cached to run the Viterbi algorithm: the maximum context of class features used. The maximum possible clique size to use is (maxLeft + maxRight + 1)</td></tr>
* <tr><td> dontExtendTaggy</td><td>boolean</td><td>false</td><td>Don't extend the range of useTaggySequences when maxLeft is increased.</td></tr>
* <tr><td> numFolds </td><td>int</td><td>1</td><td>The number of folds to use for cross-validation. CURRENTLY NOT IMPLEMENTED.</td></tr>
* <tr><td> startFold </td><td>int</td><td>1</td><td>The starting fold to run. CURRENTLY NOT IMPLEMENTED.</td></tr>
* <tr><td> endFold </td><td>int</td><td>1</td><td>The last fold to run. CURRENTLY NOT IMPLEMENTED.</td></tr>
* <tr><td> mergeTags </td><td>boolean</td><td>false</td><td>Whether to merge B- and I- tags.</td></tr>
* <tr><td> splitDocuments</td><td>boolean</td><td>true</td><td>Whether or not to split the data into separate documents for training/testing</td></tr>
* <tr><td> maxDocSize</td><td>int</td><td>10000</td><td>If this number is greater than 0, attempt to split documents bigger than this value into multiple documents at sentence boundaries during testing; otherwise do nothing.</td></tr>
* </table>
* <p/>
* Note: flags/properties overwrite left to right. That is, the parameter
* setting specified <i>last</i> is the one used.
* <p/>
* <pre>
* DOCUMENTATION ON FEATURE TEMPLATES
* <p/>
* w = word
* t = tag
* p = position (word index in sentence)
* c = class
* p = paren
* g = gazette
* a = abbrev
* s = shape
* r = regent (dependency governor)
* h = head word of phrase
* n(w) = ngrams from w
* g(w) = gazette entries containing w
* l(w) = length of w
* o(...) = occurrence patterns of words
* <p/>
* useReverse reverses meaning of prev, next everywhere below (on in macro)
* <p/>
* "Prolog" booleans: , = AND and ; = OR
* <p/>
* Mac: Y = turned on in -macro,
* + = additional positive things relative to -macro for CoNLL NERFeatureFactory
* (perhaps none...)
* - = Known negative for CoNLL NERFeatureFactory relative to -macro
* <p/>p
* Bio: + = additional things that are positive for BioCreative
* - = things negative relative to -macro
* <p/>
* HighMagnitude: There are no (0) to a few (+) to many (+++) high weight
* features of this template. (? = not used in goodCoNLL, but usually = 0)
* <p/>
* Feature Mac Bio CRFFlags HighMagnitude
* ---------------------------------------------------------------------
* w,c Y useWord 0 (useWord is almost useless with unlimited ngram features, but helps a fraction in goodCoNLL, if only because of prior fiddling
* p,c usePosition ?
* p=0,c useBeginSent ?
* p=0,s,c useBeginSent ?
* t,c Y useTags ++
* pw,c Y usePrev +
* pt,c Y usePrev,useTags 0
* nw,c Y useNext ++
* nt,c Y useNext,useTags 0
* pw,w,c Y useWordPairs +
* w,nw,c Y useWordPairs +
* pt,t,nt,c useSymTags ?
* t,nt,c useSymTags ?
* pt,t,c useSymTags ?
* pw,nw,c useSymWordPairs ?
* <p/>
* pc,c Y usePrev,useSequences,usePrevSequences +++
* pc,w,c Y usePrev,useSequences,usePrevSequences 0
* nc,c useNext,useSequences,useNextSequences ?
* w,nc,c useNext,useSequences,useNextSequences ?
* pc,nc,c useNext,usePrev,useSequences,usePrevSequences,useNextSequences ?
* w,pc,nc,c useNext,usePrev,useSequences,usePrevSequences,useNextSequences ?
* <p/>
* (pw;p2w;p3w;p4w),c + useDisjunctive (out to disjunctionWidth now) +++
* (nw;n2w;n3w;n4w),c + useDisjunctive (out to disjunctionWidth now) ++++
* (pw;p2w;p3w;p4w),s,c + useDisjunctiveShapeInteraction ?
* (nw;n2w;n3w;n4w),s,c + useDisjunctiveShapeInteraction ?
* (pw;p2w;p3w;p4w),c + useWideDisjunctive (to wideDisjunctionWidth) ?
* (nw;n2w;n3w;n4w),c + useWideDisjunctive (to wideDisjunctionWidth) ?
* (ps;p2s;p3s;p4s),c useDisjShape (out to disjunctionWidth now) ?
* (ns;n2s;n3s;n4s),c useDisjShape (out to disjunctionWidth now) ?
* <p/>
* pt,pc,t,c Y useTaggySequences +
* p2t,p2c,pt,pc,t,c Y useTaggySequences,maxLeft>=2 +
* p3t,p3c,p2t,p2c,pt,pc,t,c Y useTaggySequences,maxLeft>=3,!dontExtendTaggy ?
* p2c,pc,c Y useLongSequences ++
* p3c,p2c,pc,c Y useLongSequences,maxLeft>=3 ?
* p4c,p3c,p2c,pc,c Y useLongSequences,maxLeft>=4 ?
* p2c,pc,c,pw=BOUNDARY useBoundarySequences 0 (OK, but!)
* <p/>
* p2t,pt,t,c - useExtraTaggySequences ?
* p3t,p2t,pt,t,c - useExtraTaggySequences ?
* <p/>
* p2t,pt,t,s,p2c,pc,c - useTaggySequencesShapeInteraction ?
* p3t,p2t,pt,t,s,p3c,p2c,pc,c useTaggySequencesShapeInteraction ?
* <p/>
* s,pc,c Y useTypeySequences ++
* ns,pc,c Y useTypeySequences // error for ps? not? 0
* ps,pc,s,c Y useTypeySequences 0
* // p2s,p2c,ps,pc,s,c Y useTypeySequences,maxLeft>=2 // duplicated a useTypeSeqs2 feature
* <p/>
* n(w),c Y useNGrams (noMidNGrams, MaxNGramLeng, lowercaseNGrams, dehyphenateNGrams) +++
* n(w),s,c useNGrams,conjoinShapeNGrams ?
* <p/>
* g,c + useGazFeatures // test refining this? ?
* pg,pc,c + useGazFeatures ?
* ng,c + useGazFeatures ?
* // pg,g,c useGazFeatures ?
* // pg,g,ng,c useGazFeatures ?
* // p2g,p2c,pg,pc,g,c useGazFeatures ?
* g,w,c useMoreGazFeatures ?
* pg,pc,g,c useMoreGazFeatures ?
* g,ng,c useMoreGazFeatures ?
* <p/>
* g(w),c useGazette,sloppyGazette (contains same word) ?
* g(w),[pw,nw,...],c useGazette,cleanGazette (entire entry matches) ?
* <p/>
* s,c Y wordShape >= 0 +++
* ps,c Y wordShape >= 0,useTypeSeqs +
* ns,c Y wordShape >= 0,useTypeSeqs +
* pw,s,c Y wordShape >= 0,useTypeSeqs +
* s,nw,c Y wordShape >= 0,useTypeSeqs +
* ps,s,c Y wordShape >= 0,useTypeSeqs 0
* s,ns,c Y wordShape >= 0,useTypeSeqs ++
* ps,s,ns,c Y wordShape >= 0,useTypeSeqs ++
* pc,ps,s,c Y wordShape >= 0,useTypeSeqs,useTypeSeqs2 0
* p2c,p2s,pc,ps,s,c Y wordShape >= 0,useTypeSeqs,useTypeSeqs2,maxLeft>=2 +++
* pc,ps,s,ns,c wordShape >= 0,useTypeSeqs,useTypeSeqs3 ?
* <p/>
* p2w,s,c if l(pw) <= 3 Y useLastRealWord // weird features, but work 0
* n2w,s,c if l(nw) <= 3 Y useNextRealWord ++
* o(pw,w,nw),c Y useOccurrencePatterns // don't fully grok but has to do with capitalized name patterns ++
* <p/>
* a,c useAbbr;useMinimalAbbr
* pa,a,c useAbbr
* a,na,c useAbbr
* pa,a,na,c useAbbr
* pa,pc,a,c useAbbr;useMinimalAbbr
* p2a,p2c,pa,pc,a useAbbr
* w,a,c useMinimalAbbr
* p2a,p2c,a,c useMinimalAbbr
* <p/>
* RESTR. w,(pw,pc;p2w,p2c;p3w,p3c;p4w,p4c) + useParenMatching,maxLeft>=n
* <p/>
* c - useClassFeature
* <p/>
* p,s,c - useShapeConjunctions
* t,s,c - useShapeConjunctions
* <p/>
* w,t,c + useWordTag ?
* w,pt,c + useWordTag ?
* w,nt,c + useWordTag ?
* <p/>
* r,c useNPGovernor (only for baseNP words)
* r,t,c useNPGovernor (only for baseNP words)
* h,c useNPHead (only for baseNP words)
* h,t,c useNPHead (only for baseNP words)
* <p/>
* </pre>
*
* @author Dan Klein
* @author Jenny Finkel
* @author Christopher Manning
* @author Shipra Dingare
* @author Huy Nguyen
* @author Mengqiu Wang
*/
public class NERFeatureFactory<IN extends CoreLabel> extends FeatureFactory<IN> {
private static final long serialVersionUID = -2329726064739185544L;
public NERFeatureFactory() {
super();
}
@Override
public void init(SeqClassifierFlags flags) {
super.init(flags);
initGazette();
if (flags.useDistSim) {
initLexicon(flags);
}
}
/**
* Extracts all the features from the input data at a certain index.
*
* @param cInfo The complete data set as a List of WordInfo
* @param loc The index at which to extract features.
*/
@Override
public Collection<String> getCliqueFeatures(PaddedList<IN> cInfo, int loc, Clique clique) {
Collection<String> features = Generics.newHashSet();
String domain = cInfo.get(0).get(CoreAnnotations.DomainAnnotation.class);
final boolean doFE = domain != null;
// System.err.println(doFE+"\t"+domain);
// there are two special cases below, because 2 cliques have 2 names
Collection<String> c;
String suffix;
if (clique == cliqueC) {
//200710: tried making this clique null; didn't improve performance (rafferty)
c = featuresC(cInfo, loc);
suffix = "C";
} else if (clique == cliqueCpC) {
c = featuresCpC(cInfo, loc);
suffix = "CpC";
addAllInterningAndSuffixing(features, c, suffix);
if (doFE) {
addAllInterningAndSuffixing(features, c, domain + '-' + suffix);
}
c = featuresCnC(cInfo, loc-1);
suffix = "CnC";
} else if (clique == cliqueCp2C) {
c = featuresCp2C(cInfo, loc);
suffix = "Cp2C";
} else if (clique == cliqueCp3C) {
c = featuresCp3C(cInfo, loc);
suffix = "Cp3C";
} else if (clique == cliqueCp4C) {
c = featuresCp4C(cInfo, loc);
suffix = "Cp4C";
} else if (clique == cliqueCp5C) {
c = featuresCp5C(cInfo, loc);
suffix = "Cp5C";
} else if (clique == cliqueCpCp2C) {
c = featuresCpCp2C(cInfo, loc);
suffix = "CpCp2C";
addAllInterningAndSuffixing(features, c, suffix);
if (doFE) {
addAllInterningAndSuffixing(features, c, domain+ '-' + suffix);
}
c = featuresCpCnC(cInfo, loc-1);
suffix = "CpCnC";
} else if (clique == cliqueCpCp2Cp3C) {
c = featuresCpCp2Cp3C(cInfo, loc);
suffix = "CpCp2Cp3C";
} else if (clique == cliqueCpCp2Cp3Cp4C) {
c = featuresCpCp2Cp3Cp4C(cInfo, loc);
suffix = "CpCp2Cp3Cp4C";
} else {
throw new IllegalArgumentException("Unknown clique: " + clique);
}
addAllInterningAndSuffixing(features, c, suffix);
if (doFE) {
addAllInterningAndSuffixing(features, c, domain + '-' + suffix);
}
// System.err.println(StringUtils.join(features,"\n")+"\n");
return features;
}
// TODO: when breaking serialization, it seems like it would be better to
// move the lexicon into (Abstract)SequenceClassifier and to do this
// annotation as part of the ObjectBankWrapper. But note that it is
// serialized in this object currently and it would then need to be
// serialized elsewhere or loaded each time
private Map<String,String> lexicon;
private void initLexicon(SeqClassifierFlags flags) {
if (flags.distSimLexicon == null) {
return;
}
if (lexicon != null) {
return;
}
Timing.startDoing("Loading distsim lexicon from " + flags.distSimLexicon);
lexicon = Generics.newHashMap();
boolean terryKoo = "terryKoo".equals(flags.distSimFileFormat);
for (String line : ObjectBank.getLineIterator(flags.distSimLexicon,
flags.inputEncoding)) {
String word;
String wordClass;
if (terryKoo) {
String[] bits = line.split("\\t");
word = bits[1];
wordClass = bits[0];
if (flags.distSimMaxBits > 0 && wordClass.length() > flags.distSimMaxBits) {
wordClass = wordClass.substring(0, flags.distSimMaxBits);
}
} else {
// "alexClark"
String[] bits = line.split("\\s+");
word = bits[0];
wordClass = bits[1];
}
if ( ! flags.casedDistSim) {
word = word.toLowerCase();
}
if (flags.numberEquivalenceDistSim) {
word = WordShapeClassifier.wordShape(word, WordShapeClassifier.WORDSHAPEDIGITS);
}
lexicon.put(word, wordClass);
}
Timing.endDoing();
}
private void distSimAnnotate(PaddedList<IN> info) {
for (CoreLabel fl : info) {
if (fl.has(CoreAnnotations.DistSimAnnotation.class)) { return; }
String word = getWord(fl);
if ( ! flags.casedDistSim) {
word = word.toLowerCase();
}
if (flags.numberEquivalenceDistSim) {
word = WordShapeClassifier.wordShape(word, WordShapeClassifier.WORDSHAPEDIGITS);
}
String distSim = lexicon.get(word);
if (distSim == null) {
distSim = flags.unknownWordDistSimClass;
}
fl.set(CoreAnnotations.DistSimAnnotation.class, distSim);
}
}
private Map<String,Collection<String>> wordToSubstrings = Generics.newHashMap();
public void clearMemory() {
wordToSubstrings = Generics.newHashMap();
lexicon = null;
}
private static String dehyphenate(String str) {
// don't take out leading or ending ones, just internal
// and remember padded with < > characters
String retStr = str;
int leng = str.length();
int hyphen = 2;
do {
hyphen = retStr.indexOf('-', hyphen);
if (hyphen >= 0 && hyphen < leng - 2) {
retStr = retStr.substring(0, hyphen) + retStr.substring(hyphen + 1);
} else {
hyphen = -1;
}
} while (hyphen >= 0);
return retStr;
}
private static String greekify(String str) {
// don't take out leading or ending ones, just internal
// and remember padded with < > characters
String pattern = "(alpha)|(beta)|(gamma)|(delta)|(epsilon)|(zeta)|(kappa)|(lambda)|(rho)|(sigma)|(tau)|(upsilon)|(omega)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
return m.replaceAll("~");
}
/* end methods that do transformations */
/*
* static booleans that check strings for certain qualities *
*/
// cdm: this could be improved to handle more name types, such as
// O'Reilly, DeGuzman, etc. (need a little classifier?!?)
private static boolean isNameCase(String str) {
if (str.length() < 2) {
return false;
}
if (!(Character.isUpperCase(str.charAt(0)) || Character.isTitleCase(str.charAt(0)))) {
return false;
}
for (int i = 1; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
return false;
}
}
return true;
}
private static boolean noUpperCase(String str) {
if (str.length() < 1) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isUpperCase(str.charAt(i))) {
return false;
}
}
return true;
}
private static boolean hasLetter(String str) {
if (str.length() < 1) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) {
return true;
}
}
return false;
}
private static final Pattern ordinalPattern = Pattern.compile("(?:(?:first|second|third|fourth|fifth|"+
"sixth|seventh|eighth|ninth|tenth|"+
"eleventh|twelfth|thirteenth|"+
"fourteenth|fifteenth|sixteenth|"+
"seventeenth|eighteenth|nineteenth|"+
"twenty|twentieth|thirty|thirtieth|"+
"forty|fortieth|fifty|fiftieth|"+
"sixty|sixtieth|seventy|seventieth|"+
"eighty|eightieth|ninety|ninetieth|"+
"one|two|three|four|five|six|seven|"+
"eight|nine|hundred|hundredth)-?)+|[0-9]+(?:st|nd|rd|th)", Pattern.CASE_INSENSITIVE);
private static final Pattern numberPattern = Pattern.compile("[0-9]+");
private static final Pattern ordinalEndPattern = Pattern.compile("(?:st|nd|rd|th)", Pattern.CASE_INSENSITIVE);
private boolean isOrdinal(List<? extends CoreLabel> wordInfos, int pos) {
CoreLabel c = wordInfos.get(pos);
String cWord = getWord(c);
Matcher m = ordinalPattern.matcher(cWord);
if (m.matches()) { return true; }
m = numberPattern.matcher(cWord);
if (m.matches()) {
if (pos+1 < wordInfos.size()) {
CoreLabel n = wordInfos.get(pos+1);
m = ordinalEndPattern.matcher(getWord(n));
if (m.matches()) { return true; }
}
return false;
}
m = ordinalEndPattern.matcher(cWord);
if (m.matches()) {
if (pos > 0) {
CoreLabel p = wordInfos.get(pos-1);
m = numberPattern.matcher(getWord(p));
if (m.matches()) { return true; }
}
}
if (cWord.equals("-")) {
if (pos+1 < wordInfos.size() && pos > 0) {
CoreLabel p = wordInfos.get(pos-1);
CoreLabel n = wordInfos.get(pos+1);
m = ordinalPattern.matcher(getWord(p));
if (m.matches()) {
m = ordinalPattern.matcher(getWord(n));
if (m.matches()) {
return true;
}
}
}
}
return false;
}
/* end static booleans that check strings for certain qualities */
/**
* Gazette Stuff.
*/
private static class GazetteInfo implements Serializable {
final String feature;
final int loc;
final String[] words;
private static final long serialVersionUID = -5903728481621584810L;
public GazetteInfo(String feature, int loc, String[] words) {
this.feature = feature;
this.loc = loc;
this.words = words;
}
} // end class GazetteInfo
private Map<String,Collection<String>> wordToGazetteEntries = Generics.newHashMap();
private Map<String,Collection<GazetteInfo>> wordToGazetteInfos = Generics.newHashMap();
/** Reads a gazette file. Each line of it consists of a class name
* (a String not containing whitespace characters), followed by whitespace
* characters followed by a phrase, which is one or more tokens separated
* by a single space.
*
* @param in Where to read the gazette from
* @throws IOException If IO errors
*/
private void readGazette(BufferedReader in) throws IOException {
Pattern p = Pattern.compile("^(\\S+)\\s+(.+)$");
for (String line; (line = in.readLine()) != null; ) {
Matcher m = p.matcher(line);
if (m.matches()) {
String type = intern(m.group(1));
String phrase = m.group(2);
String[] words = phrase.split(" ");
for (int i = 0; i < words.length; i++) {
String word = intern(words[i]);
if (flags.sloppyGazette) {
Collection<String> entries = wordToGazetteEntries.get(word);
if (entries == null) {
entries = Generics.newHashSet();
wordToGazetteEntries.put(word, entries);
}
String feature = intern(type + "-GAZ" + words.length);
entries.add(feature);
feature = intern(type + "-GAZ");
entries.add(feature);
}
if (flags.cleanGazette) {
Collection<GazetteInfo> infos = wordToGazetteInfos.get(word);
if (infos == null) {
infos = Generics.newHashSet();
wordToGazetteInfos.put(word, infos);
}
GazetteInfo info = new GazetteInfo(intern(type + "-GAZ" + words.length), i, words);
infos.add(info);
info = new GazetteInfo(intern(type + "-GAZ"), i, words);
infos.add(info);
}
}
}
}
}
private Set<Class<? extends GenericAnnotation<?>>> genericAnnotationKeys; // = null; //cache which keys are generic annotations so we don't have to do too many instanceof checks
@SuppressWarnings({"unchecked", "SuspiciousMethodCalls"})
private void makeGenericKeyCache(CoreLabel c) {
genericAnnotationKeys = Generics.newHashSet();
for (Class<?> key : c.keySet()) {
if (CoreLabel.genericValues.containsKey(key)) {
Class<? extends GenericAnnotation<?>> genKey = (Class<? extends GenericAnnotation<?>>) key;
genericAnnotationKeys.add(genKey);
}
}
}
private Set<String> lastNames; // = null;
private Set<String> maleNames; // = null;
private Set<String> femaleNames; // = null;
private final Pattern titlePattern = Pattern.compile("(?:Mr|Ms|Mrs|Dr|Miss|Sen|Judge|Sir)\\.?"); // todo: should make static final and add more titles
protected Collection<String> featuresC(PaddedList<IN> cInfo, int loc) {
CoreLabel p3 = cInfo.get(loc - 3);
CoreLabel p2 = cInfo.get(loc - 2);
CoreLabel p = cInfo.get(loc - 1);
CoreLabel c = cInfo.get(loc);
CoreLabel n = cInfo.get(loc + 1);
CoreLabel n2 = cInfo.get(loc + 2);
String cWord = getWord(c);
String pWord = getWord(p);
String nWord = getWord(n);
String cShape = c.getString(CoreAnnotations.ShapeAnnotation.class);
String pShape = p.getString(CoreAnnotations.ShapeAnnotation.class);
String nShape = n.getString(CoreAnnotations.ShapeAnnotation.class);
Collection<String> featuresC = new ArrayList<String>();
if (flags.useDistSim) {
distSimAnnotate(cInfo);
}
if (flags.useBagOfWords) {
for (IN word : cInfo) {
featuresC.add(getWord(word) + "-BAGOFWORDS");
}
}
if (flags.useDistSim && flags.useMoreTags) {
featuresC.add(p.get(CoreAnnotations.DistSimAnnotation.class) + '-' + cWord + "-PDISTSIM-CWORD");
}
if (flags.useDistSim) {
featuresC.add(c.get(CoreAnnotations.DistSimAnnotation.class) + "-DISTSIM");
}
if (flags.useTitle) {
Matcher m = titlePattern.matcher(cWord);
if (m.matches()) {
featuresC.add("IS_TITLE");
}
}
if (flags.useInternal && flags.useExternal ) {
if (flags.useWord) {
featuresC.add(cWord + "-WORD");
}
if (flags.use2W) {
featuresC.add(getWord(p2) + "-P2W");
featuresC.add(getWord(n2) + "-N2W");
}
if (flags.useLC) {
featuresC.add(cWord.toLowerCase() + "-CL");
featuresC.add(pWord.toLowerCase() + "-PL");
featuresC.add(nWord.toLowerCase() + "-NL");
}
if (flags.useUnknown) { // for true casing
featuresC.add(c.get(CoreAnnotations.UnknownAnnotation.class)+"-UNKNOWN");
featuresC.add(p.get(CoreAnnotations.UnknownAnnotation.class)+"-PUNKNOWN");
featuresC.add(n.get(CoreAnnotations.UnknownAnnotation.class)+"-NUNKNOWN");
}
if (flags.useLemmas) {
String lem = c.getString(CoreAnnotations.LemmaAnnotation.class);
if (! "".equals(lem)) {
featuresC.add(lem + "-LEM");
}
}
if (flags.usePrevNextLemmas) {
String plem = p.getString(CoreAnnotations.LemmaAnnotation.class);
String nlem = n.getString(CoreAnnotations.LemmaAnnotation.class);
if (! "".equals(plem)) {
featuresC.add(plem + "-PLEM");
}
if (! "".equals(nlem)) {
featuresC.add(nlem + "-NLEM");
}
}
if (flags.checkNameList) {
try {
if (lastNames == null) {
lastNames = Generics.newHashSet();
for (String line : ObjectBank.getLineIterator(flags.lastNameList)) {
String[] cols = line.split("\\s+");
lastNames.add(cols[0]);
}
}
if (maleNames == null) {
maleNames = Generics.newHashSet();
for (String line : ObjectBank.getLineIterator(flags.maleNameList)) {
String[] cols = line.split("\\s+");
maleNames.add(cols[0]);
}
}
if (femaleNames == null) {
femaleNames = Generics.newHashSet();
for (String line : ObjectBank.getLineIterator(flags.femaleNameList)) {
String[] cols = line.split("\\s+");
femaleNames.add(cols[0]);
}
}
String name = cWord.toUpperCase();
if (lastNames.contains(name)) {
featuresC.add("LAST_NAME");
}
if (maleNames.contains(name)) {
featuresC.add("MALE_NAME");
}
if (femaleNames.contains(name)) {
featuresC.add("FEMALE_NAME");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (flags.binnedLengths != null) {
int len = cWord.length();
String featureName = null;
for (int i = 0; i <= flags.binnedLengths.length; i++) {
if (i == flags.binnedLengths.length) {
featureName = "Len-" + flags.binnedLengths[flags.binnedLengths.length - 1] + "-Inf";
} else if (len <= flags.binnedLengths[i]) {
featureName = "Len-" + ((i == 0) ? 1 : flags.binnedLengths[i - 1]) + '-' + flags.binnedLengths[i];
break;
}
}
featuresC.add(featureName);
}
if (flags.useABGENE) {
featuresC.add(c.get(CoreAnnotations.AbgeneAnnotation.class) + "-ABGENE");
featuresC.add(p.get(CoreAnnotations.AbgeneAnnotation.class) + "-PABGENE");
featuresC.add(n.get(CoreAnnotations.AbgeneAnnotation.class) + "-NABGENE");
}
if (flags.useABSTRFreqDict) {
featuresC.add(c.get(CoreAnnotations.AbstrAnnotation.class) + "-ABSTRACT" + c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
featuresC.add(c.get(CoreAnnotations.AbstrAnnotation.class) + "-ABSTRACT" + c.get(CoreAnnotations.DictAnnotation.class) + "-DICT" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
featuresC.add(c.get(CoreAnnotations.AbstrAnnotation.class) + "-ABSTRACT" + c.get(CoreAnnotations.DictAnnotation.class) + "-DICT" + c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
}
if (flags.useABSTR) {
featuresC.add(c.get(CoreAnnotations.AbstrAnnotation.class) + "-ABSTRACT");
featuresC.add(p.get(CoreAnnotations.AbstrAnnotation.class) + "-PABSTRACT");
featuresC.add(n.get(CoreAnnotations.AbstrAnnotation.class) + "-NABSTRACT");
}
if (flags.useGENIA) {
featuresC.add(c.get(CoreAnnotations.GeniaAnnotation.class) + "-GENIA");
featuresC.add(p.get(CoreAnnotations.GeniaAnnotation.class) + "-PGENIA");
featuresC.add(n.get(CoreAnnotations.GeniaAnnotation.class) + "-NGENIA");
}
if (flags.useWEBFreqDict) {
featuresC.add(c.get(CoreAnnotations.WebAnnotation.class) + "-WEB" + c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
featuresC.add(c.get(CoreAnnotations.WebAnnotation.class) + "-WEB" + c.get(CoreAnnotations.DictAnnotation.class) + "-DICT" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
featuresC.add(c.get(CoreAnnotations.WebAnnotation.class) + "-WEB" + c.get(CoreAnnotations.DictAnnotation.class) + "-DICT" + c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ" + c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
}
if (flags.useWEB) {
featuresC.add(c.get(CoreAnnotations.WebAnnotation.class) + "-WEB");
featuresC.add(p.get(CoreAnnotations.WebAnnotation.class) + "-PWEB");
featuresC.add(n.get(CoreAnnotations.WebAnnotation.class) + "-NWEB");
}
if (flags.useIsURL) {
featuresC.add(c.get(CoreAnnotations.IsURLAnnotation.class) + "-ISURL");
}
if (flags.useEntityRule) {
featuresC.add(c.get(CoreAnnotations.EntityRuleAnnotation.class)+"-ENTITYRULE");
}
if (flags.useEntityTypes) {
featuresC.add(c.get(CoreAnnotations.EntityTypeAnnotation.class) + "-ENTITYTYPE");
}
if (flags.useIsDateRange) {
featuresC.add(c.get(CoreAnnotations.IsDateRangeAnnotation.class) + "-ISDATERANGE");
}
if (flags.useABSTRFreq) {
featuresC.add(c.get(CoreAnnotations.AbstrAnnotation.class) + "-ABSTRACT" + c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ");
}
if (flags.useFREQ) {
featuresC.add(c.get(CoreAnnotations.FreqAnnotation.class) + "-FREQ");
}
if (flags.useMoreTags) {
featuresC.add(p.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + '-' + cWord + "-PTAG-CWORD");
}
if (flags.usePosition) {
featuresC.add(c.get(CoreAnnotations.PositionAnnotation.class) + "-POSITION");
}
if (flags.useBeginSent) {
String pos = c.get(CoreAnnotations.PositionAnnotation.class);
if ("0".equals(pos)) {
featuresC.add("BEGIN-SENT");
featuresC.add(cShape + "-BEGIN-SENT");
} else if (Integer.toString(cInfo.size() - 1).equals(pos)) {
featuresC.add("END-SENT");
featuresC.add(cShape + "-END-SENT");
} else {
featuresC.add("IN-SENT");
featuresC.add(cShape + "-IN-SENT");
}
}
if (flags.useTags) {
featuresC.add(c.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-TAG");
}
if (flags.useOrdinal) {
if (isOrdinal(cInfo, loc)) {
featuresC.add("C_ORDINAL");
if (isOrdinal(cInfo, loc-1)) {
//System.err.print(getWord(p) + " ");
featuresC.add("PC_ORDINAL");
}
//System.err.println(cWord);
}
if (isOrdinal(cInfo, loc-1)) {
featuresC.add("P_ORDINAL");
}
}
if (flags.usePrev) {
featuresC.add(pWord + "-PW");
if (flags.useTags) {
featuresC.add(p.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-PTAG");
}
if (flags.useDistSim) {
featuresC.add(p.get(CoreAnnotations.DistSimAnnotation.class) + "-PDISTSIM");
}
if (flags.useIsURL) {
featuresC.add(p.get(CoreAnnotations.IsURLAnnotation.class) + "-PISURL");
}
if (flags.useEntityTypes) {
featuresC.add(p.get(CoreAnnotations.EntityTypeAnnotation.class) + "-PENTITYTYPE");
}
}
if (flags.useNext) {
featuresC.add(nWord + "-NW");
if (flags.useTags) {
featuresC.add(n.getString(CoreAnnotations.PartOfSpeechAnnotation.class) + "-NTAG");
}
if (flags.useDistSim) {
featuresC.add(n.get(CoreAnnotations.DistSimAnnotation.class) + "-NDISTSIM");