Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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 解决列表项在文本中多次出现时的算法错误_Python_Python 2.7 - Fatal编程技术网

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

Python 解决列表项在文本中多次出现时的算法错误,python,python-2.7,Python,Python 2.7,此函数必须检测文本中的否定词,并在否定词后向该词添加NEG_uu前缀。逻辑是将否定词的索引保存在文本列表中,然后将NEG_前缀添加到(索引+1) 问题是,例如,当文本有多个“not”时,它就不能正常工作 def negationDetection(tweet): position = [] words = tweet.split() #to prevent error when negation word appears at the end of text size = len(words)-

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

问题是,例如,当文本有多个“not”时,它就不能正常工作

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_NEG_很好,但我感觉不到

而不是

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

如何修复此错误

您的错误在:

position.append(words.index(word) + 1)
您可以使用
索引获得单词的位置,在本例中为“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
您的bug存在于:

position.append(words.index(word) + 1)
您可以使用
索引获得单词的位置,在本例中为“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