在特定单词之前,如何计算否定或肯定单词-Python中的情感分析?

时间:2019-02-19 20:41:30

标签: python nlp sentiment-analysis

我正在尝试计算列表中的否定词在特定词之前出现的次数。例如,“这台糟糕的笔记本电脑”。指定的单词是“ laptop”,我希望输出在Python中具有“ Terrible 1”。

def run(path):
    negWords={} #dictionary to return the count
    #load the negative lexicon
    negLex=loadLexicon('negative-words.txt')
    fin=open(path)

    for line in fin: #for every line in the file (1 review per line)
        line=line.lower().strip().split(' ')
        review_set=set() #Adding all the words in the review to a set

        for word in line: #Check if the word is present in the line
            review_set.add(word)  #As it is a set, only adds one time

        for word in review_set:
            if word in negLex:
                if word in negWords:
                    negWords[word]=negWords[word]+1
                else:
                    negWords[word] = 1

    fin.close()
    return negWords

if __name__ == "__main__": 
    print(run('textfile'))

2 个答案:

答案 0 :(得分:0)

您似乎想对连续单词进行功能检查,这是一种方法,condition将针对每个连续单词进行检查。

text = 'Do you like bananas? Not only do I like bananas, I love bananas!'
trigger_words = {'bananas'}
positive_words = {'like', 'love'}

def condition(w):
    return w[0] in positive_words and w[1] in trigger_words

for c in '.,?!':
    text = text.replace(c, '')

words = text.lower().split()

matches = filter(condition, zip(words, words[1:]))
n_positives = 0
for w1, w2 in matches:
    print(f'{w1.upper()} {w2} => That\'s positive !')
    n_positives += 1
print(f'This text had a score of {n_positives}')

输出:

LIKE bananas => That's positive !
LIKE bananas => That's positive !
LOVE bananas => That's positive !
3

奖金:

  1. 只需在检查3个单词的条件下将zip(w, w[1:])更改为zip(w, w[1:], w[2:]),即可搜索3个连续单词。

  2. 您可以通过执行以下操作获得反词典:

from collections import Counter
counter = Counter((i[0] for i in matches)) # counter = {'like': 2, 'love': 1}

答案 1 :(得分:0)

这应该可以满足您的需求,它使用set和交集来避免某些循环。步骤是-

  1. 获取该行中的否定词
  2. 检查每个单词的位置
  3. 如果该位置后的单词是“笔记本电脑”,请记录下来

请注意,这只会识别出某行中第一个出现否定词,因此“可怕的笔记本电脑”将不匹配。

from collections import defaultdict

def run(path):

    negWords=defaultdict(int)  # A defaultdict(int) will start at 0, can just add.

    #load the negative lexicon
    negLex=loadLexicon('negative-words.txt')
    # ?? Is the above a list or a set, if it's a list convert to set
    negLex = set(negLex)

    fin=open(path)

    for line in fin: #for every line in the file (1 review per line)
        line=line.lower().strip().split(' ')

        # Can just pass a list to set to make a set of it's items.
        review_set = set(line)

        # Compare the review set against the neglex set. We want words that are in
        # *both* sets, so we can use intersection.
        neg_words_used = review_set & negLex

        # Is the bad word followed by the word laptop?            
        for word in neg_words_used:
            # Find the word in the line list
            ix = line.index(word)
            if ix > len(line) - 2:
                # Can't have laptop after it, it's the last word.
                continue

            # The word after this index in the line is laptop.
            if line[ix+1] == 'laptop':
                negWords[word] += 1

    fin.close()
    return negWords

如果您只对“ laptop”一词之前的单词感兴趣,那么更明智的方法是查找“ laptop”一词,然后在此之前检查该单词是否为负数。下面的示例可以做到这一点。

  1. 在当前行中找到笔记本电脑
  2. 如果笔记本电脑不在一行中,或者是第一个单词,请跳过该行
  3. 在笔记本电脑前先找到单词,然后否定单词
  4. 如果您有匹配项,请将其添加到我们的结果中

这避免了查找与笔记本电脑无关的单词。

from collections import defaultdict

def run(path):

    negWords=defaultdict(int)  # A defaultdict(int) will start at 0, can just add.

    #load the negative lexicon
    negLex=loadLexicon('negative-words.txt')
    # ?? Is the above a list or a set, if it's a list convert to set
    negLex = set(negLex)

    fin=open(path)

    for line in fin: #for every line in the file (1 review per line)
        line=line.lower().strip().split(' ')

        try:
            ix = line.index('laptop')
        except ValueError:
            # If we dont' find laptop, continue to next line.
            continue

        if ix == 0:
            # Laptop is the first word of the line, can't check prior word.
            continue


        previous_word = line[ix-1]

        if previous_word in negLex:
            # Negative word before the current one.
            negWords[previous_word] += 1

    fin.close()
    return negWords