python - Searching from a list of word to words in a text file -


i trying write program reads text file , sorts out whether comments in positive, negative or neutral. have tried sorts of ways each time no avail. can search 1 word no problems more , doesn't work. also, have if statement i've had use else twice underneath wouldn't allow me use elif. i'm going wrong appreciated. in advance.

middle = open("middle_test.txt", "r") positive = [] negative = []                                        #the empty lists neutral = []  pos_words = ["good", "great", "love", "awesome"]    #the lists i'd search neg_words = ["bad", "hate", "sucks", "crap"]  tweet in middle:     words = tweet.split()     if pos_words in words:                           #doesn't work         positive.append(words)             else:                                            #can't use elif reason         if 'bad' in words:                           #works 1 word not list             negative.append(words)         else:             neutral.append(words) 

use counter, see http://docs.python.org/2/library/collections.html#collections.counter:

import urllib2 collections import counter string import punctuation  # data 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) in data.split()])  pos_words = ["good", "great", "love", "awesome"] neg_words = ["bad", "hate", "sucks", "crap"]  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 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -