Python:在scrip中打开treetagger

2024-06-01 09:48:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python脚本中使用^{}?在

我有一个句子,treetagger应该分析它。在normal命令行中,我可以执行以下操作:

echo 'This is a test!' | cmd/tree-tagger-english-utf8  

但是如何在python脚本中实现这一点呢?在

上述命令的输出如下:

^{pr2}$

在我的脚本中,我需要标记,即“DT”,“VBZ”,“DT”,“NN”,“SENT”,我想保存在列表中。我需要这些标签稍后插入字符串。在

谢谢你的帮助!:)


Tags: 命令行testecho脚本cmdtreeenglishis
2条回答

看看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
$ 

您还可以使用miotto's treetagger-python module,它为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'?']]

Here is a blog post I made detailing installation and testing,如果您需要进一步的说明。在

相关问题 更多 >