将下载的字符串与Python中的列表进行比较

时间:2014-03-31 19:06:57

标签: python comparison analysis

我试图在Python中创建情感分析器,下载文本并根据负面和正面词汇列表进行分析。对于文本中的每个匹配, poswords.txt 中的单词应该有+1分数,对于 negwords.txt 中文本中的每个匹配,都应该是一个-1分数,文本的总分将是情绪分数。这就是我试图这样做的方法,但我只是得到0分。

以下答案似乎不起作用,我的情绪评分为0。

 split = text.split()
    poswords = open('poswords.txt','r')
    for word in split:
        if word in poswords:
            sentimentScore +=1
    poswords.close()

    negwords = open('negwords.txt','r')
    for word in split:
        if word in negwords:
            sentimentScore -=1
    negwords.close()

1 个答案:

答案 0 :(得分:1)

代码中的

poswordsnegwords只是文件句柄,您没有读取这些文件中的单词。

下面:

split = text.split()
poswords = open('poswords.txt','r')
pos = []
for line in poswords:
    pos.append(line.strip())
for word in split:
    if word in pos:
        sentimentScore +=1
poswords.close()

negwords = open('negwords.txt','r')
neg = []
for line in negwords:
    neg.append(line.strip())
for word in split:
    if word in neg:
        sentimentScore -=1
negwords.close()

如果文件很大,以上不是最佳解决方案。为正面和负面单词创建字典:

input_text = text.split() # avoid using split as a variable name, since it is a keyword
poswords = open('poswords.txt','r')
pos_dict = defaultdict(int)
for line in poswords:
    pos_dict[line.strip()] += 1
poswords.close()

negwords = open('negwords.txt','r')
neg_dict = defaultdict(int)
for line in negwords:
    neg_dict[line.strip()] += 1
negwords.close()

sentiment_score = 0
for word in input_text:
    if word in pos_dict:
        sentiment_score += 1
    elif word in neg_dict:
        sentiment_score -=1