zoukankan      html  css  js  c++  java
  • Stanford CoreNLP 3.6.0 中文指代消解模块调用失败的解决方案

    当前中文指代消解领域比较活跃的研究者是Chen和Vincent Ng,这两个人近两年在AAAI2014, 2015发了一些相关的文章,研究领域跨越零指代、代词指代、名词指代等,方法也不是很复杂,集中于规则+特征+模型的传统思路。国内集中在苏州大学周国栋老师带领的团队和刘挺、秦兵老师带领的团队,分别在Berkeley Parser、LTP基础上做了一些研究,但是遗憾的是,近年来国内学者好像没有顶会命中记录。

    鉴于当前国内的指代消解工具基本上没有开源、同时效果还说得过去的,所以经过大量调研当前中文指代消解的现状后,最终确定了使用Stanford CoreNLP作为实验对象。

    Stanford CoreNLP 是斯坦福NLP组开源的一套集分词、词性标注、命名实体识别、句法分析、情感分析、指代消解等NLP功能的软件套装,支持英文、中文等语言。

    附这个妇孺皆知的tools的链接:http://nlp.stanford.edu/software/index.shtml 和 http://stanfordnlp.github.io/CoreNLP/index.html  

    它的官方Demo链接:http://nlp.stanford.edu:8080/corenlp/ (这个DEMO对应的后台,应该是使用的英文模型)

    好了 闲话不多说,我们快快步入正题,如何调用Stanford CoreNLP 3.6.0 套装中的中文指代消解模块

    =========================================================================

    =========================================================================

    1、下载 Stanford CoreNLP 3.6.0 源码+模型,500M+,但是里面的cws、pos、parse等模型都是英文的 (http://stanfordnlp.github.io/CoreNLP/download.html)

    2、下载中文模型,分词、词性标注、NER、parser等。(我不确定有没有统一下载地址,我是一个一个点开,找到Chinese Model,下载的 http://nlp.stanford.edu/software/index.shtml)

    3、跑测试代码,在 http://stanfordnlp.github.io/CoreNLP/coref.html 里面找到运行方法,jar包调用分文件方式 或者 java代码调用分句子方式。(注意另外一个页面 http://nlp.stanford.edu/software/dcoref.shtml 中的方法是错误的,实践中有bug跑不通)

    4、在3中找到了对的接口,实践中jar包调用来处理文件的方法,是可以在中文语料上跑通的,但是3中贴的代码仍然面向的是英文语料。这时需要对其进行修改。

    代码如下:

    import edu.stanford.nlp.hcoref.CorefCoreAnnotations;
    import edu.stanford.nlp.hcoref.data.CorefChain;
    import edu.stanford.nlp.hcoref.data.Mention;
    import edu.stanford.nlp.ling.CoreAnnotations;
    import edu.stanford.nlp.pipeline.Annotation;
    import edu.stanford.nlp.pipeline.StanfordCoreNLP;
    import edu.stanford.nlp.util.CoreMap;
    import edu.stanford.nlp.util.StringUtils;
    
    import java.util.Properties;
    
    public class CorefExample {
        public static void main(String[] args) throws Exception {
            long startTime=System.currentTimeMillis();
            
            String text = "小明吃了个冰棒,它很甜。 ";
            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("	" + 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("	" + m);
                }
            }
            
            long endTime=System.currentTimeMillis(); 
            long time = (endTime-startTime)/1000;
            System.out.println("Running time "+time/60+"min "+time%60+"s");
        }
    }

    那么里面的zh-coref-default.properties 为啥CoreNLP里面没有呢。。。最后在stanford-chinese-corenlp-2015-12-08-models.jar解压后对应目录下找到了这个文件,与它官方网页里面虽然只相差一行(具体哪一行,大家可以对比看看),但是没有那个属性,真的跑不通。

  • 相关阅读:
    数据结构(2)
    python数据结构(1)
    python 中__getitem__ 和 __iter__ 的区别
    python 中的 %s,%r,__str__,__repr__
    python中的zip
    python反射,单例模式
    类python中高级用法
    python中super与成员属性
    python 类与对象解析
    【其他】BootCDN
  • 原文地址:https://www.cnblogs.com/zklidd/p/5081677.html
Copyright © 2011-2022 走看看