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!");
}
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");
}
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());
}
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");
}
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());
}
Aggregations