【问题标题】:UTF-8 issue with CoreNLP serverCoreNLP 服务器的 UTF-8 问题
【发布时间】: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.encodingISO-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


    【解决方案1】:

    这是 3.6.0 版本的一个已知错误。如果您从 GitHub 构建服务器,它应该可以正常使用 UTF-8 字符。在请求中设置适当的 Content-Type 标头也将解决此问题(请参阅https://github.com/stanfordnlp/CoreNLP/issues/125)。

    【讨论】:

      猜你喜欢
      • 2018-04-21
      • 2011-06-11
      • 2017-09-30
      • 2020-04-16
      • 1970-01-01
      • 2012-07-05
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多