16
16
import edu .stanford .nlp .trees .MemoryTreebank ;
17
17
import edu .stanford .nlp .trees .Tree ;
18
18
import edu .stanford .nlp .trees .TreeNormalizer ;
19
+ import edu .stanford .nlp .trees .ud .CoNLLUDocumentWriter ;
19
20
import edu .stanford .nlp .util .ArrayCoreMap ;
20
21
import edu .stanford .nlp .util .CoreMap ;
21
22
import edu .stanford .nlp .util .Generics ;
@@ -438,7 +439,8 @@ public int hashCode() {
438
439
439
440
public enum OutputFormat {
440
441
LIST ,
441
- OFFSET
442
+ OFFSET ,
443
+ CONLLU
442
444
}
443
445
444
446
@@ -548,6 +550,7 @@ public static void main(String[] args) throws IOException {
548
550
549
551
for (CoreMap sentence : sentences ) {
550
552
SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
553
+ SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
551
554
SemgrexMatcher matcher = semgrex .matcher (graph );
552
555
if ( ! matcher .find ()) {
553
556
continue ;
@@ -574,6 +577,30 @@ public static void main(String[] args) throws IOException {
574
577
}
575
578
System .out .printf ("+%d %s%n" , graph .vertexListSorted ().get (0 ).get (CoreAnnotations .LineNumberAnnotation .class ),
576
579
argsMap .get (CONLLU_FILE )[0 ]);
580
+ } else if (outputFormat == OutputFormat .CONLLU ) {
581
+ CoNLLUDocumentWriter writer = new CoNLLUDocumentWriter ();
582
+ String semgrexName = semgrex .toString ().trim ();
583
+ // TODO: comments should load from the CoNLLU document, if applicable
584
+ List <String > comments = new ArrayList <>(graph .getComments ());
585
+ boolean found = true ;
586
+ while (found ) {
587
+ StringBuilder comment = new StringBuilder ();
588
+ comment .append ("# semgrex pattern |" + semgrexName + "| matched at " + matcher .getMatch ().toString (CoreLabel .OutputFormat .VALUE_INDEX ));
589
+
590
+ List <String > nodeNames = new ArrayList <>();
591
+ nodeNames .addAll (matcher .getNodeNames ());
592
+ Collections .sort (nodeNames );
593
+ for (String name : nodeNames ) {
594
+ comment .append (" " );
595
+ comment .append (name );
596
+ comment .append (":" );
597
+ comment .append (matcher .getNode (name ).toString (CoreLabel .OutputFormat .VALUE_INDEX ));
598
+ }
599
+ comments .add (comment .toString ());
600
+ found = matcher .find ();
601
+ }
602
+ String output = writer .printSemanticGraph (graph , enhanced , false , comments );
603
+ System .out .print (output );
577
604
}
578
605
}
579
606
}
0 commit comments