Search in sources :

Example 76 with Annotation

use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.

the class SUTimeSimpleParser method parse.

/**
   * Parse a string with SUTime.
   *
   * @throws SUTimeParsingError if anything goes wrong
   */
public static Temporal parse(String str) throws SUTimeParsingError {
    try {
        Annotation doc = new Annotation(str);
        pipeline.annotate(doc);
        assert doc.get(CoreAnnotations.SentencesAnnotation.class) != null;
        assert doc.get(CoreAnnotations.SentencesAnnotation.class).size() > 0;
        List<CoreMap> timexAnnotations = doc.get(TimeAnnotations.TimexAnnotations.class);
        if (timexAnnotations.size() > 1) {
            throw new RuntimeException("Too many timexes for '" + str + "'");
        }
        CoreMap timex = timexAnnotations.get(0);
        return timex.get(TimeExpression.Annotation.class).getTemporal();
    } catch (Exception e) {
        SUTimeSimpleParser.SUTimeParsingError parsingError = new SUTimeSimpleParser.SUTimeParsingError(str);
        parsingError.initCause(e);
        throw parsingError;
    }
}
Also used : CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 77 with Annotation

use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.

the class DependencyParserITest method testCCProcess.

/**
   * Test that postprocessing like CC-processing can handle the parser
   * output properly
   */
public void testCCProcess() {
    Properties props = PropertiesUtils.fromString("annotators=tokenize,ssplit,pos,depparse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    String text = "Chris and John went to the store.";
    Annotation document = new Annotation(text);
    pipeline.annotate(document);
    SemanticGraph ccProcessed = document.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
    Collection<TypedDependency> dependencies = ccProcessed.typedDependencies();
    GrammaticalRelation expected = UniversalEnglishGrammaticalRelations.getConj("and");
    assertTrue(dependencies.stream().map(TypedDependency::reln).collect(Collectors.toList()).contains(expected));
}
Also used : TypedDependency(edu.stanford.nlp.trees.TypedDependency) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) Properties(java.util.Properties) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 78 with Annotation

use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.

the class DependencyParserITest method testSerializationAnnotation.

/**
   * Test that Java serialization works properly.
   */
public void testSerializationAnnotation() throws IOException, ClassNotFoundException {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse");
    String text = "Barack Obama, a Yale professor, is president.";
    Annotation document = new Annotation(text);
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    pipeline.annotate(document);
    // Serialization should not bork.
    File tempfile = IOUtils.writeObjectToTempFile(document.get(CoreAnnotations.SentencesAnnotation.class), "temp");
    // Deserialization should not bork.
    List<CoreMap> readSentences = IOUtils.readObjectFromFile(tempfile);
    // Make sure we didn't lose any information
    assertEquals(document.get(CoreAnnotations.SentencesAnnotation.class), readSentences);
}
Also used : Properties(java.util.Properties) File(java.io.File) CoreMap(edu.stanford.nlp.util.CoreMap) Annotation(edu.stanford.nlp.pipeline.Annotation) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP)

Example 79 with Annotation

use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.

the class NumberSequenceClassifierITest method checkLabels.

