我试图运行吡咯烷酮来标记包含非ASCII字符的文本。有时nlp.annotate()返回字典,有时返回字符串。
例如,
'''
From https://github.com/smilli/py-corenlp/blob/master/example.py
'''
from pycorenlp import StanfordCoreNLP
import pprint
import re
if __name__ == '__main__':
nlp = StanfordCoreNLP('http://localhost:9000')
text = u"tab with good effect, denies pain".encode('utf-8')
print('type(text): {0}'.format(type(text)))
output = nlp.annotate(text, properties={
'annotators': 'tokenize,ssplit',
'outputFormat': 'json'
})
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(output)
print('type(output): {0}'.format(type(output)))
text = u"tab with good effect\u0013\u0013, denies pain".encode('utf-8')
print('\ntype(text): {0}'.format(type(text)))
output = nlp.annotate(text, properties={
'annotators': 'tokenize,ssplit',
'outputFormat': 'json'
})
print('type(output): {0}'.format(type(output)))产出:
type(text): <type 'str'>
type(output): <type 'dict'>
type(text): <type 'str'>
type(output): <type 'unicode'>我注意到,当type(output)是<type 'unicode'>时,在斯坦福CoreNLP服务器上会收到以下警告:
WARNING: Untokenizable: ‼ (U+13, decimal: 19)是否有任何方法让nlp.annotate()总是返回相同类型的结果?
斯坦福CoreNLP服务器是通过以下方式启动的:
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer 9000我使用斯坦福CoreNLP 3.6.0、pycorenlp0.3.0和python3.5 x64在Windows7 SP1 x64终极版上。
发布于 2017-01-25 23:21:52
快速解决办法:
import json
# to place right after `output = nlp.annotate(text, properties={…})`
if type(output) is str or type(output) is unicode:
output = json.loads(output, strict=False)我使用strict=False是因为ValueError: Invalid control character at: line 1 column 33 (char 33)。
https://stackoverflow.com/questions/39605817
复制相似问题