Last active
October 9, 2017 17:40
-
-
Save sloria/6338376 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
I am having the same problem under OS X.
ImportError: No module named text.classifiers.
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
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.