当列表项在文本中出现多次时,解决算法错误

时间:2016-02-20 01:14:52

标签: python python-2.7

此函数必须检测文本中的否定词,并在否定词后添加NEG_前缀。逻辑是在文本列表中保存否定词的索引,然后将NEG_prefix添加到(索引+ 1)

问题在于,当文本例如多于一个“不”时,它无法正常工作。

def negationDetection(tweet):
position = []
words = tweet.split()
#to prevent error when negation word appears at the end of text
size = len(words)-1 
print words
negationList = ["not","no","never"]
for word in words:
    if word in negationList:
        if words.index(word) != size:
            position.append(words.index(word) + 1)
        else:
            continue
    else:
        continue
print position
for i in position:
    tweet = (tweet).replace(words[i], 'NEG_' + words[i])
return tweet
a = "hello I am not good,but I can never feel it"
print negationDetection(a)

结果是

  

你好,我不是NEG_good,但我永远不会NEG_feel

这是正确的,但是当文字是“你好我不好,但我感觉不到”时,结果是

  

你好我不是NEG_NEG_good,但我感觉不到

而不是

  

你好我不是NEG_good,但我不能NEG_feel

如何修复此错误?

1 个答案:

答案 0 :(得分:2)

你的错误在:

position.append(words.index(word) + 1)

使用index获得单词的位置,在本例中为“not”。这总是返回单词的第一次出现。一种更简单的方法是迭代索引,而不是迭代单词。

negationList = ["not","no","never"]
for word in range(len(words)):
    if words[word] in negationList:
        if word != size:
            position.append(word + 1)
        else:
            continue
    else:
        continue