如何使用 treetagger在 python 脚本中?
我给了一个句子,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”、“SENT”。稍后我需要这些标签以将它们插入字符串中。
感谢您的帮助! :)
请您参考如下方法:
查看subprocess模块:下面是一个简单的例子......
$ 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
$
