我编写了以下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)]