@@ -444,6 +444,29 @@ public enum OutputFormat {
444
444
CONLLU
445
445
}
446
446
447
+ /**
448
+ * Returns a list of matching sentences and each of the matches from those sentences.
449
+ *<br>
450
+ * Non-matching sentences are currently not returned (may change in the future to return an empty list).
451
+ */
452
+ public List <Pair <CoreMap , List <SemgrexMatch >>> matchSentences (List <CoreMap > sentences ) {
453
+ List <Pair <CoreMap , List <SemgrexMatch >>> matches = new ArrayList <>();
454
+ for (CoreMap sentence : sentences ) {
455
+ SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
456
+ SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
457
+ SemgrexMatcher matcher = matcher (graph );
458
+ if ( ! matcher .find ()) {
459
+ continue ;
460
+ }
461
+ matches .add (new Pair <>(sentence , new ArrayList <>()));
462
+ boolean found = true ;
463
+ while (found ) {
464
+ matches .get (matches .size () - 1 ).second ().add (new SemgrexMatch (this , matcher ));
465
+ found = matcher .find ();
466
+ }
467
+ }
468
+ return matches ;
469
+ }
447
470
448
471
private static final String PATTERN = "-pattern" ;
449
472
private static final String TREE_FILE = "-treeFile" ;
@@ -548,21 +571,7 @@ public static void main(String[] args) throws IOException {
548
571
}
549
572
}
550
573
551
- List <Pair <CoreMap , List <SemgrexMatch >>> matches = new ArrayList <>();
552
- for (CoreMap sentence : sentences ) {
553
- SemanticGraph graph = sentence .get (SemanticGraphCoreAnnotations .BasicDependenciesAnnotation .class );
554
- SemanticGraph enhanced = sentence .get (SemanticGraphCoreAnnotations .EnhancedDependenciesAnnotation .class );
555
- SemgrexMatcher matcher = semgrex .matcher (graph );
556
- if ( ! matcher .find ()) {
557
- continue ;
558
- }
559
- matches .add (new Pair <>(sentence , new ArrayList <>()));
560
- boolean found = true ;
561
- while (found ) {
562
- matches .get (matches .size () - 1 ).second ().add (new SemgrexMatch (semgrex , matcher ));
563
- found = matcher .find ();
564
- }
565
- }
574
+ List <Pair <CoreMap , List <SemgrexMatch >>> matches = semgrex .matchSentences (sentences );
566
575
567
576
for (Pair <CoreMap , List <SemgrexMatch >> sentenceMatches : matches ) {
568
577
CoreMap sentence = sentenceMatches .first ();
0 commit comments