Search in sources :

Example 21 with SemgrexPattern

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]");
}
Also used : SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) IndexedWord(edu.stanford.nlp.ling.IndexedWord) Test(org.junit.Test)

Aggregations

SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)21 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)12 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)12 IndexedWord (edu.stanford.nlp.ling.IndexedWord)11 CoreLabel (edu.stanford.nlp.ling.CoreLabel)6 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)5 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)3 TwoDimensionalCounter (edu.stanford.nlp.stats.TwoDimensionalCounter)3 Span (edu.stanford.nlp.ie.machinereading.structure.Span)2 TokenSequencePattern (edu.stanford.nlp.ling.tokensregex.TokenSequencePattern)2 CandidatePhrase (edu.stanford.nlp.patterns.CandidatePhrase)2 DataInstance (edu.stanford.nlp.patterns.DataInstance)2 Pattern (edu.stanford.nlp.patterns.Pattern)2 PatternsAnnotations (edu.stanford.nlp.patterns.PatternsAnnotations)2 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)2 CollectionValuedMap (edu.stanford.nlp.util.CollectionValuedMap)2 IntPair (edu.stanford.nlp.util.IntPair)2 Pair (edu.stanford.nlp.util.Pair)2 Triple (edu.stanford.nlp.util.Triple)2 ArrayList (java.util.ArrayList)2