Search in sources :

Example 11 with CorefChain

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

the class CorefAnnotatorSanityITest method getCorefChainString.

// helper to print out coref chains
public String getCorefChainString(Map<Integer, CorefChain> corefChains) {
    String returnString = "";
    if (corefChains != null) {
        for (CorefChain chain : corefChains.values()) {
            CorefChain.CorefMention representative = chain.getRepresentativeMention();
            boolean outputHeading = false;
            for (CorefChain.CorefMention mention : chain.getMentionsInTextualOrder()) {
                if (mention == representative)
                    continue;
                /*if (!outputHeading) {
            outputHeading = true;
            System.err.println("Coreference set:");
          }*/
                // all offsets start at 1!
                String corefResultString = String.format("(%d,%d,[%d,%d]) -> (%d,%d,[%d,%d]), that is: \"%s\" -> \"%s\"%n", mention.sentNum, mention.headIndex, mention.startIndex, mention.endIndex, representative.sentNum, representative.headIndex, representative.startIndex, representative.endIndex, mention.mentionSpan, representative.mentionSpan);
                returnString += corefResultString;
            }
        }
    }
    return returnString.trim();
}
Also used : CorefChain(edu.stanford.nlp.coref.data.CorefChain)

Example 12 with CorefChain

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

the class DeterministicCorefAnnotatorITest method testDeterministicCorefAnnotator.

public void testDeterministicCorefAnnotator() throws Exception {
    // create annotation with text
    String text = "Dan Ramage is working for\nMicrosoft. He's in Seattle!\nAt least, he used to be.  Ed is not in Seattle.";
    Annotation document = new Annotation(text);
    // annotate text with pipeline
    pipeline.annotate(document);
    // test CorefGraphAnnotation
    Map<Integer, CorefChain> corefChains = document.get(CorefCoreAnnotations.CorefChainAnnotation.class);
    Assert.assertNotNull(corefChains);
    // test chainID = m.corefClusterID
    for (int chainID : corefChains.keySet()) {
        CorefChain c = corefChains.get(chainID);
        for (CorefMention m : c.getMentionsInTextualOrder()) {
            Assert.assertEquals(m.corefClusterID, chainID);
        }
    }
    // test CorefClusterIdAnnotation
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    CoreLabel ramageToken = sentences.get(0).get(CoreAnnotations.TokensAnnotation.class).get(1);
    CoreLabel heToken = sentences.get(1).get(CoreAnnotations.TokensAnnotation.class).get(0);
    Integer ramageClusterId = ramageToken.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
    Assert.assertNotNull(ramageClusterId);
    Assert.assertSame(ramageClusterId, heToken.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class));
}
Also used : CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) CorefMention(edu.stanford.nlp.coref.data.CorefChain.CorefMention) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CorefChain(edu.stanford.nlp.coref.data.CorefChain) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap)

Example 13 with CorefChain

use of edu.stanford.nlp.coref.data.CorefChain 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 14 with CorefChain

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

the class ChineseHcorefDemo method main.

public static void main(String[] args) throws Exception {
    long startTime = System.currentTimeMillis();
    String text = "俄罗斯 航空 公司 一 名 官员 在 9号 说 , " + "米洛舍维奇 的 儿子 马可·米洛舍维奇 9号 早上 持 外交 护照 从 俄国 首都 莫斯科 搭机 飞往 中国 大陆 北京 , " + "可是 就 在 稍后 就 返回 莫斯科 。 " + "这 名 俄国 航空 公司 官员 说 马可 是 因为 护照 问题 而 在 北京 机场 被 中共 遣返 莫斯科 。 " + "北京 机场 方面 的 这 项 举动 清楚 显示 中共 有意 放弃 在 总统 大选 落败 的 前 南斯拉夫 总统 米洛舍维奇 , " + "因此 他 在 南斯拉夫 受到 民众 厌恶 的 儿子 马可 才 会 在 北京 机场 被 中共 当局 送回 莫斯科 。 " + "马可 持 外交 护照 能够 顺利 搭机 离开 莫斯科 , 但是 却 在 北京 受阻 , 可 算是 踢到 了 铁板 。 " + "可是 这 项 消息 和 先前 外界 谣传 中共 当局 准备 提供 米洛舍维奇 和 他 的 家人 安全 庇护所 有 着 很 大 的 出入 ," + " 一般 认为 在 去年 米洛舍维奇 挥兵 攻打 科索沃 境内 阿尔巴尼亚 一 分离主义 分子 的 时候 , " + "强力 反对 北约 组织 攻击 南斯拉夫 的 中共 , 会 全力 保护 米洛舍维奇 和 他 的 家人 及 亲信 。 " + "可是 从 9号 马可 被 送回 莫斯科 一 事 看 起来 , 中共 很 可能 会 放弃 米洛舍维奇 。";
    args = new String[] { "-props", "edu/stanford/nlp/hcoref/properties/zh-coref-default.properties" };
    Annotation document = new Annotation(text);
    Properties props = StringUtils.argsToProperties(args);
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    pipeline.annotate(document);
    System.out.println("---");
    System.out.println("coref chains");
    for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
        System.out.println("\t" + cc);
    }
    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
        System.out.println("---");
        System.out.println("mentions");
        for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) {
            System.out.println("\t" + m);
        }
    }
    long endTime = System.currentTimeMillis();
    long time = (endTime - startTime) / 1000;
    System.out.println("Running time " + time / 60 + "min " + time % 60 + "s");
}
Also used : CorefChain(edu.stanford.nlp.coref.data.CorefChain) Mention(edu.stanford.nlp.coref.data.Mention) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) Properties(java.util.Properties) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) Annotation(edu.stanford.nlp.pipeline.Annotation) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP)

Example 15 with CorefChain

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

the class CorefSystem method annotate.

public void annotate(Annotation ann) {
    Document document;
    try {
        document = docMaker.makeDocument(ann);
    } catch (Exception e) {
        throw new RuntimeException("Error making document", e);
    }
    CorefUtils.checkForInterrupt();
    corefAlgorithm.runCoref(document);
    if (removeSingletonClusters) {
        CorefUtils.removeSingletonClusters(document);
    }
    CorefUtils.checkForInterrupt();
    Map<Integer, CorefChain> result = Generics.newHashMap();
    for (CorefCluster c : document.corefClusters.values()) {
        result.put(c.clusterID, new CorefChain(c, document.positions));
    }
    ann.set(CorefCoreAnnotations.CorefChainAnnotation.class, result);
}
Also used : CorefCluster(edu.stanford.nlp.coref.data.CorefCluster) CorefChain(edu.stanford.nlp.coref.data.CorefChain) Document(edu.stanford.nlp.coref.data.Document)

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