python - 斯坦福 CoreNLP tokenize.whitespace 属性不适用于中文

标签 python stanford-nlp

我正在使用斯坦福 CoreNLP 对预标记化的中文文本进行后标记和 NER,我阅读了官方文档 https://stanfordnlp.github.io/CoreNLP/tokenize.html ,表示 tokenize.whitespace 选项“如果设置为 true,则仅在遇到空格时分隔单词”。这正是我想要的。

但我使用 python、pycorenlp 与 CoreNLP Server 交互,对 java 一无所知。然后我读了 anwser How to NER and POS tag a pre-tokenized text with Stanford CoreNLP?并认为也许唯一要做的就是在我的请求后属性字典中添加 'tokenize.whitespace' = 'true' 和另一个属性,但它根本不起作用。我这样运行我的服务器:

java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -serverProperties StanfordCoreNLP-chinese.properties -port 9000 -timeout 150000

在我的 jupyter 笔记本中:

from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')

output = nlp.annotate('公司 作为 物联网 行业', properties={
    'annotators': 'pos,ner',
    'tokenize.whitespace': 'true', # first property
    'ssplit.eolonly': 'true', # second property
    'outputFormat': 'json'
})

for sentence in output['sentences']:
    print(' '.join([token['word'] for token in sentence['tokens']]))

给出:

公司 作为 物 联网 行业

CoreNLP 仍在标记“物联网”标记,就像我不添加这两个属性一样。然后我尝试创建一个 .properties 文件并在命令行上使用它而不是斯坦福CoreNLP-chinese.properties,但它也不起作用。在我的 test.properties 中:

tokenize.whitespace=true
ssplit.eolonly=true

然后我像这样运行服务器:

  java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -serverProperties 'test.properties' -port 9000 -timeout 150000

它仍然表现得好像我什么也没改变。有人知道我错过了什么吗?如有任何帮助,我们将不胜感激:)

最佳答案

终于解决了我自己的问题。

对中文文本使用 tokenize.whitespace=true 很棘手,似乎永远不起作用;相反,添加

'tokenize.language': 'Whitespace'

到您的属性字典或同等内容中,添加

tokenize.language: Whitespace

到您的 .properties 文件以正确完成工作。

该属性写在同一页https://stanfordnlp.github.io/CoreNLP/tokenize.html#options ,这是我之前没有注意到的。为什么它存在用于同一目的的两个属性有点令人困惑。

关于python - 斯坦福 CoreNLP tokenize.whitespace 属性不适用于中文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299170/

相关文章:

python - 寻找两个pytorch张量的非交集

python - 从消耗时间的项目和不消耗时间但仍需要绘制空间的项目生成基于时间线的线性表示

java - 斯坦福自然语言处理 : Keeping punctuation tokens?

python - 过滤斯坦福依存解析器输出

python - 在 NumPy 数组中搜索序列

python - 将 src 文件夹中的文件导入到 test 文件夹中

python - 即使有垫片,Tox 也找不到 python3.6。我的 pyenv 设置有什么问题?

java - 线程中出现异常 "main"java.lang.OutOfMemoryError : Java heap space not fixed

nlp - 从 TweetNLP 中的首字母缩略词中获取全文

python - 仅获取标记化句子作为 Stanford Core NLP 的输出