use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexTest method testNamedNode.
public void testNamedNode() {
SemanticGraph graph = makeComplicatedGraph();
runTest("{} >dobj ({} >expl {})", graph, "A");
SemgrexPattern pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo)");
SemgrexMatcher matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals(1, matcher.getNodeNames().size());
assertEquals("E", matcher.getNode("foo").toString());
assertEquals("A", matcher.getMatch().toString());
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo) >mod {}");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals(1, matcher.getNodeNames().size());
assertEquals("E", matcher.getNode("foo").toString());
assertEquals("A", matcher.getMatch().toString());
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo) >mod ({} >mark {})");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals(1, matcher.getNodeNames().size());
assertEquals("E", matcher.getNode("foo").toString());
assertEquals("A", matcher.getMatch().toString());
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo) >mod ({} > {})");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals(1, matcher.getNodeNames().size());
assertEquals("E", matcher.getNode("foo").toString());
assertEquals("A", matcher.getMatch().toString());
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo) >mod ({} > {}=foo)");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals(1, matcher.getNodeNames().size());
assertEquals("E", matcher.getNode("foo").toString());
assertEquals("A", matcher.getMatch().toString());
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{} >dobj ({} >expl {}=foo) >mod ({}=foo > {})");
matcher = pattern.matcher(graph);
assertFalse(matcher.find());
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexTest method testIndex.
/**
* Test that a particular AnnotationLookup is honored
*/
public void testIndex() {
SemanticGraph graph = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
runTest("{idx:0}", graph, "ate");
runTest("{idx:1}", graph, "Bill");
runTest("{idx:2}", graph, "muffins");
runTest("{idx:3}", graph, "blueberry");
runTest("{idx:4}", graph);
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexTest method testNamedRelation.
public void testNamedRelation() {
SemanticGraph graph = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
SemgrexPattern pattern = SemgrexPattern.compile("{idx:0}=gov >>=foo {idx:3}=dep");
SemgrexMatcher matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals("ate", matcher.getNode("gov").toString());
assertEquals("blueberry", matcher.getNode("dep").toString());
assertEquals("compound", matcher.getRelnString("foo"));
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{idx:3}=dep <<=foo {idx:0}=gov");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals("ate", matcher.getNode("gov").toString());
assertEquals("blueberry", matcher.getNode("dep").toString());
assertEquals("dobj", matcher.getRelnString("foo"));
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{idx:3}=dep <=foo {idx:2}=gov");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals("muffins", matcher.getNode("gov").toString());
assertEquals("blueberry", matcher.getNode("dep").toString());
assertEquals("compound", matcher.getRelnString("foo"));
assertFalse(matcher.find());
pattern = SemgrexPattern.compile("{idx:2}=gov >=foo {idx:3}=dep");
matcher = pattern.matcher(graph);
assertTrue(matcher.find());
assertEquals("muffins", matcher.getNode("gov").toString());
assertEquals("blueberry", matcher.getNode("dep").toString());
assertEquals("compound", matcher.getRelnString("foo"));
assertFalse(matcher.find());
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexTest method makeComplicatedGraph.
public static SemanticGraph makeComplicatedGraph() {
SemanticGraph graph = new SemanticGraph();
String[] words = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
IndexedWord[] nodes = new IndexedWord[words.length];
for (int i = 0; i < words.length; ++i) {
IndexedWord word = new IndexedWord("test", 1, i + 1);
word.setWord(words[i]);
word.setValue(words[i]);
nodes[i] = word;
graph.addVertex(word);
}
graph.setRoot(nodes[0]);
// this graph isn't supposed to make sense
graph.addEdge(nodes[0], nodes[1], EnglishGrammaticalRelations.MODIFIER, 1.0, false);
graph.addEdge(nodes[0], nodes[2], EnglishGrammaticalRelations.DIRECT_OBJECT, 1.0, false);
graph.addEdge(nodes[0], nodes[3], EnglishGrammaticalRelations.INDIRECT_OBJECT, 1.0, false);
graph.addEdge(nodes[1], nodes[4], EnglishGrammaticalRelations.MARKER, 1.0, false);
graph.addEdge(nodes[2], nodes[4], EnglishGrammaticalRelations.EXPLETIVE, 1.0, false);
graph.addEdge(nodes[3], nodes[4], EnglishGrammaticalRelations.ADJECTIVAL_COMPLEMENT, 1.0, false);
graph.addEdge(nodes[4], nodes[5], EnglishGrammaticalRelations.ADJECTIVAL_MODIFIER, 1.0, false);
graph.addEdge(nodes[4], nodes[6], EnglishGrammaticalRelations.ADVERBIAL_MODIFIER, 1.0, false);
graph.addEdge(nodes[4], nodes[8], EnglishGrammaticalRelations.MODIFIER, 1.0, false);
graph.addEdge(nodes[5], nodes[7], EnglishGrammaticalRelations.POSSESSION_MODIFIER, 1.0, false);
graph.addEdge(nodes[6], nodes[7], EnglishGrammaticalRelations.POSSESSIVE_MODIFIER, 1.0, false);
graph.addEdge(nodes[7], nodes[8], EnglishGrammaticalRelations.AGENT, 1.0, false);
graph.addEdge(nodes[8], nodes[9], EnglishGrammaticalRelations.DETERMINER, 1.0, false);
return graph;
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class SemgrexTest method testLemma.
public void testLemma() {
SemanticGraph graph = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
for (IndexedWord word : graph.vertexSet()) {
word.setLemma(word.word());
}
runTest("{lemma:ate}", graph, "ate");
Tree tree = Tree.valueOf("(ROOT (S (NP (PRP I)) (VP (VBP love) (NP (DT the) (NN display))) (. .)))");
graph = SemanticGraphFactory.generateCCProcessedDependencies(tree);
for (IndexedWord word : graph.vertexSet()) {
word.setLemma(word.word());
}
// This set of three tests also provides some coverage for a
// bizarre error a user found where multiple copies of the same
// IndexedWord were created
runTest("{}=Obj <dobj {lemma:love}=Pred", graph, "display/NN");
runTest("{}=Obj <dobj {}=Pred", graph, "display/NN");
runTest("{lemma:love}=Pred >dobj {}=Obj ", graph, "love/VBP");
}
Aggregations