【问题标题】:Python IF statement with nltk.wordnet.synsets带有 nltk.wordnet.synsets 的 Python IF 语句
【发布时间】:2013-03-12 16:34:43
【问题描述】:
import nltk
from nltk import *
from nltk.corpus import wordnet as wn

output=[]
wordlist=[]

entries = nltk.corpus.cmudict.entries()

for entry in entries[:200]: #create a list of words, without the pronounciation since.pos_tag only works with a list
    wordlist.append(entry[0])

for word in nltk.pos_tag(wordlist): #create a list of nouns
    if(word[1]=='NN'):
        output.append(word[0])

for word in output:
    x = wn.synsets(word) #remove all words which does not have synsets (this is the problem)
    if len(x)<1:
        output.remove(word)

for word in output[:200]:
    print (word," ",len(wn.synsets(word)))

我正在尝试删除所有没有同义词集的单词,但由于某种原因它不起作用。运行程序后,我发现即使一个词的 len(wn.synsets(word)) = 0,它也没有从我的列表中删除。谁能告诉我出了什么问题?

【问题讨论】:

    标签: python nltk wordnet


    【解决方案1】:

    您不能在遍历列表的同时删除当前项目。这是一个演示问题的玩具示例:

    In [73]: output = range(10)
    
    In [74]: for item in output:
       ....:     output.remove(item)
    

    您可能希望删除output 中的所有项目。但其中一半仍然存在:

    In [75]: output
    Out[75]: [1, 3, 5, 7, 9]
    

    为什么不能同时循环和删除:

    想象一下 Python 使用内部计数器来记住当前项目的索引,因为它通过 for-loop

    当计数器等于 0 时(第一次通过循环),Python 执行

    output.remove(item)
    

    很好。 output 中现在少了一项。但随后 Python 将计数器增加到 1。所以 word 的下一个值是output[1], 这是原始列​​表中的第三个​​项。

    0  <-- first item removed
    1  <-- the new output[0] ** THIS ONE GETS SKIPPED **
    2  <-- the new output[1] -- gets removed on the next iteration 
    

    (解决方法)解决方案:

    相反,要么迭代output 的副本,要么构建一个新列表。在这种情况下,我认为建立一个新列表更有效:

    new_output = []
    for word in output:
        x = wn.synsets(word) 
        if len(x)>=1:
            new_output.append(word)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多