第五次作业

twitter情感分析

In [1]:
import nltk

#https://blog.csdn.net/zzulp/article/details/77150129

pos_tweets = [('I love this car', 'positive'),
    ('This view is amazing', 'positive'),
    ('I feel great this morning', 'positive'),
    ('I am so excited about the concert', 'positive'),
    ('He is my best friend', 'positive')]

neg_tweets = [('I do not like this car', 'negative'),
    ('This view is horrible', 'negative'),
    ('I feel tired this morning', 'negative'),
    ('I am not looking forward to the concert', 'negative'),
    ('He is my enemy', 'negative')]
In [2]:
tweets = []
for (words, sentiment) in pos_tweets + neg_tweets:
    words_filtered = [e.lower() for e in words.split() if len(e) >= 3]
    tweets.append((words_filtered, sentiment))
tweets[:2]
Out[2]:
[(['love', 'this', 'car'], 'positive'),
 (['this', 'view', 'amazing'], 'positive')]
In [3]:
test_tweets = [
    (['feel', 'happy', 'this', 'morning'], 'positive'),
    (['larry', 'friend'], 'positive'),
    (['not', 'like', 'that', 'man'], 'negative'),
    (['house', 'not', 'great'], 'negative'),
    (['your', 'song', 'annoying'], 'negative')]
In [4]:
# get the word lists of tweets
def get_words_in_tweets(tweets):
    all_words = []
    for (words, sentiment) in tweets:
        all_words.extend(words)
    return all_words

# get the unique word from the word list	
def get_word_features(wordlist):
    wordlist = nltk.FreqDist(wordlist)
    word_features = wordlist.keys()
    return word_features

word_features = get_word_features(get_words_in_tweets(tweets))
' '.join(word_features)
Out[4]:
'love this car view amazing feel great morning excited about the concert best friend not like horrible tired looking forward enemy'
In [5]:
def extract_features(document):
    document_words = set(document)
    features = {}
    for word in word_features:
        features['contains(%s)' % word] = (word in document_words)
    return features
In [6]:
training_set = nltk.classify.util.apply_features(extract_features,\
                                                 tweets)
classifier = nltk.NaiveBayesClassifier.train(training_set)
In [7]:
tweet_negative2 = 'Your song is annoying'
print (classifier.classify(extract_features(tweet_negative2.split())))
positive
In [8]:
from sklearn.svm import LinearSVC
import sklearn
nltk_classifiers = dir(sklearn)
for i in nltk_classifiers:
    if 'Classifier' in i:
        print(i)
