我正在使用UDPipe模型进行文本标记化和柠檬化。我可以通过使用!echo命令或将其打印到文件中来完成任务本身,但是我希望生成一个Python结构来进一步处理输出。
什么起作用
这是我的工作命令:
!echo 'the text I'm processing' | ./udpipe --tokenize --tag './path/to/my/model'退出:
Loading UDPipe model: done.
newdoc
newpar
sent_id = 1
text = прывітанне, сусвет
1 прывітанне прывітанне NOUN NN Animacy=Inan|Case=Nom|Gender=Neut|Number=Sing _ _ _ SpaceAfter=No
2 , , PUNCT PUNCT _ _ _ _ _
3 сусвет сусвет NOUN NN Animacy=Inan|Case=Nom|Gender=Masc|Number=Sing _ _ _ SpacesAfter=\n这用于将输出打印到文件中:
!echo 'the text I'm processing' | ./udpipe --tokenize --tag './path/to/my/model' >> filename.txt./udpipe是包的克隆存储库。
我尝试过的(但没有成功)
os.system()
import os
text = 'the text I'm processing'
cmd = "echo '{}' | ./udpipe --tokenize --tag './path/to/my/model'".format(text)
os.system(cmd)
Out: 0subprocess.getoutput()
import subprocess
cmd = "'the text I'm processing' | ./udpipe --tokenize --tag './path/to/my/model'"
output = subprocess.getoutput(cmd, stdout=subprocess.PIPE, shell=True)
print(output)
TypeError: getoutput() got an unexpected keyword argument 'stdout'发布于 2022-05-26 16:05:09
您已经做了一些研究,并发现了subprocess模块,这是从Python调用进程的最常见方式。如果您想使用shell的功能(例如管道),则需要将参数shell=True传递给任何实际调用进程的函数,例如subprocess.Popen(),基本进程。
from subprocess import Popen, PIPE
text = "the text I'm processing"
cmd = "echo", text, "|", "./udpipe", "--tokenize", "--tag", "./path/to/my/model"
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, text=True, shell=True)
output, _ = proc.communicate()
print(output)在您的示例中,您还使用了>>将输出追加到文件中,因此不会产生输出,您只需等待进程结束:
from subprocess import Popen
text = "the text I'm processing"
cmd = "echo", text, "|", "./udpipe", "--tokenize", "--tag", "./path/to/my/model", ">>", "filename.txt"
proc = Popen(cmd, shell=True)
proc.wait()或者您可以应用高级函数subprocess.call()。
from subprocess import call
text = "the text I'm processing"
cmd = "echo", text, "|", "./udpipe", "--tokenize", "--tag", "./path/to/my/model", ">>", "filename.txt"
call(cmd, shell=True)如果要在代码中获得进程输出,可以使用另一个更高级别的函数subprocess.check_output()。
from subprocess import check_output
text = "the text I'm processing"
cmd = "echo", text, "|", "./udpipe", "--tokenize", "--tag", "./path/to/my/model"
output = check_output(cmd, text=True, shell=True)
print(output)但是!您也可以使用python功能。例如,使用Popen(),您可以将输入传递给进程,并(如果需要)直接将其重定向到文件:
from subprocess import Popen, PIPE
text = "the text I'm processing"
cmd = "./udpipe", "--tokenize", "--tag", "./path/to/my/model"
proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
output, _ = proc.communicate(input=text)
print(output)
# OR write to file directly
with open("filename.txt", "a+") as out:
proc = Popen(cmd, stdin=PIPE, stdout=out, stderr=out, text=True)
proc.communicate(input=text)高级check_output()也是如此
from subprocess import check_output, STDOUT
text = "the text I'm processing"
cmd = "./udpipe", "--tokenize", "--tag", "./path/to/my/model"
output = check_output(cmd, input=text, stderr=STDOUT, text=True)
print(output)最后一个选项是我会使用的,但你可以应用你最喜欢的一个。
你可以帮助我的国家,检查。
https://stackoverflow.com/questions/72383503
复制相似问题