这个thread包含了一个很好的例子,说明了如何为Stanfords CoreNLP库使用包装器。下面是我正在使用的示例:
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
res = nlp.annotate("I love you. I hate him. You are nice. He is dumb",
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
for s in res["sentences"]:
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))假设我有+10000个句子想要分析,就像这个例子一样。有没有可能并行和多线程处理这些东西?
发布于 2019-02-21 01:46:59
对这种方法不太确定。在java中,我用corenlp和我想使用的管道设置了单例类。然后我在单例上调用一个方法,多个线程使用同一个实例,它接受几个句子,对它们进行注释,并对结果进行一些处理。因此,这种类型的多线程确实有效。我已经这样做了几年了,没有任何问题。
你能重构你的代码来做到这一点吗?那么设置你的流水线,然后用你的线程池一次对几个句子调用annotate?应该不会太费力。
希望这是有意义的。
https://stackoverflow.com/questions/54733059
复制相似问题