Мне показалось неэффективным перезагружать анализатор при поступлении нового ввода, поэтому я хотел бы запустить анализатор в интерактивном режиме - прочитать ввод из stdin и распечатать результат в stdout. Однако инструкция, приведенная на официальном сайте Можно ли запустить анализатор в качестве фильтра? кажется несовместимым с параметрами (например, -port
).
Я знаю, что CoreNLP можно запустить как сервер, но он не может получать текст с тегами POS в качестве входных данных, поэтому я не буду его использовать.
Вот что я пытаюсь:
class myThread(threading.Thread):
def __init__(self,inQueue,outQueue):
threading.Thread.__init__(self)
self.cmd=['java.exe',
'-mx4g',
'-cp','*',
'edu.stanford.nlp.parser.lexparser.LexicalizedParser',
'-model', 'edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz',
'-sentences', 'newline',
'-outputFormat', 'conll2007',
'-tokenized',
'-tagSeparator','/',
'-tokenizerFactory', 'edu.stanford.nlp.process.WhitespaceTokenizer',
'-tokenizerMethod', 'newCoreLabelTokenizerFactory',
'-encoding', 'utf8']
self.subp=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
self.inQueue=inQueue
self.outQueue=outQueue
def run(self):
while True:
rid,sentence=self.inQueue.get()
print(u"Receive sentence %s"%sentence)
sentence=sentence.replace("\n","")
self.subp.stdin.write((sentence+u'\n').encode('utf8'))
self.subp.stdin.flush()
print("start readline")
result=self.subp.stdout.readline()
print("end readline")
print(result)
self.outQueue.put((rid,result))