Search in sources :

Example 51 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexPatternTest method testEnv.

public void testEnv() throws IOException {
    SemanticGraph h = SemanticGraph.valueOf("[married/VBN nsubjpass>Hughes/NNP auxpass>was/VBD nmod:to>Gracia/NNP]");
    h.getFirstRoot().set(PatternsAnnotations.PatternLabel1.class, "YES");
    //SemanticGraph t = SemanticGraph
    //  .valueOf("[loved/VBD\nnsubj:Hughes/NNP\ndobj:[wife/NN poss:his/PRP$ appos:Gracia/NNP]\nconj_and:[obsessed/JJ\ncop:was/VBD\nadvmod:absolutely/RB\nprep_with:[Elicia/NN poss:his/PRP$ amod:little/JJ nn:daughter/NN]]]");
    String macro = "macro WORD = married";
    Env env = new Env();
    env.bind("pattern1", PatternsAnnotations.PatternLabel1.class);
    String pattern = "({pattern1:YES}=parent >>nsubjpass {}=node)";
    List<SemgrexPattern> pats = SemgrexBatchParser.compileStream(new ByteArrayInputStream((macro + "\n" + pattern).getBytes(StandardCharsets.UTF_8)), env);
    SemgrexPattern pat3 = pats.get(0);
    boolean ignoreCase = true;
    SemgrexMatcher mat3 = pat3.matcher(h, ignoreCase);
    if (mat3.find()) {
        String parent = mat3.getNode("parent").word();
        String node = mat3.getNode("node").word();
        System.out.println("Result: parent is " + parent + " and node is " + node);
        Assert.assertEquals(parent, "married");
        Assert.assertEquals(node, "Hughes");
    } else
        throw new RuntimeException("failed!");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) PatternsAnnotations(edu.stanford.nlp.patterns.PatternsAnnotations)

Example 52 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexTest method testPartition.

public void testPartition() {
    SemanticGraph graph = makeComplicatedGraph();
    runTest("{}=a >> {word:E}", graph, "A", "B", "C", "D");
    runTest("{}=a >> {word:E} : {}=a >> {word:B}", graph, "A");
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 53 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexTest method testMatchAll.

public void testMatchAll() {
    SemanticGraph graph = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
    Set<IndexedWord> words = graph.vertexSet();
    SemgrexPattern pattern = SemgrexPattern.compile("{}");
    SemgrexMatcher matcher = pattern.matcher(graph);
    String[] expectedMatches = { "ate", "Bill", "muffins", "blueberry" };
    for (int i = 0; i < expectedMatches.length; ++i) {
        assertTrue(matcher.findNextMatchingNode());
    }
    assertFalse(matcher.findNextMatchingNode());
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 54 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexTest method testMultipleDepths.

/**
   * Tests that if there are different paths from A to I, those paths show up for exactly the right depths
   */
public void testMultipleDepths() {
    SemanticGraph graph = makeComplicatedGraph();
    runTest("{} 3,3<< {word:A}", graph, "F", "G", "I");
    runTest("{} 4,4<< {word:A}", graph, "H", "J");
    runTest("{} 5,5<< {word:A}", graph, "I");
    runTest("{} 6,6<< {word:A}", graph, "J");
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 55 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexTest method testInitialConditions.

public void testInitialConditions() {
    SemanticGraph graph = makeComplicatedGraph();
    SemgrexPattern pattern = SemgrexPattern.compile("{}=a >> {}=b : {}=a >> {}=c");
    Map<String, IndexedWord> variables = new HashMap<String, IndexedWord>();
    variables.put("b", graph.getNodeByIndex(5));
    variables.put("c", graph.getNodeByIndex(2));
    SemgrexMatcher matcher = pattern.matcher(graph, variables);
    assertTrue(matcher.find());
    assertEquals(3, matcher.getNodeNames().size());
    assertEquals("A", matcher.getNode("a").toString());
    assertEquals("E", matcher.getNode("b").toString());
    assertEquals("B", matcher.getNode("c").toString());
    assertEquals("A", matcher.getMatch().toString());
    assertFalse(matcher.find());
}
Also used : HashMap(java.util.HashMap) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Aggregations

SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)126 IndexedWord (edu.stanford.nlp.ling.IndexedWord)57 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)53 CoreLabel (edu.stanford.nlp.ling.CoreLabel)51 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)47 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)24 Tree (edu.stanford.nlp.trees.Tree)20 CoreMap (edu.stanford.nlp.util.CoreMap)19 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)18 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)16 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)16 Annotation (edu.stanford.nlp.pipeline.Annotation)14 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)12 ArrayList (java.util.ArrayList)12 Mention (edu.stanford.nlp.coref.data.Mention)11 java.util (java.util)11 edu.stanford.nlp.util (edu.stanford.nlp.util)10 Properties (java.util.Properties)9 Collectors (java.util.stream.Collectors)9 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)8