我发现在新输入到来时重启解析器效率很低,所以我想以交互方式运行解析器 - 从stdin读取输入并将结果打印到stdout。但是,官方网站Can I have the parser run as a filter?上的说明似乎与选项不兼容(例如,-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))
我觉得你有点困惑。 CoreNLP和Stanford Parser都可以选择作为命令行过滤器运行,从stdin读取并写入stdout。但是,只有CoreNLP单独提供Web服务实现。
像port
这样的选项只对后者有意义。
所以,目前,我同意你有一个有效的用例(想要输入预先标记的文本)但目前没有web服务支持。最简单的前进方法是为解析器编写一个简单的Web服务实现。对我们来说,它可能会在某个时候发生,但还有其他一些当前的优先事项。欢迎任何人写一个。 :)