Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Verbnet:vn.classids()返回2个列表,但我需要删除其中的1个_Python_Nlp_Nltk - Fatal编程技术网

Python Verbnet:vn.classids()返回2个列表,但我需要删除其中的1个

Python Verbnet:vn.classids()返回2个列表,但我需要删除其中的1个,python,nlp,nltk,Python,Nlp,Nltk,上面的代码将打印出来 metaphor1 = " I run a race" from nltk.corpus import verbnet as vn for word in metaphor1.split(): if vn.classids(word) != []: start = set(vn.classids(word)) print (start) 第一个是“run”的所有动词词义列表,但第二个列表给出了句子中使用的词义。我想删除第一个列表,

上面的代码将打印出来

metaphor1 = " I run a race" 
from nltk.corpus import verbnet as vn
for word in metaphor1.split():
    if vn.classids(word) != []:
        start = set(vn.classids(word))
        print (start)
第一个是“run”的所有动词词义列表,但第二个列表给出了句子中使用的词义。我想删除第一个列表,并尝试使用

['meander-47.7', 'preparing-26.3-1', 'run-51.3.2', 'swarm-47.5.1-1']
['run-51.3.2']
然而,这又回来了

metaphor1 = " I run a race" 
from nltk.corpus import verbnet as vn

for word in metaphor1.split():
    if vn.classids(word) != []:
        start = set(vn.classids(word))
        remove = set(vn.classids('run'))
        final = list(set(vn.classids(word))-set(vn.classids('run')))
        print (final)

到底是什么导致了这个问题,我该如何解决它

循环返回一个空列表,
[]
,因为集合与自身的对称差异是一个空集合

您想删除synset的第三个元素,
run-51.3.2
。您错误地将变量
remove
分配给
run
的整个语法集,而不仅仅是一个元素

试试这个

[]
[]
我用
pos\u标记
作为显式动词。即使单词不在VerbNet数据库中,VerbNet也可能返回空列表,即使单词用作动词

我使用了
word\u tokenize
使代码更加通用

代码返回一个列表。如果只想将变量打印到控制台,请将
return
更改为
print

import nltk
metaphor1 = STRING HERE
for word,pos in nltk.pos_tag(nltk.word_tokenize(metaphor1):
    if 'V in pos: #Another way to focus on only verbs
       return [sense for sense in vn.classids(word) if 'run' not in sense]