Search in sources :

Example 1 with Annotator

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

the class Document method coref.

/**
   * Returns the coref chains in the document. This is a map from coref cluster IDs, to the coref chain
   * with that ID.
   * @param props The properties to use in the {@link edu.stanford.nlp.pipeline.DeterministicCorefAnnotator}.
   */
public Map<Integer, CorefChain> coref(Properties props) {
    synchronized (this.impl) {
        if (impl.getCorefChainCount() == 0) {
            // Run prerequisites
            // default is rule mention annotator
            this.runLemma(props).runNER(props).runParse(props);
            // Run mention
            Supplier<Annotator> mention = (props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultMention : getOrCreate(STANFORD_MENTION, props, () -> backend.mention(props));
            // Run coref
            Supplier<Annotator> coref = (props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultCoref : getOrCreate(STANFORD_COREF, props, () -> backend.coref(props));
            Annotation ann = asAnnotation(true);
            mention.get().annotate(ann);
            coref.get().annotate(ann);
            // Convert to proto
            synchronized (serializer) {
                for (CorefChain chain : ann.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
                    impl.addCorefChain(serializer.toProto(chain));
                }
            }
        }
        Map<Integer, CorefChain> corefs = Generics.newHashMap();
        for (CoreNLPProtos.CorefChain chain : impl.getCorefChainList()) {
            corefs.put(chain.getChainID(), fromProto(chain));
        }
        return corefs;
    }
}
Also used : Annotator(edu.stanford.nlp.pipeline.Annotator) CorefChain(edu.stanford.nlp.coref.data.CorefChain)

Example 2 with Annotator

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

the class Document method runParse.

Document runParse(Properties props) {
    if (this.sentences != null && this.sentences.size() > 0 && this.sentences.get(0).rawSentence().hasParseTree()) {
        return this;
    }
    // Run annotator
    boolean cacheAnnotation = false;
    Annotator parse = ((props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultParse : getOrCreate(STANFORD_PARSE, props, () -> backend.parse(props))).get();
    if (parse.requires().contains(CoreAnnotations.PartOfSpeechAnnotation.class) || System.getenv("CORENLP_HOST") != null) {
        // Run the POS tagger if we are (or may be) using the shift reduce parser
        runPOS(props);
        cacheAnnotation = true;
    } else {
        sentences();
    }
    Annotation ann = asAnnotation(cacheAnnotation);
    parse.annotate(ann);
    // Update data
    synchronized (serializer) {
        for (int i = 0; i < sentences.size(); ++i) {
            CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(i);
            Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
            Tree binaryTree = sentence.get(TreeCoreAnnotations.BinarizedTreeAnnotation.class);
            sentences.get(i).updateParse(serializer.toProto(tree), binaryTree == null ? null : serializer.toProto(binaryTree));
            sentences.get(i).updateDependencies(ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class)), ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class)), ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class)));
        }
    }
    return this;
}
Also used : Annotator(edu.stanford.nlp.pipeline.Annotator) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) Tree(edu.stanford.nlp.trees.Tree) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations)

Example 3 with Annotator

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

the class RuleBasedCorefMentionFinder method getParser.

private Annotator getParser() {
    if (parserProcessor == null) {
        Annotator parser = StanfordCoreNLP.getExistingAnnotator("parse");
        if (parser == null) {
            // TODO: these assertions rule out the possibility of alternately named parse/pos annotators
            throw new AssertionError("Failed to get parser - this should not be possible");
        }
        if (parser.requires().contains(CoreAnnotations.PartOfSpeechAnnotation.class)) {
            Annotator tagger = StanfordCoreNLP.getExistingAnnotator("pos");
            if (tagger == null) {
                throw new AssertionError("Parser required tagger, but failed to find the pos annotator");
            }
            List<Annotator> annotators = Generics.newArrayList();
            annotators.add(tagger);
            annotators.add(parser);
            parserProcessor = new AnnotationPipeline(annotators);
        } else {
            parserProcessor = parser;
        }
    }
    return parserProcessor;
}
Also used : Annotator(edu.stanford.nlp.pipeline.Annotator) AnnotationPipeline(edu.stanford.nlp.pipeline.AnnotationPipeline) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations)

Example 4 with Annotator

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

the class Document method sentences.

/**
   * Get the sentences in this document, as a list.
   * @param props The properties to use in the {@link edu.stanford.nlp.pipeline.WordsToSentencesAnnotator}.
   * @return A list of Sentence objects representing the sentences in the document.
   */
