Python 有没有办法让pycorenlp';s`nlp.annotate()`是否总是返回相同类型的结果?

Python 有没有办法让pycorenlp';s`nlp.annotate()`是否总是返回相同类型的结果?,python,stanford-nlp,Python,Stanford Nlp,我正在尝试运行以标记包含非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('ht

我正在尝试运行以标记包含非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'>
有没有办法让
nlp.annotate()
始终返回相同类型的结果


该系统是通过以下方式启动的:

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer 9000
我在Windows 7 SP1 x64 Ultimate上使用斯坦福CoreNLP 3.6.0、pycorenlp 0.3.0和python 3.5 x64。

快速修复:

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
,因为

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)