print (nltk_classifiers)
['_ASSUME_FINITE', '__SKLEARN_SETUP__', '__all__', '__builtins__', '__cached__', '__check_build', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_contextmanager', 'base', 'clone', 'config_context', 'datasets', 'exceptions', 'externals', 'feature_extraction', 'feature_selection', 'get_config', 'linear_model', 'logger', 'logging', 'metrics', 'model_selection', 'neighbors', 'os', 'preprocessing', 'random_projection', 're', 'set_config', 'setup_module', 'svm', 'sys', 'utils', 'warnings']
In [9]:
from sklearn.svm import LinearSVC
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(LinearSVC())
svm_classifier = classif.train(training_set)
In [10]:
# Don’t be too positive, let’s try another example:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
negative
In [11]:
from sklearn.tree import DecisionTreeClassifier
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(DecisionTreeClassifier())
svm_classifier = classif.train(training_set)
In [12]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
positive
In [13]:
from sklearn.linear_model import LogisticRegression 
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(LogisticRegression())
svm_classifier = classif.train(training_set)
In [14]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
positive
In [15]:
from sklearn.naive_bayes import MultinomialNB
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(MultinomialNB())
svm_classifier = classif.train(training_set)
In [16]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
negative
In [17]:
from sklearn.neighbors import KNeighborsClassifier  
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(KNeighborsClassifier())
svm_classifier = classif.train(training_set)
In [18]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
positive
In [19]:
from sklearn.ensemble import RandomForestClassifier    
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(RandomForestClassifier())
svm_classifier = classif.train(training_set)
In [20]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
positive
In [21]:
from sklearn.ensemble import GradientBoostingClassifier 
from nltk.classify.scikitlearn import SklearnClassifier
classif = SklearnClassifier(GradientBoostingClassifier())
svm_classifier = classif.train(training_set)
In [22]:
tweet_negative2 = 'Your song is annoying'
print (svm_classifier.classify(extract_features(tweet_negative2.split())))
positive
In [23]:
with open("C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/happy.txt", 'r',encoding="utf8") as f:
    tweet_happy = f.readlines() 
f.close()  
In [24]:
tweet_happy
Out[24]:
["I am LOVIN my Life right about now! I'm loving the people God is placing in my life. #Happy&Focused! Striving to be the BEST WOMAN I can be!\n",
 "Happy St.Patrick's Day! Wasn't always easy to celebrate it \n",
 "no one understands how happy i am right now. (':\n",
 'AGH. DELETE DELETE DELETE. I JUST MEANT HAPPY. I AM NOT A STRIPPER. OH, GAWD.\n',
 'she feels as happy as i am. and i am happy to say that we r both blessed. been the best spring break ever,\n',
 "I am so happy, it's kind of unbelievable.\n",
 'Ignorance is bliss and i need that shyt...I am happy. Thats just the saddest lie. #Cudi\n',
 'WMYB on the radio and i am one happy person(: #thinkingpositive\n',
 "realize you fucking twat. I'm not dragging in a sad way. I am actually quite happy! And I wouldn't say I don't care for you.\n",
 'because of you  i am happy\n',
 'i am very happy today ........yuhuuuuuuuiiiiiiii\n',
 'I am soooo happy I got a chance to hang my boo @BlaqBetty2 last night!!! Its been way too long!!! Muah!\n',
 'I am so happy for you and your drivers. That was an amazing finish for the MRW teams today\n',
 '17 more days until Opening Day. To say that I am happy about this is an understatement. The #mlb2012 season will be the BEST one ever! @mlb\n',
 'I am happy for the friendships I am developing with some people on twisters\n',
 '" Convert Kudankulam plant into a gasoline-based one" says Wise-Admiral Ramdas. I am happy he is a \'former\' Naval Chief now.\n',
 'Today was a perfect  day with my raza I am happy\n',
 "i'm assuming it's your bday where you're at even if it's not where i am lol . Well HAPPY BIRTHDAY !\n",
 "I don't specifically know why I like this picture, but I do. I am happy. :)\n",
 'Ignorance is bliss and i need that shyt...I am happy. Thats just the saddest lie. #Cudi\n',
 'Happy Sunday. I went with blue eyes today.  http://t.co/x4Tqbs0y\n',
 'i am happy with jointwitte\n',
 "I'm am so happy the PTSD,ADHD, psycho is now following me. Does he still have a HIGHER SECURITY CLEARENCE than POTUS OBAMA?\n",
 "i'm assuming it's your bday where you're at even if it's not where i am lol . Well HAPPY BIRTHDAY !\n",
 'Dear #hiltongardeninn I am NOT happy with the fact that I got stuck with the room beside the ice machine. #goldmember\n',
 'I am so happy with my life!\n',
 "i'm pretty happy with where i am now.\n",
 'Happy Birthday Ton! I am still working hard at what we both started, getting our music out there. RIP Family http://t.co/jURzc6WH\n',
 'Jamie, your daughter is stunning. I am so happy for you girl.\n',
 'As happy as I am to see my frends I wish it was summer(:\n',
 "I am happy you're sober. :)\n",
 'I am brewing w envy. Ok fine. Happy for you. :/\n',
 "I still check on you from time to time. i'm glad you're happy. i really am.\n",
 'Jamie, your daughter is stunning. I am so happy for you girl.\n',
 "I am so happy, it's kind of unbelievable.\n",
 ":'D YUUUUSSSS lmao yall don't know how happy i am #sj3\n",
 'I said I will close the distance up. I did it, so glad to have you around. Happy Birthday baby ❤ http://t.co/919VOI5v\n',
 'Happy 16th baby girl ❤ I love you!!! @khamiltonnn  http://t.co/axejLg1X\n',
 'I am so happy I am your friend right now.\n',
 '*giggles and smiles brightly* I am glad we are both happy too. Life is good.\n',
 "I'm happy for him...really, I am. She's an amazing girl, and they deserve each other. He's happy & thats all that matters...right?.....\n",
 "Dearest one can't be replace, even you're annoys me. But you're the one I love MOST! Happy B'day Brenda http://t.co/SXFW7EwM\n",
 'This has been a good weekend! I am so happy that I am back with everyone... now for the busiest week of my life\n',
 "If you don't tweet for a while you can do 3 in a row!  I am super duper happy to be home with my sweetie!  He is the best! I love him!\n",
 'We finished our first season of @TheBEATDance & I am so happy & proud & thankful & overwhelmed & lots of other good stuff! So Amazing #2013\n',
 'I am  so happy because I�ve keep a $100,00 Starbucks GIFT CARD 4 Free - I get it here: http://t.co/en6kV48W\n',
 'Did I mention that I am sad I have to go back to work tomorrow....but happy I can just do my job for now on.....\n',
 'I am happy, I am successful, bein in my own living space, bein in the masters prog I chose for myself, a life full of positivity...yay me!\n',
 'I Am So HAPPY .\n',
 'Looking through old pics and realizing everything happens for a reason. So happy with where I am right now\n',
 "Disney doesn't have TLC l, therefore this can not be the happiest place on earth because I am not happy...\n",
 'I wish for a stupid dream for the rest of the week. Maybe a dream where I am President, no skip that. *paging* Happy Monday Twitter.☺☺\n',
 'I am sorry! Happy birthdax :)\n',
 'had a taste for Jims...& I just had the nastiest steak hoagy from @JimsSteakout...very disappointed, a waste of my $11..i am not happy!\n',
 'im so happy we have school tmrw because that means one day closer to summer?!(: am i right\n',
 '17 more days until Opening Day. To say that I am happy about this is an understatement. The #mlb2012 season will be the BEST one ever! @mlb\n',
 'haha, i am def happy about tanning lol. it is such a stress reliever.\n',
 'Oo I think no licking my lolly sounds good. I am happy to hear you are getting a little better!!\n',
 'patamon : yayyy!! I am so happy, now I can play games with you!! pata~\n',
 "As long as she's happy then so am I.. My life <3  http://t.co/ZqqqbEih\n",
 "I'm always happy! Photo by @krisstephens  http://t.co/3T4VGuDb • you look really hot babe xxx < Look At!\n",
 "Nothing's wrong with thay @TheRealTank: I'm always happy! Photo by http://t.co/ywiJKEyD\n",
 'Sorry I post so many screen shots, I just like showing how happy I am(: #sorrynotsorry #sorryboutit\n',
 'Give it up give it up give it up for SHINee .. Thank you .. I am happy because of you, because of sherlock\n',
 'When I am happy my heart starts to slow\n',
 "My little brother is re evaluating my life I'm back to a B+ Haha... # @SavinTheWorldKP kirsti must be how happy I am from last night\n",
 'My hair gone bye bye and I am so happy to say I am glad to have my hair back. (Even though this is a hard thing to see)\n',
 'My life has no purpose, no direction, no aim, no meaning, and yet I’m happy. I can’t figure it out. What am I doing right?\n',
 'i am X-5’s fan & now ,i am NUEST fan too。。。i am from China!!!happy to follow!!!!\n',
 "I'm always happy! Photo by @krisstephens  http://t.co/Yy5WTHGn • you look really hot babe xxx\n",
 'I am happy, I am successful, bein in my own living space, bein in the masters prog I chose for myself, a life full of positivity...yay me!\n',
 'Alli, thanks for favoriteing my tweet! I like literally screamed! I am so happy Please follow me!!!?:)\n',
 'After several mini mental breakdowns 2day I am happy 2 say tht evrythng is starting 2 fall n2 place. Things happen 4 a reason. #justgowithit\n',
 'Why am I so happy right now!?\n',
 'Why the fuck am I so awake and so happy?\n',
 'just few hours to have you in America again!!! #TWonUS cant describe how happy I am :D\n',
 'Happy for Big 12 that KU won but I am a huge Robbie Hummel fan! Great player who really is what college hoops is suppose to be about!\n',
 'I am happy for who I am because I went through so much shit to become the person who I am today.\n',
 "No school in the am I'm soo happy !\n",
 "I am happy and all that I'm legal now but I would do anything to be like 5 again #foreveryoung\n"]
In [25]:
with open("C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/sad.txt", 'r',encoding="utf8") as f:
    tweet_sad = f.readlines() 
f.close()  
In [26]:
tweet_sad
Out[26]:
['When I think about #TrayvonMartin I can not help but worry about my own son!  I am so sad and angry right now! Have been all day actually.\n',
 '#iLikePeopleWho makes me laugh when I am sad.\n',
 'Sometimes I just instantly get sad when I realize how blessed I am and I think about homeless people sleeping outside and hungry :(\n',
 'I have zero pics from last night very sad :( but hey I am proud of that hat!!\n',
 'I hear ya.  I tried for a while and am now watching Goldmember. So sad\n',
 "I feel happy, I feel sad. And I don't know who I am.,It’s not the way you plan it is how you make it happend.\n",
 'it was so good! But so sad. I cried like a baby!! I am so sad that its ending :(\n',
 'no I am not in NYC these days... I know I am sad as well\n',
 'Why am I suddenly all sad?\n',
 "follows everyone but everyone doesn't follow back now i am sad\n",
 'Reading past convos i had with females, i am so oblivious and CANNOT take a hint. This is sad\n',
 'I am sad that the chief of police has already made up his mind that the shooter was protecting himself.\n',
 'I am on my PC!  The link is not working.  :: sad::\n',
 'As mad and sad as I am about Mike being killed, it was such a Brilliant example of tragic writing at its best.\n',
 'ohhhh come on ! now im sad. I am the one with really no friends at all.\n',
 'Oh my good!!!! Too much feelings inside and any way to show them... How sad girl am i?? A big one\n',
 'meh i am sad\n',
 'Recently found out blue mountain state is over . . . What am I suppose to watch now #sad\n',
 'seeing you at the same place i am at makes me very sad. sad that you didnt even say ONE FUCKING word to me.\n',
 'I am actually kind of sad that its not flannel weather anymore... #Iloveflannel #bestmaterialever #afterwoolandleather\n',
 'if any of your guys are followed by @diggy_simmons your rlly lucky cus im like trying and i need help cus i am going crazy. ugh i am sad :(\n',
 'i am kinda sad. lol\n',
 'Whenever I am sad, angry, or bored, I eat.\n',
 'Good night all.  I am so sad that Obama made us all slaves today.  Maybe tomorrow will be a better day....peace to us all...\n',
 'my sad had work this am smh happy bday tho uce!! I still owe u an sit\n',
 'I want a dq blizzard. Cheer me up I am sad :(\n',
 "Sad thing is My parents don't really know who I am.. they know me but dont KNOW me.\n",
 'my fucking ear hurts, Im sad asf, Im mad asf. ughhh wtf am i gonna do with myself\n',
 "I don't believe you..fuck ya I am I'm already sad cause I need some alcohol in my system\n",
 "I am so technology incapable, I cannot use tumbler XD it's so sad lol\n",
 'The only way im ever gonna get any flowers is if i buy them myself.....and thats sad. http://t.co/ltINc7x3\n',
 'i am sad like fucking shit\n',
 'yeah I know :( sad day\n',
 'I like tortured souls, sad as I am!!\n',
 'Well spring break is over in about 1 hour and I am sad:(\n',
 'are you sad the season is ending? ? I am:(\n',
 'Just watched the season finally of jersey shore and i am sad its over :(\n',
 'Is it sad that i am super excited for the new season of Frozen Planet to start Sunday on animal planet..\n',
 '(Insert sad asshole tweet here) Yea i wanna cuddle and am going to sleep alone right now... Night bitches! #dueces\n',
 'dynastycrisis: http://t.co/Z9wSRHqM\n',
 'When am I going to be able to see him without getting sad..\n',
 'we r so sad! i am crying in the first 3 mins ;(\n',
 'are you sad that the season is ending!? I know I am!\n',
 'I am very sad to need to set an alarm for the morning. Worst part of getting the boys to school.\n',
 'Kendra & Lexi .. who am i ? "Oh im crying again:( im so sad.. i really did like him:\'( wahhh" .... bitch moan bitch moan..\n',
 'But seriously why am I so sad tonight?\n',
 'When I am sad and mad I pick on my face till I rip flesh off. I need help -_-\n',
 "As sad as I am that you're not part of my life anymore I'm at least glad someone else is still getting the chance to have you in theirs. ...\n",
 "me ?? i am so sad read it T.T sorry :'( RT @lovely_leejoon: @dewii_1205 You mean Rani doesn't like you?I see..(T . T)\n",
 "I'm feeling so sad now... I will no longer be looking forward to #TheWalkingDead every Sunday. What am I going to do?\n",
 'So sad I have 8 AM class tomorrow #wahwah\n',
 'And if I am ever sad I feel like everyone thinks its because of you.\n',
 "I don't remember what this episodes holds!! But you are all so sad, I am worried!! #don'trememberyesterday\n",
 "I am.. its sad.  Like I can't even kill a spidar.. my little cousins do it.. haah\n",
 "realize you fucking twat. I'm not dragging in a sad way. I am actually quite happy! And I wouldn't say I don't care for you.\n",
 'Doing the only thing I know to do when I am sad and depressed, eating cheese\n',
 'O Gawsh please stop, I am over here dying laughin...Sad but, True...!\n',
 'I dont have gf because girls dont fine me attractive & that is why i am sad,negative girls hate the way i look i hate myself\n',
 "I  wanted to go so bad : c I love them so much it's sad am going to miss out :c sigh*\n",
 "You're my hero. you inspire me to do good in life. When i am sad I turn to you. I can relate to all of your songs. I love you.\n",
 '"I\'m all out of faith, this is how I feel, sad and I am ashamed, lying without tickets on the floor." I will sing this if I dont get tickets\n',
 'Ha!!! This is sad, but so true :) honey I am home!!!\n',
 "I am the #1 fan of #TheWalkingDead  I'm just sad to see Shane be killed but Rick had to do it right.\n",
 'SOUPY MAY NOT BE SAD ANYMORE BUT I SURE AM #fucking #FUCK\n',
 "it's sad how excited I am for breaking dawn part 2 to come out this november #teamedward #inlove\n",
 "I am so sad it's almost over. Glad to see Lynette is gonna fight tho!! #getitgirl\n",
 'When I am sad and think of how amazing my Lord is, it makes me cry tears of happiness.\n',
 'Ha oh shut up, & I am downnnnn!  & are you sad cause soccer season is almost over cause I am :c\n',
 'I hate to see people sad , so I try my best to make em smile , and it seems to work...thats just how I am....So smile :)\n',
 'Actually.... Maybe I still AM that sad....\n',
 "I am so proud! just sad it had to end how it did. and sad for what I'm gonna have to deal with tomorrow\n",
 'what is going on? everyone is sad now? I am confused-diana\n',
 'I am getting soo tired of all the racism and ignorance going on in the world today! The sad thing is #ItNeverEnds\n',
 "Why am I listening to sad songs? I'm not sad?\n",
 "After playing DDR and jumping rope today I've come to the sad realization that I am seriously out of shape...yikes.\n",
 'Getting sad because I am forever alone. xD\n',
 'I really am just so sad.\n',
 'Haizzzz.....1 more hours...i am leaving JB.....and my cousin are all leaving ad....SAD!!!!!!\n',
 ':( its not like i am laying here in bed, not even tired, wishing i had someone to talk too, havent talk to you in days....#SAD\n',
 'Woot! Thanks chica! I feel like I should be graduating this May, I am doing all of the graduation type things and yet am not. SAD\n']
In [27]:
import re
In [28]:
tweets = []
none_aplhabet = re.compile(r'[^a-zA-Z\s]')  
for words in tweet_happy:
    words = none_aplhabet.sub("",words)  #去除标点符号
    words_filtered = [e.lower() for e in words.split() if len(e) >= 3]    
    tweets.append((words_filtered, 'positive'))
for words in tweet_sad:
    words = none_aplhabet.sub("",words)  #去除标点符号
    words_filtered = [e.lower() for e in words.split() if len(e) >= 3]    
    tweets.append((words_filtered, 'negative'))
tweets[0]
Out[28]:
(['lovin',
  'life',
  'right',
  'about',
  'now',
  'loving',
  'the',
  'people',
  'god',
  'placing',
  'life',
  'happyampfocused',
  'striving',
  'the',
  'best',
  'woman',
  'can'],
 'positive')
In [29]:
# get the word lists of tweets
def get_words_in_tweets(tweets):
    all_words = []
    for (words,srntiment) in tweets:
        all_words.extend(words)
    return all_words

# get the unique word from the word list	
def get_word_features(wordlist):
    wordlist = nltk.FreqDist(wordlist)
    word_features = wordlist.keys()
    return word_features

word_features = get_word_features(get_words_in_tweets(tweets))
' '.join(word_features)
Out[29]:
'lovin life right about now loving the people god placing happyampfocused striving best woman can happy stpatricks day wasnt always easy celebrate one understands how agh delete just meant not stripper gawd she feels and say that both blessed been spring break ever its kind unbelievable ignorance bliss need shyti thats saddest lie cudi wmyb radio person thinkingpositive realize you fucking twat dragging sad way actually quite wouldnt dont care for because very today yuhuuuuuuuiiiiiiii soooo got chance hang boo blaqbetty last night too long muah your drivers was amazing finish mrw teams more days until opening this understatement mlb season will friendships developing with some twisters convert kudankulam plant into gasolinebased says wiseadmiral ramdas former naval chief perfect raza assuming bday where youre even lol well birthday specifically know why like picture but sunday went blue eyes httptcoxtqbsy jointwitte ptsdadhd psycho following does still have higher security clearence than potus obama dear hiltongardeninn fact stuck room beside ice machine goldmember pretty ton working hard what started getting our music out there rip family httptcojurzcwh jamie daughter stunning girl see frends wish summer sober brewing envy fine check from time glad really yuuuussss lmao yall said close distance did around baby httptcovoiv love khamiltonnn httptcoaxejlgx friend giggles smiles brightly are good himreally shes they deserve each other hes amp all mattersright dearest cant replace annoys most brenda httptcosxfwewm has weekend back everyone busiest week tweet while row super duper home sweetie him finished first thebeatdance proud thankful overwhelmed lots stuff ive keep starbucks gift card free get here httptcoenkvw mention work tomorrowbut job successful bein own living space masters prog chose myself full positivityyay looking through old pics realizing everything happens reason disney doesnt tlc therefore happiest place earth stupid dream rest maybe president skip paging monday twitter sorry birthdax had taste jimsamp nastiest steak hoagy jimssteakoutvery disappointed waste school tmrw means closer haha def tanning such stress reliever think licking lolly sounds hear little better patamon yayyy play games pata then httptcozqqqbeih photo krisstephens httptcotvgudb look hot babe xxx nothings wrong thay therealtank httptcoywijkeyd post many screen shots showing sorrynotsorry sorryboutit give shinee thank sherlock when heart starts slow brother evaluating savintheworldkp kirsti must hair gone bye though thing purpose direction aim meaning yet figure doing fan nuest tooi chinahappy follow httptcoyywthgn alli thanks favoriteing literally screamed please after several mini mental breakdowns tht evrythng starting fall things happen justgowithit fuck awake few hours america again twonus describe big won huge robbie hummel great player who college hoops suppose much shit become soo legal would anything foreveryoung trayvonmartin help worry son angry ilikepeoplewho makes laugh sometimes instantly homeless sleeping outside hungry zero hey hat tried watching feel amits plan make happend cried ending nyc these suddenly follows reading past convos females oblivious cannot take hint police already made his mind shooter protecting himself link mad mike being killed brilliant example tragic writing ohhhh come friends feelings inside any show them meh recently found mountain state over watch seeing same didnt word flannel weather anymore iloveflannel bestmaterialever afterwoolandleather guys followed diggysimmons rlly lucky cus trying going crazy ugh kinda whenever bored eat slaves tomorrow daypeace smh tho uce owe sit want blizzard cheer parents ear hurts asf ughhh wtf gonna believe youfuck cause alcohol system technology incapable use tumbler only flowers buy myselfand httptcoltincx yeah tortured souls hour watched finally jersey shore excited new frozen planet start animal insert asshole yea wanna cuddle sleep alone bitches dueces dynastycrisis httptcozwsrhqm able without crying mins set alarm morning worst part boys kendra lexi wahhh bitch moan seriously tonight pick face till flesh off least someone else theirs read lovelyleejoon dewii mean rani youi seet feeling longer forward thewalkingdead every class wahwah thinks remember episodes holds worried dontrememberyesterday kill spidar cousins haah depressed eating cheese gawsh stop dying laughinsad true girls attractive sadnegative hate wanted bad miss sigh hero inspire turn relate songs faith ashamed lying tickets floor sing honey shane rick soupy may sure breaking dawn november teamedward inlove almost lynette fight getitgirl lord cry tears happiness shut downnnnn soccer try smile seems workthats amso end deal confuseddiana tired racism world itneverends listening playing ddr jumping rope realization shapeyikes forever haizzzz hoursi leaving jband cousin adsad laying bed wishing talk havent dayssad woot chica should graduating graduation type'
In [30]:
def extract_features(document):
    document_words = set(document)
    features = {}
    for word in word_features:
        features['contains(%s)' % word] = (word in document_words)
    return features
In [31]:
training_set = nltk.classify.util.apply_features(extract_features,tweets)
training_set[100]
Out[31]:
({'contains(able)': False,
  'contains(about)': False,
  'contains(actually)': False,
  'contains(adsad)': False,
  'contains(after)': False,
  'contains(afterwoolandleather)': False,
  'contains(again)': False,
  'contains(agh)': False,
  'contains(aim)': False,
  'contains(alarm)': False,
  'contains(alcohol)': False,
  'contains(all)': False,
  'contains(alli)': False,
  'contains(almost)': False,
  'contains(alone)': False,
  'contains(already)': False,
  'contains(always)': False,
  'contains(amazing)': False,
  'contains(america)': False,
  'contains(amits)': False,
  'contains(amp)': False,
  'contains(amso)': False,
  'contains(and)': True,
  'contains(angry)': False,
  'contains(animal)': False,
  'contains(annoys)': False,
  'contains(any)': True,
  'contains(anymore)': False,
  'contains(anything)': False,
  'contains(are)': True,
  'contains(around)': False,
  'contains(asf)': False,
  'contains(ashamed)': False,
  'contains(asshole)': False,
  'contains(assuming)': False,
  'contains(attractive)': False,
  'contains(awake)': False,
  'contains(babe)': False,
  'contains(baby)': False,
  'contains(back)': False,
  'contains(bad)': False,
  'contains(bday)': False,
  'contains(because)': False,
  'contains(become)': False,
  'contains(bed)': False,
  'contains(been)': False,
  'contains(bein)': False,
  'contains(being)': False,
  'contains(believe)': False,
  'contains(beside)': False,
  'contains(best)': False,
  'contains(bestmaterialever)': False,
  'contains(better)': False,
  'contains(big)': False,
  'contains(birthdax)': False,
  'contains(birthday)': False,
  'contains(bitch)': False,
  'contains(bitches)': False,
  'contains(blaqbetty)': False,
  'contains(blessed)': False,
  'contains(bliss)': False,
  'contains(blizzard)': False,
  'contains(blue)': False,
  'contains(boo)': False,
  'contains(bored)': False,
  'contains(both)': False,
  'contains(boys)': False,
  'contains(break)': False,
  'contains(breakdowns)': False,
  'contains(breaking)': False,
  'contains(brenda)': False,
  'contains(brewing)': False,
  'contains(brightly)': False,
  'contains(brilliant)': False,
  'contains(brother)': False,
  'contains(busiest)': False,
  'contains(but)': False,
  'contains(buy)': False,
  'contains(bye)': False,
  'contains(can)': False,
  'contains(cannot)': False,
  'contains(cant)': False,
  'contains(card)': False,
  'contains(care)': False,
  'contains(cause)': False,
  'contains(celebrate)': False,
  'contains(chance)': False,
  'contains(check)': False,
  'contains(cheer)': False,
  'contains(cheese)': False,
  'contains(chica)': False,
  'contains(chief)': False,
  'contains(chinahappy)': False,
  'contains(chose)': False,
  'contains(class)': False,
  'contains(clearence)': False,
  'contains(close)': False,
  'contains(closer)': False,
  'contains(college)': False,
  'contains(come)': False,
  'contains(confuseddiana)': False,
  'contains(convert)': False,
  'contains(convos)': False,
  'contains(cousin)': False,
  'contains(cousins)': False,
  'contains(crazy)': True,
  'contains(cried)': False,
  'contains(cry)': False,
  'contains(crying)': False,
  'contains(cuddle)': False,
  'contains(cudi)': False,
  'contains(cus)': True,
  'contains(daughter)': False,
  'contains(dawn)': False,
  'contains(day)': False,
  'contains(daypeace)': False,
  'contains(days)': False,
  'contains(dayssad)': False,
  'contains(ddr)': False,
  'contains(deal)': False,
  'contains(dear)': False,
  'contains(dearest)': False,
  'contains(def)': False,
  'contains(delete)': False,
  'contains(depressed)': False,
  'contains(describe)': False,
  'contains(deserve)': False,
  'contains(developing)': False,
  'contains(dewii)': False,
  'contains(did)': False,
  'contains(didnt)': False,
  'contains(diggysimmons)': True,
  'contains(direction)': False,
  'contains(disappointed)': False,
  'contains(disney)': False,
  'contains(distance)': False,
  'contains(does)': False,
  'contains(doesnt)': False,
  'contains(doing)': False,
  'contains(dont)': False,
  'contains(dontrememberyesterday)': False,
  'contains(downnnnn)': False,
  'contains(dragging)': False,
  'contains(dream)': False,
  'contains(drivers)': False,
  'contains(dueces)': False,
  'contains(duper)': False,
  'contains(dying)': False,
  'contains(dynastycrisis)': False,
  'contains(each)': False,
  'contains(ear)': False,
  'contains(earth)': False,
  'contains(easy)': False,
  'contains(eat)': False,
  'contains(eating)': False,
  'contains(else)': False,
  'contains(end)': False,
  'contains(ending)': False,
  'contains(envy)': False,
  'contains(episodes)': False,
  'contains(evaluating)': False,
  'contains(even)': False,
  'contains(ever)': False,
  'contains(every)': False,
  'contains(everyone)': False,
  'contains(everything)': False,
  'contains(evrythng)': False,
  'contains(example)': False,
  'contains(excited)': False,
  'contains(eyes)': False,
  'contains(face)': False,
  'contains(fact)': False,
  'contains(faith)': False,
  'contains(fall)': False,
  'contains(family)': False,
  'contains(fan)': False,
  'contains(favoriteing)': False,
  'contains(feel)': False,
  'contains(feeling)': False,
  'contains(feelings)': False,
  'contains(feels)': False,
  'contains(females)': False,
  'contains(few)': False,
  'contains(fight)': False,
  'contains(figure)': False,
  'contains(finally)': False,
  'contains(fine)': False,
  'contains(finish)': False,
  'contains(finished)': False,
  'contains(first)': False,
  'contains(flannel)': False,
  'contains(flesh)': False,
  'contains(floor)': False,
  'contains(flowers)': False,
  'contains(follow)': False,
  'contains(followed)': True,
  'contains(following)': False,
  'contains(follows)': False,
  'contains(for)': False,
  'contains(forever)': False,
  'contains(foreveryoung)': False,
  'contains(former)': False,
  'contains(forward)': False,
  'contains(found)': False,
  'contains(free)': False,
  'contains(frends)': False,
  'contains(friend)': False,
  'contains(friends)': False,
  'contains(friendships)': False,
  'contains(from)': False,
  'contains(frozen)': False,
  'contains(fuck)': False,
  'contains(fucking)': False,
  'contains(full)': False,
  'contains(games)': False,
  'contains(gasolinebased)': False,
  'contains(gawd)': False,
  'contains(gawsh)': False,
  'contains(get)': False,
  'contains(getitgirl)': False,
  'contains(getting)': False,
  'contains(gift)': False,
  'contains(giggles)': False,
  'contains(girl)': False,
  'contains(girls)': False,
  'contains(give)': False,
  'contains(glad)': False,
  'contains(god)': False,
  'contains(going)': True,
  'contains(goldmember)': False,
  'contains(gone)': False,
  'contains(gonna)': False,
  'contains(good)': False,
  'contains(got)': False,
  'contains(graduating)': False,
  'contains(graduation)': False,
  'contains(great)': False,
  'contains(guys)': True,
  'contains(haah)': False,
  'contains(had)': False,
  'contains(haha)': False,
  'contains(hair)': False,
  'contains(haizzzz)': False,
  'contains(hang)': False,
  'contains(happen)': False,
  'contains(happend)': False,
  'contains(happens)': False,
  'contains(happiest)': False,
  'contains(happiness)': False,
  'contains(happy)': False,
  'contains(happyampfocused)': False,
  'contains(hard)': False,
  'contains(has)': False,
  'contains(hat)': False,
  'contains(hate)': False,
  'contains(have)': False,
  'contains(havent)': False,
  'contains(hear)': False,
  'contains(heart)': False,
  'contains(help)': True,
  'contains(here)': False,
  'contains(hero)': False,
  'contains(hes)': False,
  'contains(hey)': False,
  'contains(higher)': False,
  'contains(hiltongardeninn)': False,
  'contains(him)': False,
  'contains(himreally)': False,
  'contains(himself)': False,
  'contains(hint)': False,
  'contains(his)': False,
  'contains(hoagy)': False,
  'contains(holds)': False,
  'contains(home)': False,
  'contains(homeless)': False,
  'contains(honey)': False,
  'contains(hoops)': False,
  'contains(hot)': False,
  'contains(hour)': False,
  'contains(hours)': False,
  'contains(hoursi)': False,
  'contains(how)': False,
  'contains(httptcoaxejlgx)': False,
  'contains(httptcoenkvw)': False,
  'contains(httptcojurzcwh)': False,
  'contains(httptcoltincx)': False,
  'contains(httptcosxfwewm)': False,
  'contains(httptcotvgudb)': False,
  'contains(httptcovoiv)': False,
  'contains(httptcoxtqbsy)': False,
  'contains(httptcoywijkeyd)': False,
  'contains(httptcoyywthgn)': False,
  'contains(httptcozqqqbeih)': False,
  'contains(httptcozwsrhqm)': False,
  'contains(huge)': False,
  'contains(hummel)': False,
  'contains(hungry)': False,
  'contains(hurts)': False,
  'contains(ice)': False,
  'contains(ignorance)': False,
  'contains(ilikepeoplewho)': False,
  'contains(iloveflannel)': False,
  'contains(incapable)': False,
  'contains(inlove)': False,
  'contains(insert)': False,
  'contains(inside)': False,
  'contains(inspire)': False,
  'contains(instantly)': False,
  'contains(into)': False,
  'contains(itneverends)': False,
  'contains(its)': False,
  'contains(ive)': False,
  'contains(jamie)': False,
  'contains(jband)': False,
  'contains(jersey)': False,
  'contains(jimsamp)': False,
  'contains(jimssteakoutvery)': False,
  'contains(job)': False,
  'contains(jointwitte)': False,
  'contains(jumping)': False,
  'contains(just)': False,
  'contains(justgowithit)': False,
  'contains(keep)': False,
  'contains(kendra)': False,
  'contains(khamiltonnn)': False,
  'contains(kill)': False,
  'contains(killed)': False,
  'contains(kind)': False,
  'contains(kinda)': False,
  'contains(kirsti)': False,
  'contains(know)': False,
  'contains(krisstephens)': False,
  'contains(kudankulam)': False,
  'contains(last)': False,
  'contains(laugh)': False,
  'contains(laughinsad)': False,
  'contains(laying)': False,
  'contains(least)': False,
  'contains(leaving)': False,
  'contains(legal)': False,
  'contains(lexi)': False,
  'contains(licking)': False,
  'contains(lie)': False,
  'contains(life)': False,
  'contains(like)': True,
  'contains(link)': False,
  'contains(listening)': False,
  'contains(literally)': False,
  'contains(little)': False,
  'contains(living)': False,
  'contains(lmao)': False,
  'contains(lol)': False,
  'contains(lolly)': False,
  'contains(long)': False,
  'contains(longer)': False,
  'contains(look)': False,
  'contains(looking)': False,
  'contains(lord)': False,
  'contains(lots)': False,
  'contains(love)': False,
  'contains(lovelyleejoon)': False,
  'contains(lovin)': False,
  'contains(loving)': False,
  'contains(lucky)': True,
  'contains(lying)': False,
  'contains(lynette)': False,
  'contains(machine)': False,
  'contains(mad)': False,
  'contains(made)': False,
  'contains(make)': False,
  'contains(makes)': False,
  'contains(many)': False,
  'contains(masters)': False,
  'contains(mattersright)': False,
  'contains(may)': False,
  'contains(maybe)': False,
  'contains(mean)': False,
  'contains(meaning)': False,
  'contains(means)': False,
  'contains(meant)': False,
  'contains(meh)': False,
  'contains(mental)': False,
  'contains(mention)': False,
  'contains(mike)': False,
  'contains(mind)': False,
  'contains(mini)': False,
  'contains(mins)': False,
  'contains(miss)': False,
  'contains(mlb)': False,
  'contains(moan)': False,
  'contains(monday)': False,
  'contains(more)': False,
  'contains(morning)': False,
  'contains(most)': False,
  'contains(mountain)': False,
  'contains(mrw)': False,
  'contains(muah)': False,
  'contains(much)': False,
  'contains(music)': False,
  'contains(must)': False,
  'contains(myself)': False,
  'contains(myselfand)': False,
  'contains(nastiest)': False,
  'contains(naval)': False,
  'contains(need)': True,
  'contains(new)': False,
  'contains(night)': False,
  'contains(not)': False,
  'contains(nothings)': False,
  'contains(november)': False,
  'contains(now)': False,
  'contains(nuest)': False,
  'contains(nyc)': False,
  'contains(obama)': False,
  'contains(oblivious)': False,
  'contains(off)': False,
  'contains(ohhhh)': False,
  'contains(old)': False,
  'contains(one)': False,
  'contains(only)': False,
  'contains(opening)': False,
  'contains(other)': False,
  'contains(our)': False,
  'contains(out)': False,
  'contains(outside)': False,
  'contains(over)': False,
  'contains(overwhelmed)': False,
  'contains(owe)': False,
  'contains(own)': False,
  'contains(paging)': False,
  'contains(parents)': False,
  'contains(part)': False,
  'contains(past)': False,
  'contains(pata)': False,
  'contains(patamon)': False,
  'contains(people)': False,
  'contains(perfect)': False,
  'contains(person)': False,
  'contains(photo)': False,
  'contains(pick)': False,
  'contains(pics)': False,
  'contains(picture)': False,
  'contains(place)': False,
  'contains(placing)': False,
  'contains(plan)': False,
  'contains(planet)': False,
  'contains(plant)': False,
  'contains(play)': False,
  'contains(player)': False,
  'contains(playing)': False,
  'contains(please)': False,
  'contains(police)': False,
  'contains(positivityyay)': False,
  'contains(post)': False,
  'contains(potus)': False,
  'contains(president)': False,
  'contains(pretty)': False,
  'contains(prog)': False,
  'contains(protecting)': False,
  'contains(proud)': False,
  'contains(psycho)': False,
  'contains(ptsdadhd)': False,
  'contains(purpose)': False,
  'contains(quite)': False,
  'contains(racism)': False,
  'contains(radio)': False,
  'contains(ramdas)': False,
  'contains(rani)': False,
  'contains(raza)': False,
  'contains(read)': False,
  'contains(reading)': False,
  'contains(realization)': False,
  'contains(realize)': False,
  'contains(realizing)': False,
  'contains(really)': False,
  'contains(reason)': False,
  'contains(recently)': False,
  'contains(relate)': False,
  'contains(reliever)': False,
  'contains(remember)': False,
  'contains(replace)': False,
  'contains(rest)': False,
  'contains(rick)': False,
  'contains(right)': False,
  'contains(rip)': False,
  'contains(rlly)': True,
  'contains(robbie)': False,
  'contains(room)': False,
  'contains(rope)': False,
  'contains(row)': False,
  'contains(sad)': True,
  'contains(saddest)': False,
  'contains(sadnegative)': False,
  'contains(said)': False,
  'contains(same)': False,
  'contains(savintheworldkp)': False,
  'contains(say)': False,
  'contains(says)': False,
  'contains(school)': False,
  'contains(screamed)': False,
  'contains(screen)': False,
  'contains(season)': False,
  'contains(security)': False,
  'contains(see)': False,
  'contains(seeing)': False,
  'contains(seems)': False,
  'contains(seet)': False,
  'contains(seriously)': False,
  'contains(set)': False,
  'contains(several)': False,
  'contains(shane)': False,
  'contains(shapeyikes)': False,
  'contains(she)': False,
  'contains(sherlock)': False,
  'contains(shes)': False,
  'contains(shinee)': False,
  'contains(shit)': False,
  'contains(shooter)': False,
  'contains(shore)': False,
  'contains(shots)': False,
  'contains(should)': False,
  'contains(show)': False,
  'contains(showing)': False,
  'contains(shut)': False,
  'contains(shyti)': False,
  'contains(sigh)': False,
  'contains(sing)': False,
  'contains(sit)': False,
  'contains(skip)': False,
  'contains(slaves)': False,
  'contains(sleep)': False,
  'contains(sleeping)': False,
  'contains(slow)': False,
  'contains(smh)': False,
  'contains(smile)': False,
  'contains(smiles)': False,
  'contains(sober)': False,
  'contains(soccer)': False,
  'contains(some)': False,
  'contains(someone)': False,
  'contains(sometimes)': False,
  'contains(son)': False,
  'contains(songs)': False,
  'contains(soo)': False,
  'contains(soooo)': False,
  'contains(sorry)': False,
  'contains(sorryboutit)': False,
  'contains(sorrynotsorry)': False,
  'contains(souls)': False,
  'contains(sounds)': False,
  'contains(soupy)': False,
  'contains(space)': False,
  'contains(specifically)': False,
  'contains(spidar)': False,
  'contains(spring)': False,
  'contains(starbucks)': False,
  'contains(start)': False,
  'contains(started)': False,
  'contains(starting)': False,
  'contains(starts)': False,
  'contains(state)': False,
  'contains(steak)': False,
  'contains(still)': False,
  'contains(stop)': False,
  'contains(stpatricks)': False,
  'contains(stress)': False,
  'contains(stripper)': False,
  'contains(striving)': False,
  'contains(stuck)': False,
  'contains(stuff)': False,
  'contains(stunning)': False,
  'contains(stupid)': False,
  'contains(successful)': False,
  'contains(such)': False,
  'contains(suddenly)': False,
  'contains(summer)': False,
  'contains(sunday)': False,
  'contains(super)': False,
  'contains(suppose)': False,
  'contains(sure)': False,
  'contains(sweetie)': False,
  'contains(system)': False,
  'contains(take)': False,
  'contains(talk)': False,
  'contains(tanning)': False,
  'contains(taste)': False,
  'contains(teamedward)': False,
  'contains(teams)': False,
  'contains(tears)': False,
  'contains(technology)': False,
  'contains(than)': False,
  'contains(thank)': False,
  'contains(thankful)': False,
  'contains(thanks)': False,
  'contains(that)': False,
  'contains(thats)': False,
  'contains(thay)': False,
  'contains(the)': False,
  'contains(thebeatdance)': False,
  'contains(theirs)': False,
  'contains(them)': False,
  'contains(then)': False,
  'contains(there)': False,
  'contains(therealtank)': False,
  'contains(therefore)': False,
  'contains(these)': False,
  'contains(thewalkingdead)': False,
  'contains(they)': False,
  'contains(thing)': False,
  'contains(things)': False,
  'contains(think)': False,
  'contains(thinkingpositive)': False,
  'contains(thinks)': False,
  'contains(this)': False,
  'contains(tho)': False,
  'contains(though)': False,
  'contains(through)': False,
  'contains(tht)': False,
  'contains(tickets)': False,
  'contains(till)': False,
  'contains(time)': False,
  'contains(tired)': False,
  'contains(tlc)': False,
  'contains(tmrw)': False,
  'contains(today)': False,
  'contains(tomorrow)': False,
  'contains(tomorrowbut)': False,
  'contains(ton)': False,
  'contains(tonight)': False,
  'contains(too)': False,
  'contains(tooi)': False,
  'contains(tortured)': False,
  'contains(tragic)': False,
  'contains(trayvonmartin)': False,
  'contains(tried)': False,
  'contains(true)': False,
  'contains(try)': False,
  'contains(trying)': True,
  'contains(tumbler)': False,
  'contains(turn)': False,
  'contains(twat)': False,
  'contains(tweet)': False,
  'contains(twisters)': False,
  'contains(twitter)': False,
  'contains(twonus)': False,
  'contains(type)': False,
  'contains(uce)': False,
  'contains(ugh)': True,
  'contains(ughhh)': False,
  'contains(unbelievable)': False,
  'contains(understands)': False,
  'contains(understatement)': False,
  'contains(until)': False,
  'contains(use)': False,
  'contains(very)': False,
  'contains(wahhh)': False,
  'contains(wahwah)': False,
  'contains(wanna)': False,
  'contains(want)': False,
  'contains(wanted)': False,
  'contains(was)': False,
  'contains(wasnt)': False,
  'contains(waste)': False,
  'contains(watch)': False,
  'contains(watched)': False,
  'contains(watching)': False,
  'contains(way)': False,
  'contains(weather)': False,
  'contains(week)': False,
  'contains(weekend)': False,
  'contains(well)': False,
  'contains(went)': False,
  'contains(what)': False,
  'contains(when)': False,
  'contains(whenever)': False,
  'contains(where)': False,
  'contains(while)': False,
  'contains(who)': False,
  'contains(why)': False,
  'contains(will)': False,
  'contains(wiseadmiral)': False,
  'contains(wish)': False,
  'contains(wishing)': False,
  'contains(with)': False,
  'contains(without)': False,
  'contains(wmyb)': False,
  'contains(woman)': False,
  'contains(won)': False,
  'contains(woot)': False,
  'contains(word)': False,
  'contains(work)': False,
  'contains(working)': False,
  'contains(workthats)': False,
  'contains(world)': False,
  'contains(worried)': False,
  'contains(worry)': False,
  'contains(worst)': False,
  'contains(would)': False,
  'contains(wouldnt)': False,
  'contains(writing)': False,
  'contains(wrong)': False,
  'contains(wtf)': False,
  'contains(xxx)': False,
  'contains(yall)': False,
  'contains(yayyy)': False,
  'contains(yea)': False,
  'contains(yeah)': False,
  'contains(yet)': False,
  'contains(you)': False,
  'contains(youfuck)': False,
  'contains(youi)': False,
  'contains(your)': True,
  'contains(youre)': False,
  'contains(yuhuuuuuuuiiiiiiii)': False,
  'contains(yuuuussss)': False,
  'contains(zero)': False},
 'negative')
In [32]:
classifier = nltk.NaiveBayesClassifier.train(training_set)
In [33]:
with open("C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/happy_test.txt", 'r',encoding="utf8") as f:
    happy_test = f.readlines() 
f.close()  
In [34]:
print (classifier.classify(extract_features(happy_test[0].split())))
positive
In [35]:
extract_features(happy_test[0].split())
Out[35]:
{'contains(able)': False,
 'contains(about)': False,
 'contains(actually)': False,
 'contains(adsad)': False,
 'contains(after)': False,
 'contains(afterwoolandleather)': False,
 'contains(again)': False,
 'contains(agh)': False,
 'contains(aim)': False,
 'contains(alarm)': False,
 'contains(alcohol)': False,
 'contains(all)': True,
 'contains(alli)': False,
 'contains(almost)': False,
 'contains(alone)': False,
 'contains(already)': False,
 'contains(always)': False,
 'contains(amazing)': True,
 'contains(america)': False,
 'contains(amits)': False,
 'contains(amp)': False,
 'contains(amso)': False,
 'contains(and)': True,
 'contains(angry)': False,
 'contains(animal)': False,
 'contains(annoys)': False,
 'contains(any)': False,
 'contains(anymore)': False,
 'contains(anything)': False,
 'contains(are)': False,
 'contains(around)': False,
 'contains(asf)': False,
 'contains(ashamed)': False,
 'contains(asshole)': False,
 'contains(assuming)': False,
 'contains(attractive)': False,
 'contains(awake)': False,
 'contains(babe)': False,
 'contains(baby)': False,
 'contains(back)': False,
 'contains(bad)': False,
 'contains(bday)': False,
 'contains(because)': False,
 'contains(become)': False,
 'contains(bed)': False,
 'contains(been)': False,
 'contains(bein)': False,
 'contains(being)': False,
 'contains(believe)': False,
 'contains(beside)': False,
 'contains(best)': False,
 'contains(bestmaterialever)': False,
 'contains(better)': False,
 'contains(big)': False,
 'contains(birthdax)': False,
 'contains(birthday)': False,
 'contains(bitch)': False,
 'contains(bitches)': False,
 'contains(blaqbetty)': False,
 'contains(blessed)': False,
 'contains(bliss)': False,
 'contains(blizzard)': False,
 'contains(blue)': False,
 'contains(boo)': False,
 'contains(bored)': False,
 'contains(both)': False,
 'contains(boys)': False,
 'contains(break)': False,
 'contains(breakdowns)': False,
 'contains(breaking)': False,
 'contains(brenda)': False,
 'contains(brewing)': False,
 'contains(brightly)': False,
 'contains(brilliant)': False,
 'contains(brother)': False,
 'contains(busiest)': False,
 'contains(but)': False,
 'contains(buy)': False,
 'contains(bye)': False,
 'contains(can)': False,
 'contains(cannot)': False,
 'contains(cant)': False,
 'contains(card)': False,
 'contains(care)': False,
 'contains(cause)': False,
 'contains(celebrate)': False,
 'contains(chance)': False,
 'contains(check)': False,
 'contains(cheer)': False,
 'contains(cheese)': False,
 'contains(chica)': False,
 'contains(chief)': False,
 'contains(chinahappy)': False,
 'contains(chose)': False,
 'contains(class)': False,
 'contains(clearence)': False,
 'contains(close)': False,
 'contains(closer)': False,
 'contains(college)': False,
 'contains(come)': False,
 'contains(confuseddiana)': False,
 'contains(convert)': False,
 'contains(convos)': False,
 'contains(cousin)': False,
 'contains(cousins)': False,
 'contains(crazy)': False,
 'contains(cried)': False,
 'contains(cry)': False,
 'contains(crying)': False,
 'contains(cuddle)': False,
 'contains(cudi)': False,
 'contains(cus)': False,
 'contains(daughter)': False,
 'contains(dawn)': False,
 'contains(day)': False,
 'contains(daypeace)': False,
 'contains(days)': False,
 'contains(dayssad)': False,
 'contains(ddr)': False,
 'contains(deal)': False,
 'contains(dear)': False,
 'contains(dearest)': False,
 'contains(def)': False,
 'contains(delete)': False,
 'contains(depressed)': False,
 'contains(describe)': False,
 'contains(deserve)': True,
 'contains(developing)': False,
 'contains(dewii)': False,
 'contains(did)': False,
 'contains(didnt)': False,
 'contains(diggysimmons)': False,
 'contains(direction)': False,
 'contains(disappointed)': False,
 'contains(disney)': False,
 'contains(distance)': False,
 'contains(does)': False,
 'contains(doesnt)': False,
 'contains(doing)': False,
 'contains(dont)': False,
 'contains(dontrememberyesterday)': False,
 'contains(downnnnn)': False,
 'contains(dragging)': False,
 'contains(dream)': False,
 'contains(drivers)': False,
 'contains(dueces)': False,
 'contains(duper)': False,
 'contains(dying)': False,
 'contains(dynastycrisis)': False,
 'contains(each)': True,
 'contains(ear)': False,
 'contains(earth)': False,
 'contains(easy)': False,
 'contains(eat)': False,
 'contains(eating)': False,
 'contains(else)': False,
 'contains(end)': False,
 'contains(ending)': False,
 'contains(envy)': False,
 'contains(episodes)': False,
 'contains(evaluating)': False,
 'contains(even)': False,
 'contains(ever)': False,
 'contains(every)': False,
 'contains(everyone)': False,
 'contains(everything)': False,
 'contains(evrythng)': False,
 'contains(example)': False,
 'contains(excited)': False,
 'contains(eyes)': False,
 'contains(face)': False,
 'contains(fact)': False,
 'contains(faith)': False,
 'contains(fall)': False,
 'contains(family)': False,
 'contains(fan)': False,
 'contains(favoriteing)': False,
 'contains(feel)': False,
 'contains(feeling)': False,
 'contains(feelings)': False,
 'contains(feels)': False,
 'contains(females)': False,
 'contains(few)': False,
 'contains(fight)': False,
 'contains(figure)': False,
 'contains(finally)': False,
 'contains(fine)': False,
 'contains(finish)': False,
 'contains(finished)': False,
 'contains(first)': False,
 'contains(flannel)': False,
 'contains(flesh)': False,
 'contains(floor)': False,
 'contains(flowers)': False,
 'contains(follow)': False,
 'contains(followed)': False,
 'contains(following)': False,
 'contains(follows)': False,
 'contains(for)': True,
 'contains(forever)': False,
 'contains(foreveryoung)': False,
 'contains(former)': False,
 'contains(forward)': False,
 'contains(found)': False,
 'contains(free)': False,
 'contains(frends)': False,
 'contains(friend)': False,
 'contains(friends)': False,
 'contains(friendships)': False,
 'contains(from)': False,
 'contains(frozen)': False,
 'contains(fuck)': False,
 'contains(fucking)': False,
 'contains(full)': False,
 'contains(games)': False,
 'contains(gasolinebased)': False,
 'contains(gawd)': False,
 'contains(gawsh)': False,
 'contains(get)': False,
 'contains(getitgirl)': False,
 'contains(getting)': False,
 'contains(gift)': False,
 'contains(giggles)': False,
 'contains(girl)': False,
 'contains(girls)': False,
 'contains(give)': False,
 'contains(glad)': False,
 'contains(god)': False,
 'contains(going)': False,
 'contains(goldmember)': False,
 'contains(gone)': False,
 'contains(gonna)': False,
 'contains(good)': False,
 'contains(got)': False,
 'contains(graduating)': False,
 'contains(graduation)': False,
 'contains(great)': False,
 'contains(guys)': False,
 'contains(haah)': False,
 'contains(had)': False,
 'contains(haha)': False,
 'contains(hair)': False,
 'contains(haizzzz)': False,
 'contains(hang)': False,
 'contains(happen)': False,
 'contains(happend)': False,
 'contains(happens)': False,
 'contains(happiest)': False,
 'contains(happiness)': False,
 'contains(happy)': True,
 'contains(happyampfocused)': False,
 'contains(hard)': False,
 'contains(has)': False,
 'contains(hat)': False,
 'contains(hate)': False,
 'contains(have)': False,
 'contains(havent)': False,
 'contains(hear)': False,
 'contains(heart)': False,
 'contains(help)': False,
 'contains(here)': False,
 'contains(hero)': False,
 'contains(hes)': False,
 'contains(hey)': False,
 'contains(higher)': False,
 'contains(hiltongardeninn)': False,
 'contains(him)': False,
 'contains(himreally)': False,
 'contains(himself)': False,
 'contains(hint)': False,
 'contains(his)': False,
 'contains(hoagy)': False,
 'contains(holds)': False,
 'contains(home)': False,
 'contains(homeless)': False,
 'contains(honey)': False,
 'contains(hoops)': False,
 'contains(hot)': False,
 'contains(hour)': False,
 'contains(hours)': False,
 'contains(hoursi)': False,
 'contains(how)': False,
 'contains(httptcoaxejlgx)': False,
 'contains(httptcoenkvw)': False,
 'contains(httptcojurzcwh)': False,
 'contains(httptcoltincx)': False,
 'contains(httptcosxfwewm)': False,
 'contains(httptcotvgudb)': False,
 'contains(httptcovoiv)': False,
 'contains(httptcoxtqbsy)': False,
 'contains(httptcoywijkeyd)': False,
 'contains(httptcoyywthgn)': False,
 'contains(httptcozqqqbeih)': False,
 'contains(httptcozwsrhqm)': False,
 'contains(huge)': False,
 'contains(hummel)': False,
 'contains(hungry)': False,
 'contains(hurts)': False,
 'contains(ice)': False,
 'contains(ignorance)': False,
 'contains(ilikepeoplewho)': False,
 'contains(iloveflannel)': False,
 'contains(incapable)': False,
 'contains(inlove)': False,
 'contains(insert)': False,
 'contains(inside)': False,
 'contains(inspire)': False,
 'contains(instantly)': False,
 'contains(into)': False,
 'contains(itneverends)': False,
 'contains(its)': False,
 'contains(ive)': False,
 'contains(jamie)': False,
 'contains(jband)': False,
 'contains(jersey)': False,
 'contains(jimsamp)': False,
 'contains(jimssteakoutvery)': False,
 'contains(job)': False,
 'contains(jointwitte)': False,
 'contains(jumping)': False,
 'contains(just)': False,
 'contains(justgowithit)': False,
 'contains(keep)': False,
 'contains(kendra)': False,
 'contains(khamiltonnn)': False,
 'contains(kill)': False,
 'contains(killed)': False,
 'contains(kind)': False,
 'contains(kinda)': False,
 'contains(kirsti)': False,
 'contains(know)': False,
 'contains(krisstephens)': False,
 'contains(kudankulam)': False,
 'contains(last)': False,
 'contains(laugh)': False,
 'contains(laughinsad)': False,
 'contains(laying)': False,
 'contains(least)': False,
 'contains(leaving)': False,
 'contains(legal)': False,
 'contains(lexi)': False,
 'contains(licking)': False,
 'contains(lie)': False,
 'contains(life)': False,
 'contains(like)': False,
 'contains(link)': False,
 'contains(listening)': False,
 'contains(literally)': False,
 'contains(little)': False,
 'contains(living)': False,
 'contains(lmao)': False,
 'contains(lol)': False,
 'contains(lolly)': False,
 'contains(long)': False,
 'contains(longer)': False,
 'contains(look)': False,
 'contains(looking)': False,
 'contains(lord)': False,
 'contains(lots)': False,
 'contains(love)': False,
 'contains(lovelyleejoon)': False,
 'contains(lovin)': False,
 'contains(loving)': False,
 'contains(lucky)': False,
 'contains(lying)': False,
 'contains(lynette)': False,
 'contains(machine)': False,
 'contains(mad)': False,
 'contains(made)': False,
 'contains(make)': False,
 'contains(makes)': False,
 'contains(many)': False,
 'contains(masters)': False,
 'contains(mattersright)': False,
 'contains(may)': False,
 'contains(maybe)': False,
 'contains(mean)': False,
 'contains(meaning)': False,
 'contains(means)': False,
 'contains(meant)': False,
 'contains(meh)': False,
 'contains(mental)': False,
 'contains(mention)': False,
 'contains(mike)': False,
 'contains(mind)': False,
 'contains(mini)': False,
 'contains(mins)': False,
 'contains(miss)': False,
 'contains(mlb)': False,
 'contains(moan)': False,
 'contains(monday)': False,
 'contains(more)': False,
 'contains(morning)': False,
 'contains(most)': False,
 'contains(mountain)': False,
 'contains(mrw)': False,
 'contains(muah)': False,
 'contains(much)': False,
 'contains(music)': False,
 'contains(must)': False,
 'contains(myself)': False,
 'contains(myselfand)': False,
 'contains(nastiest)': False,
 'contains(naval)': False,
 'contains(need)': False,
 'contains(new)': False,
 'contains(night)': False,
 'contains(not)': False,
 'contains(nothings)': False,
 'contains(november)': False,
 'contains(now)': False,
 'contains(nuest)': False,
 'contains(nyc)': False,
 'contains(obama)': False,
 'contains(oblivious)': False,
 'contains(off)': False,
 'contains(ohhhh)': False,
 'contains(old)': False,
 'contains(one)': False,
 'contains(only)': False,
 'contains(opening)': False,
 'contains(other)': False,
 'contains(our)': False,
 'contains(out)': False,
 'contains(outside)': False,
 'contains(over)': False,
 'contains(overwhelmed)': False,
 'contains(owe)': False,
 'contains(own)': False,
 'contains(paging)': False,
 'contains(parents)': False,
 'contains(part)': False,
 'contains(past)': False,
 'contains(pata)': False,
 'contains(patamon)': False,
 'contains(people)': False,
 'contains(perfect)': False,
 'contains(person)': False,
 'contains(photo)': False,
 'contains(pick)': False,
 'contains(pics)': False,
 'contains(picture)': False,
 'contains(place)': False,
 'contains(placing)': False,
 'contains(plan)': False,
 'contains(planet)': False,
 'contains(plant)': False,
 'contains(play)': False,
 'contains(player)': False,
 'contains(playing)': False,
 'contains(please)': False,
 'contains(police)': False,
 'contains(positivityyay)': False,
 'contains(post)': False,
 'contains(potus)': False,
 'contains(president)': False,
 'contains(pretty)': False,
 'contains(prog)': False,
 'contains(protecting)': False,
 'contains(proud)': False,
 'contains(psycho)': False,
 'contains(ptsdadhd)': False,
 'contains(purpose)': False,
 'contains(quite)': False,
 'contains(racism)': False,
 'contains(radio)': False,
 'contains(ramdas)': False,
 'contains(rani)': False,
 'contains(raza)': False,
 'contains(read)': False,
 'contains(reading)': False,
 'contains(realization)': False,
 'contains(realize)': False,
 'contains(realizing)': False,
 'contains(really)': False,
 'contains(reason)': False,
 'contains(recently)': False,
 'contains(relate)': False,
 'contains(reliever)': False,
 'contains(remember)': False,
 'contains(replace)': False,
 'contains(rest)': False,
 'contains(rick)': False,
 'contains(right)': False,
 'contains(rip)': False,
 'contains(rlly)': False,
 'contains(robbie)': False,
 'contains(room)': False,
 'contains(rope)': False,
 'contains(row)': False,
 'contains(sad)': False,
 'contains(saddest)': False,
 'contains(sadnegative)': False,
 'contains(said)': False,
 'contains(same)': False,
 'contains(savintheworldkp)': False,
 'contains(say)': False,
 'contains(says)': False,
 'contains(school)': False,
 'contains(screamed)': False,
 'contains(screen)': False,
 'contains(season)': False,
 'contains(security)': False,
 'contains(see)': False,
 'contains(seeing)': False,
 'contains(seems)': False,
 'contains(seet)': False,
 'contains(seriously)': False,
 'contains(set)': False,
 'contains(several)': False,
 'contains(shane)': False,
 'contains(shapeyikes)': False,
 'contains(she)': False,
 'contains(sherlock)': False,
 'contains(shes)': False,
 'contains(shinee)': False,
 'contains(shit)': False,
 'contains(shooter)': False,
 'contains(shore)': False,
 'contains(shots)': False,
 'contains(should)': False,
 'contains(show)': False,
 'contains(showing)': False,
 'contains(shut)': False,
 'contains(shyti)': False,
 'contains(sigh)': False,
 'contains(sing)': False,
 'contains(sit)': False,
 'contains(skip)': False,
 'contains(slaves)': False,
 'contains(sleep)': False,
 'contains(sleeping)': False,
 'contains(slow)': False,
 'contains(smh)': False,
 'contains(smile)': False,
 'contains(smiles)': False,
 'contains(sober)': False,
 'contains(soccer)': False,
 'contains(some)': False,
 'contains(someone)': False,
 'contains(sometimes)': False,
 'contains(son)': False,
 'contains(songs)': False,
 'contains(soo)': False,
 'contains(soooo)': False,
 'contains(sorry)': False,
 'contains(sorryboutit)': False,
 'contains(sorrynotsorry)': False,
 'contains(souls)': False,
 'contains(sounds)': False,
 'contains(soupy)': False,
 'contains(space)': False,
 'contains(specifically)': False,
 'contains(spidar)': False,
 'contains(spring)': False,
 'contains(starbucks)': False,
 'contains(start)': False,
 'contains(started)': False,
 'contains(starting)': False,
 'contains(starts)': False,
 'contains(state)': False,
 'contains(steak)': False,
 'contains(still)': False,
 'contains(stop)': False,
 'contains(stpatricks)': False,
 'contains(stress)': False,
 'contains(stripper)': False,
 'contains(striving)': False,
 'contains(stuck)': False,
 'contains(stuff)': False,
 'contains(stunning)': False,
 'contains(stupid)': False,
 'contains(successful)': False,
 'contains(such)': False,
 'contains(suddenly)': False,
 'contains(summer)': False,
 'contains(sunday)': False,
 'contains(super)': False,
 'contains(suppose)': False,
 'contains(sure)': False,
 'contains(sweetie)': False,
 'contains(system)': False,
 'contains(take)': False,
 'contains(talk)': False,
 'contains(tanning)': False,
 'contains(taste)': False,
 'contains(teamedward)': False,
 'contains(teams)': False,
 'contains(tears)': False,
 'contains(technology)': False,
 'contains(than)': False,
 'contains(thank)': False,
 'contains(thankful)': False,
 'contains(thanks)': False,
 'contains(that)': True,
 'contains(thats)': True,
 'contains(thay)': False,
 'contains(the)': False,
 'contains(thebeatdance)': False,
 'contains(theirs)': False,
 'contains(them)': False,
 'contains(then)': False,
 'contains(there)': False,
 'contains(therealtank)': False,
 'contains(therefore)': False,
 'contains(these)': False,
 'contains(thewalkingdead)': False,
 'contains(they)': True,
 'contains(thing)': False,
 'contains(things)': False,
 'contains(think)': False,
 'contains(thinkingpositive)': False,
 'contains(thinks)': False,
 'contains(this)': False,
 'contains(tho)': False,
 'contains(though)': False,
 'contains(through)': False,
 'contains(tht)': False,
 'contains(tickets)': False,
 'contains(till)': False,
 'contains(time)': False,
 'contains(tired)': False,
 'contains(tlc)': False,
 'contains(tmrw)': False,
 'contains(today)': False,
 'contains(tomorrow)': False,
 'contains(tomorrowbut)': False,
 'contains(ton)': False,
 'contains(tonight)': False,
 'contains(too)': False,
 'contains(tooi)': False,
 'contains(tortured)': False,
 'contains(tragic)': False,
 'contains(trayvonmartin)': False,
 'contains(tried)': False,
 'contains(true)': False,
 'contains(try)': False,
 'contains(trying)': False,
 'contains(tumbler)': False,
 'contains(turn)': False,
 'contains(twat)': False,
 'contains(tweet)': False,
 'contains(twisters)': False,
 'contains(twitter)': False,
 'contains(twonus)': False,
 'contains(type)': False,
 'contains(uce)': False,
 'contains(ugh)': False,
 'contains(ughhh)': False,
 'contains(unbelievable)': False,
 'contains(understands)': False,
 'contains(understatement)': False,
 'contains(until)': False,
 'contains(use)': False,
 'contains(very)': False,
 'contains(wahhh)': False,
 'contains(wahwah)': False,
 'contains(wanna)': False,
 'contains(want)': False,
 'contains(wanted)': False,
 'contains(was)': False,
 'contains(wasnt)': False,
 'contains(waste)': False,
 'contains(watch)': False,
 'contains(watched)': False,
 'contains(watching)': False,
 'contains(way)': False,
 'contains(weather)': False,
 'contains(week)': False,
 'contains(weekend)': False,
 'contains(well)': False,
 'contains(went)': False,
 'contains(what)': False,
 'contains(when)': False,
 'contains(whenever)': False,
 'contains(where)': False,
 'contains(while)': False,
 'contains(who)': False,
 'contains(why)': False,
 'contains(will)': False,
 'contains(wiseadmiral)': False,
 'contains(wish)': False,
 'contains(wishing)': False,
 'contains(with)': False,
 'contains(without)': False,
 'contains(wmyb)': False,
 'contains(woman)': False,
 'contains(won)': False,
 'contains(woot)': False,
 'contains(word)': False,
 'contains(work)': False,
 'contains(working)': False,
 'contains(workthats)': False,
 'contains(world)': False,
 'contains(worried)': False,
 'contains(worry)': False,
 'contains(worst)': False,
 'contains(would)': False,
 'contains(wouldnt)': False,
 'contains(writing)': False,
 'contains(wrong)': False,
 'contains(wtf)': False,
 'contains(xxx)': False,
 'contains(yall)': False,
 'contains(yayyy)': False,
 'contains(yea)': False,
 'contains(yeah)': False,
 'contains(yet)': False,
 'contains(you)': False,
 'contains(youfuck)': False,
 'contains(youi)': False,
 'contains(your)': False,
 'contains(youre)': False,
 'contains(yuhuuuuuuuiiiiiiii)': False,
 'contains(yuuuussss)': False,
 'contains(zero)': False}
In [36]:
with open("C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/sad_test.txt", 'r',encoding="utf8") as f:
    sad_test = f.readlines() 
f.close() 
In [37]:
print (classifier.classify(extract_features(sad_test[0].split())))
negative
In [38]:
def classify_tweet(tweet):
    return classifier.classify(extract_features(nltk.word_tokenize(tweet)))
In [39]:
def read_tweets(fname, t_type):
    tweets = []
    f = open(fname, 'r')
    line = f.readline()
    while line != '':
        tweets.append([line, t_type])
        line = f.readline()
    f.close()
    return tweets
In [40]:
test_tweets = read_tweets('C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/happy_test.txt', 'positive')
test_tweets.extend(read_tweets('C:/Users/shysh/Desktop/Twitter-Sentimental-Analysis-master/sad_test.txt', 'negative'))
total = accuracy = float(len(test_tweets))
for tweet in test_tweets:
    if classify_tweet(tweet[0]) != tweet[1]:
        accuracy -= 1
        print(classify_tweet(tweet[0]))
print('Total accuracy: %f%% (%d/20).' % (accuracy / total * 100, accuracy))
negative
positive
Total accuracy: 90.000000% (18/20).
In [41]:
test_tweets
Out[41]:
[["I'm happy for him...really, I am. She's an amazing girl, and they deserve each other. He's happy & thats all that matters...right?.....\n",
  'positive'],
 ['Feel so happy with no reason... Just happy... Hey my brain, am I missing something? :))\n',
  'positive'],
 ['We finished our first season of @TheBEATDance & I am so happy & proud & thankful & overwhelmed & lots of other good stuff! So Amazing #2013\n',
  'positive'],
 ['am i allowed to be happy about something, or do yo wanna distroy the little i have left?\n',
  'positive'],
 ["I am so happy right now I can't even focus on anything else\n", 'positive'],
 ["Why am I being sneaked around her fam when I'm open about us.... But we both happy shit don't add up.\n",
  'positive'],
 ['Heavens suppose to be the happiest place in the world I am happy everyday with the people I love but I feel like I live in heaven everyday:)\n',
  'positive'],
 ['I am  so happy since I have get an $100,00 STARBUCKS GIFT-CARD for Free. I grab it here http://t.co/cg8M1Ubq\n',
  'positive'],
 ['I am one #happy girl :)\n', 'positive'],
 ['I Am So HAPPY .\n', 'positive'],
 ["I am in Nashville at wild horse saloon and see you are playing here can't tell you how bummed I am that I'm missing it! #sad\n",
  'negative'],
 ['I am so sad right now\n', 'negative'],
 ['Thank you Lillian, it is very sad for me, but I am excited to see  the #yegfood community evolve & support eachother!\n',
  'negative'],
 ["I'm am honestly too caring of others & selfless. The sad thing is. I can't do anything bout it.\n",
  'negative'],
 ['Did I mention that I am sad I have to go back to work tomorrow....but happy I can just do my job for now on.....\n',
  'negative'],
 ['I am sad. Purdue deserved it. #marchmadness\n', 'negative'],
 ['I feel so sick to my stomach. I am so sad.\n', 'negative'],
 ["that's just sad I really am mad now!!!!!!\n", 'negative'],
 ["oh god last pick. do I take Trumbo because I will be sad if I don't or do I take someone else I don't know I am too neurotic for this shit\n",
  'negative'],
 ['I am hungry. Wanna cook sup ayam but multi cooker rosak. I have to buy the third multi cooker after this :( Me iz sad.\n',
  'negative']]