Search in sources :

Example 26 with CorefChain

use of edu.stanford.nlp.coref.data.CorefChain in project CoreNLP by stanfordnlp.

the class XMLOutputter method addCorefGraphInfo.

/**
   * Generates the XML content for the coreference chain object.
   */
private static boolean addCorefGraphInfo(Options options, Element corefInfo, List<CoreMap> sentences, Map<Integer, CorefChain> corefChains, String curNS) {
    boolean foundCoref = false;
    for (CorefChain chain : corefChains.values()) {
        if (!options.printSingletons && chain.getMentionsInTextualOrder().size() <= 1)
            continue;
        foundCoref = true;
        Element chainElem = new Element("coreference", curNS);
        CorefChain.CorefMention source = chain.getRepresentativeMention();
        addCorefMention(options, chainElem, curNS, sentences, source, true);
        for (CorefChain.CorefMention mention : chain.getMentionsInTextualOrder()) {
            if (mention == source)
                continue;
            addCorefMention(options, chainElem, curNS, sentences, mention, false);
        }
        corefInfo.appendChild(chainElem);
    }
    return foundCoref;
}
Also used : CorefChain(edu.stanford.nlp.coref.data.CorefChain)

Example 27 with CorefChain

use of edu.stanford.nlp.coref.data.CorefChain in project CoreNLP by stanfordnlp.

the class Sentence method coref.

/**
   * Get the coreference chain for just this sentence.
   * Note that this method is actually fairly computationally expensive to call, as it constructs and prunes
   * the coreference data structure for the entire document.
   *
   * @return A coreference chain, but only for this sentence
   */
public Map<Integer, CorefChain> coref() {
    // Get the raw coref structure
    Map<Integer, CorefChain> allCorefs = document.coref();
    // Delete coreference chains not in this sentence
    Set<Integer> toDeleteEntirely = new HashSet<>();
    for (Map.Entry<Integer, CorefChain> integerCorefChainEntry : allCorefs.entrySet()) {
        CorefChain chain = integerCorefChainEntry.getValue();
        List<CorefChain.CorefMention> mentions = new ArrayList<>(chain.getMentionsInTextualOrder());
        mentions.stream().filter(m -> m.sentNum != this.sentenceIndex() + 1).forEach(chain::deleteMention);
        if (chain.getMentionsInTextualOrder().isEmpty()) {
            toDeleteEntirely.add(integerCorefChainEntry.getKey());
        }
    }
    // Clean up dangling empty chains
    toDeleteEntirely.forEach(allCorefs::remove);
    // Return
    return allCorefs;
}
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) CorefChain(edu.stanford.nlp.coref.data.CorefChain)

Aggregations

CorefChain (edu.stanford.nlp.coref.data.CorefChain)27 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)17 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)12 CoreLabel (edu.stanford.nlp.ling.CoreLabel)12 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)10 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)7 Tree (edu.stanford.nlp.trees.Tree)7 CoreMap (edu.stanford.nlp.util.CoreMap)7 RelationTriple (edu.stanford.nlp.ie.util.RelationTriple)6 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)6 CorefMention (edu.stanford.nlp.coref.data.CorefChain.CorefMention)5 Annotation (edu.stanford.nlp.pipeline.Annotation)5 SentimentCoreAnnotations (edu.stanford.nlp.sentiment.SentimentCoreAnnotations)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 EntityMention (edu.stanford.nlp.ie.machinereading.structure.EntityMention)3 MachineReadingAnnotations (edu.stanford.nlp.ie.machinereading.structure.MachineReadingAnnotations)3 RelationMention (edu.stanford.nlp.ie.machinereading.structure.RelationMention)3 Span (edu.stanford.nlp.ie.machinereading.structure.Span)3 CoreAnnotation (edu.stanford.nlp.ling.CoreAnnotation)3