Search in sources :

Example 86 with SemanticGraph

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]");
}
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)

Example 87 with SemanticGraph

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();
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) ArrayList(java.util.ArrayList) List(java.util.List)

Example 88 with SemanticGraph

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);
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) ArrayList(java.util.ArrayList) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 89 with SemanticGraph

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);
}
Also used : SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 90 with SemanticGraph

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));
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) 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