@@ -549,27 +549,38 @@ public static void main(String[] args) throws IOException {
549549 }
550550 }
551551
552+ List <Pair <CoreMap , List <SemgrexMatch >>> matches = new ArrayList <>();
552553 for (CoreMap sentence : sentences ) {
553554 SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
554555 SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
555556 SemgrexMatcher matcher = semgrex .matcher (graph );
556557 if ( ! matcher .find ()) {
557558 continue ;
558559 }
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+ }
559567
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 );
560572 if (outputFormat == OutputFormat .LIST ) {
561573 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 ++;
565577 log .info ("Match " + i + " at: " + matcher .getMatch ().toString (CoreLabel .OutputFormat .VALUE_INDEX ));
566578 List <String > nodeNames = Generics .newArrayList ();
567579 nodeNames .addAll (matcher .getNodeNames ());
568580 Collections .sort (nodeNames );
569581 for (String name : nodeNames ) {
570582 log .info (" " + name + ": " + matcher .getNode (name ).toString (CoreLabel .OutputFormat .VALUE_INDEX ));
571583 }
572- found = matcher .find ();
573584 }
574585 } else if (outputFormat == OutputFormat .OFFSET ) {
575586 if (graph .vertexListSorted ().isEmpty ()) {
@@ -585,8 +596,7 @@ public static void main(String[] args) throws IOException {
585596 if (comments .size () == 0 ) {
586597 comments .addAll (graph .getComments ());
587598 }
588- boolean found = true ;
589- while (found ) {
599+ for (SemgrexMatch matcher : sentenceMatches .second ()) {
590600 StringBuilder comment = new StringBuilder ();
591601 comment .append ("# semgrex pattern |" + semgrexName + "| matched at " + matcher .getMatch ().toString (CoreLabel .OutputFormat .VALUE_INDEX ));
592602
@@ -600,7 +610,6 @@ public static void main(String[] args) throws IOException {
600610 comment .append (matcher .getNode (name ).toString (CoreLabel .OutputFormat .VALUE_INDEX ));
601611 }
602612 comments .add (comment .toString ());
603- found = matcher .find ();
604613 }
605614 String output = writer .printSemanticGraph (graph , enhanced , false , comments );
606615 System .out .print (output );
0 commit comments