private static void checkLabels(StanfordCoreNLP pipe, String text, String[] labels, String[] normed) {
    Annotation doc = new Annotation(text);
    pipe.annotate(doc);
    assertTrue(doc.get(CoreAnnotations.SentencesAnnotation.class) != null);
    assertTrue(doc.get(CoreAnnotations.SentencesAnnotation.class).size() > 0);
    CoreMap sent = doc.get(CoreAnnotations.SentencesAnnotation.class).get(0);
    assertTrue(sent.get(CoreAnnotations.TokensAnnotation.class) != null);
    List<CoreLabel> tokens = sent.get(CoreAnnotations.TokensAnnotation.class);
    if (VERBOSE) {
        for (CoreLabel token : tokens) {
            System.out.println('\t' + token.word() + ' ' + token.tag() + ' ' + token.ner() + ' ' + (token.containsKey(CoreAnnotations.NumericCompositeTypeAnnotation.class) ? token.get(CoreAnnotations.NumericCompositeValueAnnotation.class) + " " : "") + (token.containsKey(TimeAnnotations.TimexAnnotation.class) ? token.get(TimeAnnotations.TimexAnnotation.class) + " " : ""));
        }
    }
    // check NER labels
    assertTrue(tokens.size() == labels.length);
    for (int i = 0; i < labels.length; i++) {
        if (labels[i] == null) {
            assertTrue(tokens.get(i).ner() == null);
        } else {
            Pattern p = Pattern.compile(labels[i]);
            System.err.println("COMPARING NER " + labels[i] + " with " + tokens.get(i).ner());
            System.err.flush();
            assertTrue("NER should not be null for token " + tokens.get(i) + " in sentence " + tokens, tokens.get(i).ner() != null);
            assertTrue(tokens.get(i).ner() + " does not match " + p + " for token " + tokens.get(i) + " in sentence " + tokens, p.matcher(tokens.get(i).ner()).matches());
        }
    }
    // check normalized values, if gold is given
    if (normed != null) {
        assertTrue(tokens.size() == normed.length);
        for (int i = 0; i < normed.length; i++) {
            if (normed[i] == null) {
                assertTrue(tokens.get(i).get(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class) == null);
            } else {
                Pattern p = Pattern.compile(normed[i]);
                String n = tokens.get(i).get(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class);
                String message = "COMPARING NORMED \"" + normed[i] + "\" with \"" + n + "\"";
                assertTrue(message + "; latter should not be null", n != null);
                assertTrue(message + "; latter should match", p.matcher(n).matches());
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 80 with Annotation

use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.

the class NaturalLogicAnnotatorITest method testAnnotatorRuns.

@Test
public void testAnnotatorRuns() {
    // Run pipeline
    StanfordCoreNLP pipeline = new StanfordCoreNLP(new Properties() {

        {
            setProperty("annotators", "tokenize,ssplit,pos,lemma,parse,natlog");
            setProperty("ssplit.isOneSentence", "true");
            setProperty("tokenize.class", "PTBTokenizer");
            setProperty("tokenize.language", "en");
            setProperty("enforceRequirements", "false");
        }
    });
    Annotation ann = new Annotation("All cats have tails");
    pipeline.annotate(ann);
    // Check output
    List<CoreLabel> tokens = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(CoreAnnotations.TokensAnnotation.class);
    assertTrue(tokens.get(0).containsKey(NaturalLogicAnnotations.OperatorAnnotation.class));
    assertTrue(tokens.get(0).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
    assertTrue(tokens.get(1).get(NaturalLogicAnnotations.PolarityAnnotation.class).isDownwards());
    assertTrue(tokens.get(2).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
    assertTrue(tokens.get(3).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) Properties(java.util.Properties) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) Test(org.junit.Test)

Aggregations

Annotation (edu.stanford.nlp.pipeline.Annotation)91 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)58 CoreMap (edu.stanford.nlp.util.CoreMap)50 CoreLabel (edu.stanford.nlp.ling.CoreLabel)30 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)27 ArrayList (java.util.ArrayList)25 Properties (java.util.Properties)25 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)19 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)14 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)13 SentencesAnnotation (edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation)12 TreeAnnotation (edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation)12 List (java.util.List)11 Tree (edu.stanford.nlp.trees.Tree)10 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)8 IOException (java.io.IOException)8 TokensAnnotation (edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation)7 CorefChain (edu.stanford.nlp.coref.data.CorefChain)6 EntityMentionsAnnotation (edu.stanford.nlp.ie.machinereading.structure.MachineReadingAnnotations.EntityMentionsAnnotation)6 CoreAnnotation (edu.stanford.nlp.ling.CoreAnnotation)6