protected List<Sentence> sentences(Properties props, Annotator tokenizer) {
    if (sentences == null) {
        Annotator ssplit = props == EMPTY_PROPS ? defaultSSplit : getOrCreate(STANFORD_SSPLIT, props, () -> backend.wordToSentences(props)).get();
        // Annotate
        Annotation ann = new Annotation(this.impl.getText());
        tokenizer.annotate(ann);
        ssplit.annotate(ann);
        // (docid)
        if (ann.containsKey(CoreAnnotations.DocIDAnnotation.class)) {
            impl.setDocID(ann.get(CoreAnnotations.DocIDAnnotation.class));
        }
        // (sentences)
        List<CoreMap> sentences = ann.get(CoreAnnotations.SentencesAnnotation.class);
        this.sentences = new ArrayList<>(sentences.size());
        for (CoreMap sentence : sentences) {
            //Sentence sent = new Sentence(this, sentence);
            Sentence sent = new Sentence(this, this.serializer.toProtoBuilder(sentence), sentence.get(CoreAnnotations.TextAnnotation.class), defaultProps);
            this.sentences.add(sent);
            this.impl.addSentence(sent.serialize());
        }
    }
    return sentences;
}
Also used : Annotator(edu.stanford.nlp.pipeline.Annotator) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SentimentCoreAnnotations(edu.stanford.nlp.sentiment.SentimentCoreAnnotations) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations)

Example 5 with Annotator

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

the class Sentence method openieTriples.

/**
   * Get the OpenIE triples associated with this sentence.
   * Note that this function may be slower than you would expect, as it has to
   * convert the underlying Protobuf representation back into {@link CoreLabel}s.
   *
   * @param props The properties to use for the OpenIE annotator.
   * @return A collection of {@link RelationTriple} objects representing the OpenIE triples in the sentence.
   */
public Collection<RelationTriple> openieTriples(Properties props) {
    document.runOpenie(props);
    synchronized (impl) {
        List<CoreLabel> tokens = asCoreLabels();
        Annotation doc = document.asAnnotation();
        return impl.getOpenieTripleList().stream().map(x -> ProtobufAnnotationSerializer.fromProto(x, doc, this.sentenceIndex())).collect(Collectors.toList());
    }
}
Also used : java.util(java.util) CorefChain(edu.stanford.nlp.coref.data.CorefChain) ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) edu.stanford.nlp.util(edu.stanford.nlp.util) BiFunction(java.util.function.BiFunction) CoreNLPProtos(edu.stanford.nlp.pipeline.CoreNLPProtos) Tree(edu.stanford.nlp.trees.Tree) SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) Function(java.util.function.Function) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) OutputStream(java.io.OutputStream) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) Polarity(edu.stanford.nlp.naturalli.Polarity) OperatorSpec(edu.stanford.nlp.naturalli.OperatorSpec) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Annotator(edu.stanford.nlp.pipeline.Annotator) SemanticGraphFactory(edu.stanford.nlp.semgraph.SemanticGraphFactory) Stream(java.util.stream.Stream) Annotation(edu.stanford.nlp.pipeline.Annotation) TokenSequenceMatcher(edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher) SentenceFragment(edu.stanford.nlp.naturalli.SentenceFragment) InputStream(java.io.InputStream) CoreLabel(edu.stanford.nlp.ling.CoreLabel) Annotation(edu.stanford.nlp.pipeline.Annotation)

Aggregations

Annotator (edu.stanford.nlp.pipeline.Annotator)8 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)5 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)4 Annotation (edu.stanford.nlp.pipeline.Annotation)4 CorefChain (edu.stanford.nlp.coref.data.CorefChain)3 Tree (edu.stanford.nlp.trees.Tree)3 RelationTriple (edu.stanford.nlp.ie.util.RelationTriple)2 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 TokenSequenceMatcher (edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher)2 TokenSequencePattern (edu.stanford.nlp.ling.tokensregex.TokenSequencePattern)2 OperatorSpec (edu.stanford.nlp.naturalli.OperatorSpec)2 Polarity (edu.stanford.nlp.naturalli.Polarity)2 SentenceFragment (edu.stanford.nlp.naturalli.SentenceFragment)2 CoreNLPProtos (edu.stanford.nlp.pipeline.CoreNLPProtos)2 ProtobufAnnotationSerializer (edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer)2 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)2 SemanticGraphFactory (edu.stanford.nlp.semgraph.SemanticGraphFactory)2 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)2 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)2 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)2