use of edu.stanford.nlp.semgraph.semgrex.SemgrexPattern in project CoreNLP by stanfordnlp.
the class SsurgeonTest method simpleTest.
/**
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
* a semgrex pattern, and then the resulting actions over the named nodes in the
* semgrex match.
*/
@Test
public void simpleTest() throws Exception {
SemanticGraph sg = SemanticGraph.valueOf("[mixed/VBN nsubj>[Joe/NNP appos>[bartender/NN det>the/DT]] obj>[drink/NN det>a/DT]]");
SemgrexPattern semgrexPattern = SemgrexPattern.compile("{}=a1 >appos=e1 {}=a2 <nsubj=e2 {}=a3");
SsurgeonPattern pattern = new SsurgeonPattern(semgrexPattern);
System.out.println("Start = " + sg.toCompactString());
// Find and snip the appos and root to nsubj links
SsurgeonEdit apposSnip = new RemoveNamedEdge("e1", "a1", "a2");
pattern.addEdit(apposSnip);
SsurgeonEdit nsubjSnip = new RemoveNamedEdge("e2", "a3", "a1");
pattern.addEdit(nsubjSnip);
// Attach Joe to be the nsubj of bartender
SsurgeonEdit reattachSubj = new AddEdge("a2", "a1", EnglishGrammaticalRelations.NOMINAL_SUBJECT);
pattern.addEdit(reattachSubj);
// Attach copula
IndexedWord isNode = new IndexedWord();
isNode.set(CoreAnnotations.TextAnnotation.class, "is");
isNode.set(CoreAnnotations.LemmaAnnotation.class, "is");
isNode.set(CoreAnnotations.OriginalTextAnnotation.class, "is");
isNode.set(CoreAnnotations.PartOfSpeechAnnotation.class, "VBN");
SsurgeonEdit addCopula = new AddDep("a2", EnglishGrammaticalRelations.COPULA, isNode);
pattern.addEdit(addCopula);
// Destroy subgraph
SsurgeonEdit destroySubgraph = new DeleteGraphFromNode("a3");
pattern.addEdit(destroySubgraph);
// Process and output modified
Collection<SemanticGraph> newSgs = pattern.execute(sg);
for (SemanticGraph newSg : newSgs) System.out.println("Modified = " + newSg.toCompactString());
String firstGraphString = newSgs.iterator().next().toCompactString().trim();
assertEquals(firstGraphString, "[bartender cop>is nsubj>Joe det>the]");
}
Aggregations