Search in sources :

Example 21 with AnnotatedText

use of com.graphaware.nlp.domain.AnnotatedText in project neo4j-nlp-stanfordnlp by graphaware.

the class DependencyParserTest method testStanfordNLPWithPredefinedProcessors.

@Test
public void testStanfordNLPWithPredefinedProcessors() throws Exception {
    StanfordCoreNLP pipeline = ((StanfordTextProcessor) textProcessor).getPipeline("default");
    String text = "Donald Trump flew yesterday to New York City";
    AnnotatedText at = textProcessor.annotateText(text, "en", PIPELINE_DEFAULT);
    Annotation document = new Annotation(text);
    pipeline.annotate(document);
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    CoreMap sentence = sentences.get(0);
    System.out.println(sentence.toString());
    SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
    System.out.println(graph);
    List<SemanticGraphEdge> edges = graph.edgeListSorted();
    for (SemanticGraphEdge edge : edges) {
        System.out.println(edge.getRelation().getShortName());
        System.out.println(String.format("Source is : %s - Target is : %s - Relation is : %s", edge.getSource(), edge.getTarget(), edge.getRelation()));
    }
}
Also used : AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) StanfordTextProcessor(com.graphaware.nlp.processor.stanford.StanfordTextProcessor) CoreMap(edu.stanford.nlp.util.CoreMap) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) Test(org.junit.Test)

Example 22 with AnnotatedText

use of com.graphaware.nlp.domain.AnnotatedText in project neo4j-nlp-stanfordnlp by graphaware.

the class DependencyParserTest method testEnhancedDependencyParsingWithComplexTest.

@Test
public void testEnhancedDependencyParsingWithComplexTest() throws Exception {
    String text = "Softfoot and Small Paul would kill the Old Beard, Dirk would do Blane, and Lark and his cousins would silence Bannen and old Dywen, to keep them from sniffing after their trail.";
    StanfordCoreNLP pipeline = ((StanfordTextProcessor) textProcessor).getPipeline("default");
    AnnotatedText at = textProcessor.annotateText(text, "en", PIPELINE_DEFAULT);
    Annotation document = new Annotation(text);
    pipeline.annotate(document);
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        System.out.println(sentence.toString());
        SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
        System.out.println(graph);
        for (SemanticGraphEdge edge : graph.edgeListSorted()) {
            System.out.println(String.format("Source is : %s - Target is : %s - Relation is : %s", edge.getSource(), edge.getTarget(), edge.getRelation()));
        }
    }
}
Also used : AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) StanfordTextProcessor(com.graphaware.nlp.processor.stanford.StanfordTextProcessor) CoreMap(edu.stanford.nlp.util.CoreMap) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) Test(org.junit.Test)

Example 23 with AnnotatedText

use of com.graphaware.nlp.domain.AnnotatedText in project neo4j-nlp by graphaware.

the class AnnotatedTextPersister method fromNode.

@Override
public AnnotatedText fromNode(Node node, Object... properties) {
    if (!node.hasLabel(configuration().getLabelFor(Labels.AnnotatedText))) {
        throw new RuntimeException("Expected an " + configuration().getLabelFor(Labels.AnnotatedText) + " node.");
    }
    AnnotatedText annotatedText = mapper().convertValue(node.getAllProperties(), AnnotatedText.class);
    node.getRelationships(configuration().getRelationshipFor(Relationships.CONTAINS_SENTENCE), Direction.OUTGOING).forEach(relationship -> {
        Sentence sentence = (Sentence) getPersister(Sentence.class).fromNode(relationship.getEndNode());
        annotatedText.addSentence(sentence);
    });
    return annotatedText;
}
Also used : AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) Sentence(com.graphaware.nlp.domain.Sentence)

Example 24 with AnnotatedText

use of com.graphaware.nlp.domain.AnnotatedText in project neo4j-nlp by graphaware.

the class AnnotatedTextPersistenceTest method testTagOccurrenceGetAValue.

