2
2
3
3
import java .io .*;
4
4
import java .util .*;
5
+ import java .util .stream .Collectors ;
5
6
6
7
import edu .stanford .nlp .semgraph .SemanticGraph ;
8
+ import edu .stanford .nlp .semgraph .SemanticGraphCoreAnnotations ;
7
9
import edu .stanford .nlp .semgraph .SemanticGraphEdge ;
8
10
import edu .stanford .nlp .semgraph .SemanticGraphFactory ;
9
11
import edu .stanford .nlp .io .IOUtils ;
10
12
import edu .stanford .nlp .ling .*;
11
- import edu .stanford .nlp .trees .ud .CoNLLUDocumentReader ;
13
+ import edu .stanford .nlp .pipeline .Annotation ;
14
+ import edu .stanford .nlp .pipeline .CoNLLUReader ;
12
15
import edu .stanford .nlp .trees .GrammaticalStructure ;
13
16
import edu .stanford .nlp .trees .MemoryTreebank ;
14
17
import edu .stanford .nlp .trees .Tree ;
15
18
import edu .stanford .nlp .trees .TreeNormalizer ;
19
+ import edu .stanford .nlp .util .ArrayCoreMap ;
20
+ import edu .stanford .nlp .util .CoreMap ;
16
21
import edu .stanford .nlp .util .Generics ;
17
22
import edu .stanford .nlp .util .Pair ;
18
23
import edu .stanford .nlp .util .StringUtils ;
@@ -506,7 +511,7 @@ public static void main(String[] args) throws IOException {
506
511
useExtras = Boolean .parseBoolean (argsMap .get (EXTRAS )[0 ]);
507
512
}
508
513
509
- List <SemanticGraph > graphs = Generics . newArrayList ();
514
+ List <CoreMap > sentences = new ArrayList <> ();
510
515
// TODO: allow other sources of graphs, such as dependency files
511
516
if (argsMap .containsKey (TREE_FILE ) && argsMap .get (TREE_FILE ).length > 0 ) {
512
517
for (String treeFile : argsMap .get (TREE_FILE )) {
@@ -517,25 +522,32 @@ public static void main(String[] args) throws IOException {
517
522
// TODO: allow other languages... this defaults to English
518
523
SemanticGraph graph = SemanticGraphFactory .makeFromTree (tree , mode , useExtras ?
519
524
GrammaticalStructure .Extras .MAXIMAL : GrammaticalStructure .Extras .NONE );
520
- graphs .add (graph );
525
+ CoreMap sentence = new ArrayCoreMap ();
526
+ sentence .set (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class , graph );
527
+ List <CoreLabel > tokens = graph .vertexListSorted ().stream ().map (x -> x .backingLabel ()).collect (Collectors .toList ());
528
+ sentence .set (CoreAnnotations .TokensAnnotation .class , tokens );
529
+ sentences .add (sentence );
521
530
}
522
531
}
523
532
}
524
533
525
534
if (argsMap .containsKey (CONLLU_FILE ) && argsMap .get (CONLLU_FILE ).length > 0 ) {
526
- CoNLLUDocumentReader reader = new CoNLLUDocumentReader ();
527
- for ( String conlluFile : argsMap . get ( CONLLU_FILE )) {
528
- log . info ( "Loading file " + conlluFile );
529
- Iterator < Pair < SemanticGraph , SemanticGraph >> it = reader . getIterator ( IOUtils . readerFromString ( conlluFile ) );
530
-
531
- while ( it . hasNext () ) {
532
- SemanticGraph graph = it . next (). first ;
533
- graphs . add ( graph );
535
+ try {
536
+ CoNLLUReader reader = new CoNLLUReader ();
537
+ for ( String conlluFile : argsMap . get ( CONLLU_FILE )) {
538
+ log . info ( "Loading file " + conlluFile );
539
+ List < Annotation > docs = reader . readCoNLLUFile ( conlluFile );
540
+ for ( Annotation doc : docs ) {
541
+ sentences . addAll ( doc . get ( CoreAnnotations . SentencesAnnotation . class )) ;
542
+ }
534
543
}
544
+ } catch (ClassNotFoundException e ) {
545
+ throw new RuntimeException (e );
535
546
}
536
547
}
537
548
538
- for (SemanticGraph graph : graphs ) {
549
+ for (CoreMap sentence : sentences ) {
550
+ SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
539
551
SemgrexMatcher matcher = semgrex .matcher (graph );
540
552
if ( ! matcher .find ()) {
541
553
continue ;
0 commit comments