forked from ravikiranj/twitter-sentiment-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes_classifier_demo.py
executable file
·36 lines (27 loc) · 1.23 KB
/
naive_bayes_classifier_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import nltk
from nltk.classify import *
import classifier_helper
from classifier_helper import *
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')]
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))
word_features = get_word_features(get_words_in_tweets(tweets))
set_word_features(word_features)
training_set = nltk.classify.apply_features(extract_features, tweets)
test = 1
classifier = nltk.NaiveBayesClassifier.train(training_set)
tweet = 'Im happy'
print classifier.classify(extract_features(tweet.split()))
print nltk.classify.accuracy(classifier, training_set)
classifier.show_most_informative_features(10)