【发布时间】:2016-03-24 17:29:05
【问题描述】:
我使用以下命令运行Stanford CoreNLP Server:
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer
我尝试解析句子Who was Darth Vader’s son?。请注意,Vader 后面的撇号不是 ASCII 字符。
online demo成功解析句子:
我在 localhost 上运行的服务器失败:
我也尝试使用 Python 执行查询。
import requests
url = 'http://localhost:9000/'
sentence = 'Who was Darth Vader’s son?'
r=requests.post(url, params={'properties' : '{"annotators": "tokenize,ssplit,pos,ner", "outputFormat": "json"}'}, data=sentence.encode('utf8'))
tree = r.json()
最后一条命令引发异常:
ValueError: Invalid control character at: line 1 column 1172 (char 1171)
但是,我注意到文本中出现了字符\x00(即r.text)。如果我删除它们,则 json 解析成功:
import json
tree = json.loads(r.text.replace('\x00', ''))
最后,r.encoding 是ISO-8859-1,即使我没有使用选项-strict 来运行服务器。请注意,如果我手动将其替换为 UTF-8,它不会改变任何内容。
如果我运行相同的代码将url = 'http://localhost:9000/' 替换为url = 'http://corenlp.run/',那么一切都会成功。调用r.json()返回一个dict,r.encoding确实是UTF-8,并且文本中没有字符\x00。
我运行的 CoreNLP 服务器出了什么问题?
【问题讨论】:
标签: utf-8 server stanford-nlp