Lynten / stanford-corenlp

Python wrapper for Stanford CoreNLP.
MIT License
919 stars 200 forks source link

python可以添加自定义词典吗 #64

Open lishixin7 opened 5 years ago

lishixin7 commented 5 years ago

python可以添加自定义词典吗

jiangzhonglian commented 5 years ago

同问

skywalker-young commented 5 years ago

留名同问。。。

Lijiachen1018 commented 4 years ago

我尝试过自定义词典,但是没有效果

dashu101 commented 1 week ago

我尝试过自定义词典,但是没有效果

  • [x] 参考文档,感谢前人探路

  • [x] 下载

    • [x] CoreNLP的源码git
    • [x] 下载chinese模型jar包,解压到src
  • [x] 我用的Eclipse,用ant导入build.xml,Eclipse-File-New-Other-Java-Java Project from Existing Ant Buildfile
  • [x] 启动服务,总是没法找到standordCoreNLP-chinese.properties

    • [x] Configuration设置参数-serverProperties edu/stanford/nlp/pipeline/StanfordCoreNLP-chinese.properties
    • [x] 修改IOUtils.java 472行getInputStreamFromURLOrClasspathOrFileSystem
      try { 
      String base = IOUtils.class.getResource("/").getPath();
      if (textFileOrUrl.indexOf("/")!=0) {
      textFileOrUrl = base +textFileOrUrl;//资源文件根目录+需要寻找到文件位置
      }
      in = findStreamInClasspathOrFileSystem(textFileOrUrl);
      }
    • [x] debug后发现并没有载入properties中的模型,修改 中的78行StanfordCoreNLPServer.java 这样启动服务的时候就会载入模型
      protected static String preloadedAnnotators = "tokenize, ssplit, pos, lemma, ner, depparse, coref, natlog, openie";
    • [x] 从edu.stanford.nlp.pipeline 运行 StanfordCoreNLPServer.java,localhost:9000
  • [x] 加入自定义词语,重新生成词典

    • [x] 下载Chinese word segmenter,解压
    • [x] 添加词典
      java -cp "*" -mx1g edu.stanford.nlp.wordseg.ChineseDictionary -inputDicts my_dict.txt,dict-chris6.ser.gz -output my_new_dict.ser.gz
  • [x] jar包
    java -Xmx8g -cp "*" -Djava.io.tmpdir=/path/to/a/tmp_dir edu.stanford.nlp.pipeline.StanfordCoreNLPServer -serverProperties StanfordCoreNLP-chinese.properties -port 6666

不一定是没有效果,你可以尝试创建一些比较生僻的词语再试一试。因为内部应该是先匹配短词语后分词的