@@ -549,27 +549,38 @@ public static void main(String[] args) throws IOException {
549
549
}
550
550
}
551
551
552
+ List <Pair <CoreMap , List <SemgrexMatch >>> matches = new ArrayList <>();
552
553
for (CoreMap sentence : sentences ) {
553
554
SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
554
555
SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
555
556
SemgrexMatcher matcher = semgrex .matcher (graph );
556
557
if ( ! matcher .find ()) {
557
558
continue ;
558
559
}
560
+ matches .add (new Pair <>(sentence , new ArrayList <>()));
561
+ boolean found = true ;
562
+ while (found ) {
563
+ matches .get (matches .size () - 1 ).second ().add (new SemgrexMatch (semgrex , matcher ));
564
+ found = matcher .find ();
565
+ }
566
+ }
559
567
568
+ for (Pair <CoreMap , List <SemgrexMatch >> sentenceMatches : matches ) {
569
+ CoreMap sentence = sentenceMatches .first ();
570
+ SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
571
+ SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
560
572
if (outputFormat == OutputFormat .LIST ) {
561
573
log .info ("Matched graph:" + System .lineSeparator () + graph .toString (SemanticGraph .OutputFormat .LIST ));
562
- int i = 1 ;
563
- boolean found = true ;
564
- while ( found ) {
574
+ int i = 0 ;
575
+ for ( SemgrexMatch matcher : sentenceMatches . second ()) {
576
+ i ++;
565
577
log .info ("Match " + i + " at: " + matcher .getMatch ().toString (CoreLabel .OutputFormat .VALUE_INDEX ));
566
578
List <String > nodeNames = Generics .newArrayList ();
567
579
nodeNames .addAll (matcher .getNodeNames ());
568
580
Collections .sort (nodeNames );
569
581
for (String name : nodeNames ) {
570
582
log .info (" " + name + ": " + matcher .getNode (name ).toString (CoreLabel .OutputFormat .VALUE_INDEX ));
571
583
}
572
- found = matcher .find ();
573
584
}
574
585
} else if (outputFormat == OutputFormat .OFFSET ) {
575
586
if (graph .vertexListSorted ().isEmpty ()) {
@@ -585,8 +596,7 @@ public static void main(String[] args) throws IOException {
585
596
if (comments .size () == 0 ) {
586
597
comments .addAll (graph .getComments ());
587
598
}
588
- boolean found = true ;
589
- while (found ) {
599
+ for (SemgrexMatch matcher : sentenceMatches .second ()) {
590
600
StringBuilder comment = new StringBuilder ();
591
601
comment .append ("# semgrex pattern |" + semgrexName + "| matched at " + matcher .getMatch ().toString (CoreLabel .OutputFormat .VALUE_INDEX ));
592
602
@@ -600,7 +610,6 @@ public static void main(String[] args) throws IOException {
600
610
comment .append (matcher .getNode (name ).toString (CoreLabel .OutputFormat .VALUE_INDEX ));
601
611
}
602
612
comments .add (comment .toString ());
603
- found = matcher .find ();
604
613
}
605
614
String output = writer .printSemanticGraph (graph , enhanced , false , comments );
606
615
System .out .print (output );
0 commit comments