use of edu.stanford.nlp.semgraph.SemanticGraph 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]] dobj>[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]");
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenterTest method mkExtraction.
/**
* Create a relation from a CoNLL format like:
* <pre>
* word_index word parent_index incoming_relation
* </pre>
*/
protected Optional<RelationTriple> mkExtraction(String conll, int listIndex, boolean allNominals) {
Pair<SemanticGraph, List<CoreLabel>> info = mkTree(conll);
SemanticGraph tree = info.first;
List<CoreLabel> sentence = info.second;
// Run extractor
Optional<RelationTriple> segmented = new RelationTripleSegmenter(allNominals).segment(tree, Optional.empty());
if (segmented.isPresent() && listIndex == 0) {
return segmented;
}
List<RelationTriple> extracted = new RelationTripleSegmenter(allNominals).extract(tree, sentence);
if (extracted.size() > listIndex) {
return Optional.of(extracted.get(listIndex - (segmented.isPresent() ? 1 : 0)));
}
return Optional.empty();
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenterTest method mkTree.
/**
* Parse a CoNLL formatted tree into a SemanticGraph object (along with a list of tokens).
*
* @param conll The CoNLL formatted tree.
*
* @return A pair of a SemanticGraph and a token list, corresponding to the parse of the sentence
* and to tokens in the sentence.
*/
protected Pair<SemanticGraph, List<CoreLabel>> mkTree(String conll) {
List<CoreLabel> sentence = new ArrayList<>();
SemanticGraph tree = new SemanticGraph();
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int index = Integer.parseInt(fields[0]);
String word = fields[1];
CoreLabel label = IETestUtils.mkWord(word, index);
sentence.add(label);
if (fields[2].equals("0")) {
tree.addRoot(new IndexedWord(label));
} else {
tree.addVertex(new IndexedWord(label));
}
if (fields.length > 4) {
label.setTag(fields[4]);
}
if (fields.length > 5) {
label.setNER(fields[5]);
}
if (fields.length > 6) {
label.setLemma(fields[6]);
}
}
int i = 0;
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int parent = Integer.parseInt(fields[2]);
String reln = fields[3];
if (parent > 0) {
tree.addEdge(new IndexedWord(sentence.get(parent - 1)), new IndexedWord(sentence.get(i)), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
}
i += 1;
}
return Pair.makePair(tree, sentence);
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenterTest method testVPOnlyReplacedWith.
public void testVPOnlyReplacedWith() {
String conll = "1\treplaced\t0\tconj:and\tVBD\n" + "2\twith\t5\tcase\tIN\n" + "3\ta\t5\tdet\tDT\n" + "4\tdifferent\t5\tamod\tJJ\n" + "5\ttype\t1\tnmod:with\tNN\n" + "6\tof\t7\tcase\tIN\n" + "7\tfilter\t5\tnmod:of\tNN\n";
// Positive case
boolean matches = false;
SemanticGraph tree = mkTree(conll).first;
for (SemgrexPattern candidate : new RelationTripleSegmenter().VP_PATTERNS) {
if (candidate.matcher(tree).matches()) {
matches = true;
}
}
assertTrue(matches);
}
use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.
the class CoNLLUDocumentReaderWriterTest method testMultiWords.
public void testMultiWords() {
CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
Reader stringReader = new StringReader(MULTIWORD_TEST_INPUT);
Iterator<SemanticGraph> it = reader.getIterator(stringReader);
SemanticGraph sg = it.next();
assertNotNull(sg);
assertFalse("The input only contains one dependency tree.", it.hasNext());
assertEquals("[have/VBP nsubj>I/PRP neg>not/RB dobj>[clue/NN det>a/DT] punct>./.]", sg.toCompactString(true));
for (IndexedWord iw : sg.vertexListSorted()) {
if (iw.index() != 2 && iw.index() != 3) {
assertEquals("", iw.originalText());
} else {
assertEquals("haven't", iw.originalText());
}
}
assertEquals(Integer.valueOf(3), sg.getNodeByIndex(2).get(CoreAnnotations.LineNumberAnnotation.class));
}
Aggregations