Python 遍历列表列表并使用不同的列表计算匹配项
我是python新手,目前正在为我的硕士论文进行情绪分析。然而,有一个问题,我目前正在研究,我真的不知道如何解决它 我需要在包含单词BLA的字符串中找到一个句子,然后将该句子中的每个单词与我的积极词和消极词词典进行比较。如果否定词多于肯定词,计数器应执行+1。最后,我会说:在文件1中,有4个否定句,包括BLA这个词 到目前为止,我使用正则表达式删除了所有不包含BLA这个词的句子。然后,我将句子中的单词分开,并创建了一个列表。它看起来像这样: [['we'、'sberform'、'because'、'of'、'BLA']、['BLA'、'is'、'bad']、['BLA'、'is'、'good'] 现在,我想把每一个单词与字典中的否定词和肯定词进行比较。因为我需要找出包含BLA这个词的句子是肯定的还是否定的,所以在转移到第二个列表之前,我只在列表中的一个列表中计算这个词是很重要的 这个例子的结果应该是2,因为2个句子是否定的,1个是肯定的 在其他情况下,我只在文本中查找否定词,我这样做:Python 遍历列表列表并使用不同的列表计算匹配项,python,list,loops,frequency,sentiment-analysis,Python,List,Loops,Frequency,Sentiment Analysis,我是python新手,目前正在为我的硕士论文进行情绪分析。然而,有一个问题,我目前正在研究,我真的不知道如何解决它 我需要在包含单词BLA的字符串中找到一个句子,然后将该句子中的每个单词与我的积极词和消极词词典进行比较。如果否定词多于肯定词,计数器应执行+1。最后,我会说:在文件1中,有4个否定句,包括BLA这个词 到目前为止,我使用正则表达式删除了所有不包含BLA这个词的句子。然后,我将句子中的单词分开,并创建了一个列表。它看起来像这样: [['we'、'sberform'、'because'
# Reset the number of negative words to zero
negative_count=0
# For each negative word, count the number of occurrences
for j in range(len(negative_words)):
negative_count=negative_count+text_devided.count(negative_words[j])
因此,我可能会这样做,但在一个循环中,通过列表
如果你知道如何以不同的方式解决这个问题,我也愿意接受。我想你是说用你的字典 …每一个单词都有否定词和肯定词词典 python列表。
为此,我将:
list_with_sentences = [['we', 'underperform', 'because', 'of', 'BLA'], ['BLA', 'is', 'bad'], ['BLA', 'is', 'good']]
pos_words = 0
neg_words = 0
total_neg_count = 0
for sentence in list_with_sentences:
for word in sentence:
for item in dictonary_pos_word:
if item == word:
pos_words = pos_words + 1
for item in dictonary_neg_word:
if item == word:
neg_words = neg_words + 1
if neg_words > pos_words:
total_neg_count = total_neg_count + 1
ls = [
['we', 'underperform', 'because', 'of', 'BLA'],
['BLA', 'is', 'bad'],
['BLA', 'is', 'good']
]
positive_words = ("good",)
negative_words = ("underperform", "bad")
for line in ls:
score = sum(map(lambda w: 1 if w in positive_words else -1 if w in negative_words else 0, line))
"""
Score < 0: Negative
Score > 0: Positive
Score = 0: Neutral or same number of positive/negative words
"""
print("Sentence:", " ".join(line))
print(" Score:", score)
print()
Sentence: we underperform because of BLA
Score: -1
Sentence: BLA is bad
Score: -1
Sentence: BLA is good
Score: 1