从文本文件中的单词列表搜索

时间:2014-03-10 09:14:14

标签: python

我正在尝试编写一个程序来读取文本文件,然后将其分类为其中的注释是正面,负面还是中性。我已经尝试过各种方法来做到这一点,但每次都无济于事。我可以搜索1个单词而没有任何问题,但不止于此,它不起作用。另外,我有一个if语句,但我不得不在它下面使用两次因为它不允许我使用elif。任何帮助我出错的地方都会非常感激。提前谢谢。

middle = open("middle_test.txt", "r")
positive = []
negative = []                                        #the empty lists
neutral = []

pos_words = ["GOOD", "GREAT", "LOVE", "AWESOME"]    #the lists I'd like to search
neg_words = ["BAD", "HATE", "SUCKS", "CRAP"]

for tweet in middle:
    words = tweet.split()
    if pos_words in words:                           #doesn't work
        positive.append(words)        
    else:                                            #can't use elif for some reason
        if 'BAD' in words:                           #works but is only 1 word not list
            negative.append(words)
        else:
            neutral.append(words)

5 个答案:

答案 0 :(得分:1)

使用Counter,请参阅http://docs.python.org/2/library/collections.html#collections.Counter

import urllib2
from collections import Counter
from string import punctuation

# data from http://inclass.kaggle.com/c/si650winter11/data
target_url = "http://goo.gl/oMufKm" 
data = urllib2.urlopen(target_url).read()

word_freq = Counter([i.lower().strip(punctuation) for i in data.split()])

pos_words = ["good", "great", "love", "awesome"]
neg_words = ["bad", "hate", "sucks", "crap"]

for i in pos_words:
    try:
        print i, word_freq[i]
    except: # if word not in data
        pass

[OUT]:

good 638
great 1082
love 7716
awesome 2032

答案 1 :(得分:0)

您可以使用以下代码计算段落中的正面和负面字数:

from collections import Counter

def readwords( filename ):
    f = open(filename)
    words = [ line.rstrip() for line in f.readlines()]
    return words

# >cat positive.txt 
# good
# awesome
# >cat negative.txt 
# bad
# ugly

positive = readwords('positive.txt')
negative = readwords('negative.txt')

print positive
print negative

paragraph = 'this is really bad and in fact awesome. really awesome.'

count = Counter(paragraph.split())

pos = 0
neg = 0
for key, val in count.iteritems():
    key = key.rstrip('.,?!\n') # removing possible punctuation signs
    if key in positive:
        pos += val
    if key in negative:
        neg += val

print pos, neg

答案 2 :(得分:0)

您没有从文件中读取行。而这一行

if pos_words in words:

我认为这是用单词检查列表[“GOOD”,“GREAT”,“LOVE”,“AWESOME”]。那就是你在列表中查找列表pos_words = [“GOOD”,“GREAT”,“LOVE”,“AWESOME”]。

答案 3 :(得分:0)

你有一些问题。首先,您可以创建从文件中读取注释的函数,并将注释分为单词。制作它们并检查它们是否按您的要求工作。然后主程序看起来像:

for comment in get_comments(file_name):
    words = get_words(comment)
    classified = False
    # at first look for negative comment
    for neg_word in NEGATIVE_WORDS:
        if neg_word in words:
            classified = True
            negatives.append(comment)
            break
    # now look for positive
    if not classified:
        for pos_word in POSITIVE_WORDS:
            if pos_word in words:
                classified = True
                positives.append(comment)
                break
    if not classified:
        neutral.append(comment)

答案 4 :(得分:0)

小心,open()返回一个文件对象。

>>> f = open('workfile', 'w')
>>> print f
<open file 'workfile', mode 'w' at 80a0960>

使用此:

>>> f.readline()
'This is the first line of the file.\n'

然后使用set intersection:

positive += list(set(pos_words) & set(tweet.split()))