gpt4 book ai didi

python - Subprocess.Popen 中的 Python 程序错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:37 25 4
gpt4 key购买 nike

我编写了以下Python程序,其中包括包装函数lesk_similarity,以使用Perl程序计算相似度分数。 GetMAxSim 函数根据两个合成物的组合数量返回最大相似度得分。

import nltk
from nltk.corpus import wordnet
import subprocess

def lesk_similarity(a,b):
cmd = ['perl','./sample.pl',a,b]
proc = subprocess.Popen(cmd,stdout = subprocess.PIPE)
for line in proc.stdout:
if 'Similarity' in line:
similarity = int(line.split('=')[-1])
return similarity

def getMaxSim(synsets1, synsets2):
maxSim = None
for s1 in synsets1:
for s2 in synsets2:
sim = lesk_similarity(s1,s2)
if maxSim == None or maxSim < sim:
maxSim = sim
s3 = s1
s4 = s2

print s3.definition
print s4.definition
return maxSi

ChurchSynsets = wordnet.synsets("church", pos="n")
TempleSynsets = wordnet.synsets("temple", pos="n")
print "create<>make:", getMaxSim(ChurchSynsets, TempleSynsets)

churchSynset 和 Templesynset 包含以下数据:

churchsynset[Synset('church.n.01'),Synset('church.n.02'),Synset('church_service.n.01'), Synset('church.n.04')]寺庙同义词集 [Synset('temple.n.01'), Synset('temple.n.02'), Synset('temple.n.03'), Synset('synagogue.n.01')]

运行此代码后出现以下错误:

madhusudan@maddy:~/WordNet-Similarity-2.05/samples$ python test4.py
create<>make:
Traceback (most recent call last):
File "test4.py", line 40, in <module>
print "create<>make:", getMaxSim(ChurchSynsets, TempleSynsets)
File "test4.py", line 17, in getMaxSim
sim = lesk_similarity(s1,s2)
File "test4.py", line 7, in lesk_similarity
proc = subprocess.Popen(cmd,stdout = subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings

请帮我解决这个问题。

最佳答案

尝试更改此行 cmd = ['perl','./sample.pl',a,b]

致 cmd = ['perl','./sample.pl', str(a), str(b)]

关于python - Subprocess.Popen 中的 Python 程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21823057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com