Search in sources :

Example 21 with SemanticGraph

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

the class SemgrexPatternITest method testNERUniversalDependencies.

@Test
public void testNERUniversalDependencies() throws Exception {
    String sentence = "John lives in Washington.";
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    props.setProperty("parse.originalDependencies", "false");
    Annotation doc = new Annotation(sentence);
    pipeline.annotate(doc);
    CoreMap sent = doc.get(CoreAnnotations.SentencesAnnotation.class).get(0);
    SemanticGraph graph = sent.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
    graph.prettyPrint();
    String patStr = "({word:/lives/} >/nmod:in/ {word:/\\QCalifornia\\E|\\QWashington\\E/} >nsubj {ner:PERSON})";
    SemgrexPattern pat = SemgrexPattern.compile(patStr);
    SemgrexMatcher mat = pat.matcher(graph, true);
    assertTrue(mat.find());
}
Also used : SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Properties(java.util.Properties) CoreMap(edu.stanford.nlp.util.CoreMap) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) Test(org.junit.Test)

Example 22 with SemanticGraph

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

the class Ssurgeon method createPatternXMLDoc.

private static Document createPatternXMLDoc(List<SsurgeonPattern> patterns) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document domDoc = db.newDocument();
        Element rootElt = domDoc.createElement(SsurgeonPattern.ELT_LIST_TAG);
        domDoc.appendChild(rootElt);
        int ordinal = 1;
        for (SsurgeonPattern pattern : patterns) {
            Element patElt = domDoc.createElement(SsurgeonPattern.SSURGEON_ELEM_TAG);
            patElt.setAttribute(SsurgeonPattern.ORDINAL_ATTR, String.valueOf(ordinal));
            Element semgrexElt = domDoc.createElement(SsurgeonPattern.SEMGREX_ELEM_TAG);
            semgrexElt.appendChild(domDoc.createTextNode(pattern.getSemgrexPattern().pattern()));
            patElt.appendChild(semgrexElt);
            Element uidElem = domDoc.createElement(SsurgeonPattern.UID_ELEM_TAG);
            uidElem.appendChild(domDoc.createTextNode(pattern.getUID()));
            patElt.appendChild(uidElem);
            Element notesElem = domDoc.createElement(SsurgeonPattern.NOTES_ELEM_TAG);
            notesElem.appendChild(domDoc.createTextNode(pattern.getNotes()));
            patElt.appendChild(notesElem);
            SemanticGraph semgrexGraph = pattern.getSemgrexGraph();
            if (semgrexGraph != null) {
                Element patNode = domDoc.createElement(SsurgeonPattern.SEMGREX_GRAPH_ELEM_TAG);
                patNode.appendChild(domDoc.createTextNode(semgrexGraph.toCompactString()));
            }
            Element editList = domDoc.createElement(SsurgeonPattern.EDIT_LIST_ELEM_TAG);
            patElt.appendChild(editList);
            int editOrdinal = 1;
            for (SsurgeonEdit edit : pattern.getEditScript()) {
                Element editElem = domDoc.createElement(SsurgeonPattern.EDIT_ELEM_TAG);
                editElem.setAttribute(SsurgeonPattern.ORDINAL_ATTR, String.valueOf(editOrdinal));
                editElem.appendChild(domDoc.createTextNode(edit.toEditString()));
                editList.appendChild(editElem);
                editOrdinal++;
            }
            rootElt.appendChild(patElt);
            ordinal++;
        }
        return domDoc;
    } catch (Exception e) {
        log.error(Ssurgeon.class.getName(), "createPatternXML");
        log.error(e);
        return null;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Document(org.w3c.dom.Document)

Example 23 with SemanticGraph

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

the class SsurgeonPattern method main.

/**
   * Simply reads the given Ssurgeon pattern from file (args[0]), parses it, and prints it out.
   * Use this for debugging the class and patterns. 
   */
public static void main(String[] args) {
    if (args.length == 0) {
        System.out.println("Usage: SsurgeonPattern FILEPATH [\"COMPACT_SEMANTIC_GRAPH\"], FILEPATH=path to ssurgeon pattern to parse and print., SENTENCE=test sentence (in quotes)");
        System.exit(-1);
    }
    File tgtFile = new File(args[0]);
    try {
        Ssurgeon.inst().initLog(new File("./ssurgeon.log"));
        Ssurgeon.inst().setLogPrefix("SsurgeonPattern test");
        List<SsurgeonPattern> patterns = Ssurgeon.inst().readFromFile(tgtFile);
        for (SsurgeonPattern pattern : patterns) {
            System.out.println("- - - - -");
            System.out.println(pattern);
        }
        if (args.length > 1) {
            for (int i = 1; i < args.length; i++) {
                String text = args[i];
                SemanticGraph sg = SemanticGraph.valueOf(text);
                Collection<SemanticGraph> generated = Ssurgeon.inst().exhaustFromPatterns(patterns, sg);
                System.out.println("\n= = = = = = = = = =\nSrc text = " + text);
                System.out.println(sg.toCompactString());
                System.out.println("# generated  = " + generated.size());
                for (SemanticGraph genSg : generated) {
                    System.out.println(genSg);
                    System.out.println(". . . . .");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 24 with SemanticGraph

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

the class CoNLLUDocumentReaderWriterTest method testComment.

public void testComment() {
    CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
    Reader stringReader = new StringReader(COMMENT_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));
    assertEquals(Integer.valueOf(3), sg.getNodeByIndex(1).get(CoreAnnotations.LineNumberAnnotation.class));
    assertEquals(2, sg.getComments().size());
    assertEquals("#comment line 1", sg.getComments().get(0));
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 25 with SemanticGraph

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

the class CoNLLUDocumentReaderWriterTest method testSingleReadAndWrite.

/**
     * Tests whether reading a Semantic Graph and printing it
     * is equal to the original input.
     */
private void testSingleReadAndWrite(String input) {
    String clean = input.replaceAll("[\\t ]+", "\t");
    CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
    CoNLLUDocumentWriter writer = new CoNLLUDocumentWriter();
    Reader stringReader = new StringReader(clean);
    Iterator<SemanticGraph> it = reader.getIterator(stringReader);
    SemanticGraph sg = it.next();
    String output = writer.printSemanticGraph(sg);
    assertEquals(clean, output);
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

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