@Test
public void testTagOccurrenceGetAValue() {
    clearDb();
    AnnotatedText annotatedText = createAnnotatedTextWithSameTagInSameTextWithDifferentPos();
    TestNLPGraph test = new TestNLPGraph(getDatabase());
    try (Transaction tx = getDatabase().beginTx()) {
        getNLPManager().getPersister(AnnotatedText.class).persist(annotatedText, "test", "1");
        tx.success();
    }
    test.assertTagOccurrenceWithValueExist("cool");
    executeInTransaction("MATCH (n:TagOccurrence) WHERE n.value = 'cool' RETURN n", (result -> {
        assertTrue(result.hasNext());
        Node n = (Node) result.next().get("n");
        String[] ners = (String[]) n.getProperty("ne");
        assertTrue(Arrays.asList(ners).contains("NER_Cool0"));
    }));
}
Also used : Arrays(java.util.Arrays) TextProcessor(com.graphaware.nlp.processor.TextProcessor) Sentence(com.graphaware.nlp.domain.Sentence) Test(org.junit.Test) StubTextProcessor(com.graphaware.nlp.stub.StubTextProcessor) Node(org.neo4j.graphdb.Node) TestNLPGraph(com.graphaware.nlp.util.TestNLPGraph) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) Transaction(org.neo4j.graphdb.Transaction) Assert(org.junit.Assert) Collections(java.util.Collections) NLPIntegrationTest(com.graphaware.nlp.NLPIntegrationTest) Tag(com.graphaware.nlp.domain.Tag) Before(org.junit.Before) Transaction(org.neo4j.graphdb.Transaction) AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) TestNLPGraph(com.graphaware.nlp.util.TestNLPGraph) Node(org.neo4j.graphdb.Node) Test(org.junit.Test) NLPIntegrationTest(com.graphaware.nlp.NLPIntegrationTest)

Example 25 with AnnotatedText

use of com.graphaware.nlp.domain.AnnotatedText in project neo4j-nlp by graphaware.

the class AnnotatedTextPersistenceTest method createAnnotatedTextWithSameTagInSameTextWithDifferentPos.

private AnnotatedText createAnnotatedTextWithSameTagInSameTextWithDifferentPos() {
    AnnotatedText annotatedText = new AnnotatedText();
    AtomicInteger inc = new AtomicInteger();
    for (String s : "Hello my name is cool. And I am cool.".split("\\.")) {
        Sentence sentence = new Sentence(s, inc.get());
        for (String token : s.split(" ")) {
            Tag tag = new Tag(token, "en");
            if (token.equals("cool")) {
                int v = inc.get();
                tag.setPos(Collections.singletonList("cool" + v));
                tag.setNe(Collections.singletonList("NER_Cool" + v));
            }
            sentence.addTagOccurrence(0, 20, token, sentence.addTag(tag));
        }
        inc.incrementAndGet();
        annotatedText.addSentence(sentence);
    }
    return annotatedText;
}
Also used : AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tag(com.graphaware.nlp.domain.Tag) Sentence(com.graphaware.nlp.domain.Sentence)

Aggregations

AnnotatedText (com.graphaware.nlp.domain.AnnotatedText)32 Test (org.junit.Test)18 Sentence (com.graphaware.nlp.domain.Sentence)17 Tag (com.graphaware.nlp.domain.Tag)11 TextProcessor (com.graphaware.nlp.processor.TextProcessor)9 TestAnnotatedText (com.graphaware.nlp.util.TestAnnotatedText)8 NLPIntegrationTest (com.graphaware.nlp.NLPIntegrationTest)5 TagUtils.newTag (com.graphaware.nlp.util.TagUtils.newTag)5 TestNLPGraph (com.graphaware.nlp.util.TestNLPGraph)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Transaction (org.neo4j.graphdb.Transaction)5 StanfordTextProcessor (com.graphaware.nlp.processor.stanford.StanfordTextProcessor)3 StubTextProcessor (com.graphaware.nlp.stub.StubTextProcessor)3 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Node (org.neo4j.graphdb.Node)3 PipelineSpecification (com.graphaware.nlp.dsl.request.PipelineSpecification)2