我正在使用斯坦福 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/