Skip to content

Commit 3010f9a

Browse files
committed
Refactor a method which processes a list of sentences all at once for a given Semgrex expression. Will make it easier to extend, such as with postprocessing steps
1 parent 3da7981 commit 3010f9a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,29 @@ public enum OutputFormat {
444444
CONLLU
445445
}
446446

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+
}
447470

448471
private static final String PATTERN = "-pattern";
449472
private static final String TREE_FILE = "-treeFile";
@@ -548,21 +571,7 @@ public static void main(String[] args) throws IOException {
548571
}
549572
}
550573

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);
566575

567576
for (Pair<CoreMap, List<SemgrexMatch>> sentenceMatches : matches) {
568577
CoreMap sentence = sentenceMatches.first();

0 commit comments

Comments
 (0)