如何在python中使用treetagger?
我有一个句子,treetagger应该分析它。在normal命令行中,我可以执行以下操作:
echo 'This is a test!' | cmd/tree-tagger-english-utf8 但是我如何在python脚本中做到这一点呢?
上述命令的输出如下:
echo 'This is a test!' | cmd/tree-tagger-english
reading parameters ...
tagging ...
finished.
This DT this
is VBZ be
a DT a
test NN test
! SENT !在我的脚本中,我需要标记,即"DT","VBZ","DT","NN",“已发送”,我想把它们保存在一个列表中。稍后我需要这些标记将它们插入到字符串中。
(谢谢你的帮助!)
发布于 2014-05-28 15:34:06
查看子过程模块:下面是一个简单的示例..。
$ cat test.py
#!/usr/bin/python
import os
import sys
import subprocess
list_of_lists = []
process = subprocess.Popen(["cmd/tree-tagger-english-utf8"], stdout=subprocess.PIPE)
(output, err) = process.communicate(sys.stdin)
count = 0
for line in output.split('\n'):
# condition to skip the first 3 lines
if count<3:
count=count+1
else:
new_list = [elem for elem in line.split()]
list_of_lists.append(new_list)
exit_code = process.wait()
print list_of_lists
$ 发布于 2015-10-15 23:41:11
您还可以使用miotto的treetagger-python模块,它为TreeTagger提供了一个非常容易使用的接口.
只需确保定义一个新的TREETAGGER环境变量,以便Python模块知道在哪里找到TreeTagger可执行文件。其余的看起来都是这样的:
>>> from treetagger import TreeTagger
>>> tt_en = TreeTagger(encoding='utf-8', language='english')
>>> from pprint import pprint
>>> pprint(tt_en.tag('Does this thing even work?'))
[[u'Does', u'VBZ', u'do'],
[u'this', u'DT', u'this'],
[u'thing', u'NN', u'thing'],
[u'even', u'RB', u'even'],
[u'work', u'VB', u'work'],
[u'?', u'SENT', u'?']]这是我写的一篇博客文章,详细介绍了安装和测试,如果你需要更多的建议。
https://stackoverflow.com/questions/23915166
复制相似问题