Skip to content

Instantly share code, notes, and snippets.

@sloria
Last active October 9, 2017 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sloria/6338376 to your computer and use it in GitHub Desktop.
Save sloria/6338376 to your computer and use it in GitHub Desktop.
import random
from nltk.corpus import movie_reviews
from textblob.classifiers import NaiveBayesClassifier
random.seed(1)
train = [
('I love this sandwich.', 'pos'),
('This is an amazing place!', 'pos'),
('I feel very good about these beers.', 'pos'),
('This is my best work.', 'pos'),
("What an awesome view", 'pos'),
('I do not like this restaurant', 'neg'),
('I am tired of this stuff.', 'neg'),
("I can't deal with this", 'neg'),
('He is my sworn enemy!', 'neg'),
('My boss is horrible.', 'neg')
]
test = [
('The beer was good.', 'pos'),
('I do not enjoy my job', 'neg'),
("I ain't feeling dandy today.", 'neg'),
("I feel amazing!", 'pos'),
('Gary is a friend of mine.', 'pos'),
("I can't believe I'm doing this.", 'neg')
]
cl = NaiveBayesClassifier(train)
# Grab some movie review data
reviews = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle(reviews)
new_train, new_test = reviews[0:100], reviews[101:200]
# Update the classifier with the new training data
cl.update(new_train)
# Compute accuracy
accuracy = cl.accuracy(test + new_test)
print("Accuracy: {0}".format(accuracy))
# Show 5 most informative features
cl.show_informative_features(5)
@monika21089
Copy link

when i run this program it shows me error. i am running it on windows 7 and have python version 2.7.7.
the error come like
Traceback (most recent call last):
File "C:\Users\monika\Desktop\python1\nltkclassifiers.py", line 3, in
from text.classifiers import NaiveBayesClassifier
ImportError: No module named text.classifiers.

@cctv2206
Copy link

cctv2206 commented Nov 6, 2015

I am having the same problem under OS X.
ImportError: No module named text.classifiers.

@pattonwebz
Copy link

I had the same import error as the other 2 posters. I discovered it was because this script imports the wrong package. Instead of text.classifiers it should be textblob.classifiers.

I just forked this gist and made it work here: https://gist.github.com/pattonwebz/9b1ea78570e086c37d01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment