] 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) # Classify some text print(cl.classify("Their burgers are amazing.")) # "pos" print(cl.classify("I don't like their pizza.")) # "neg" # Classify a TextBlob blob = TextBlob("The beer was amazing. But the hangover was horrible. " "My boss was not pleased.", classifier=cl) print(blob) print(blob.classify()) for sentence in blob.sentences: print(sentence) print(sentence.classify()) # Compute accuracy print("Accuracy: {0}".format(cl.accuracy(test))) # Show 5 most informative features cl.show_informative_features(5)
('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) # Classify some text print(cl.classify("Their burgers are amazing.")) # "pos" print(cl.classify("I don't like their pizza.")) # "neg" # Classify a TextBlob blob = TextBlob( "The beer was amazing. But the hangover was horrible. " "My boss was not pleased.", classifier=cl) print(blob) print(blob.classify()) for sentence in blob.sentences: print(sentence) print(sentence.classify()) # Compute accuracy print("Accuracy: {0}".format(cl.accuracy(test))) # Show 5 most informative features cl.show_informative_features(5)
def classify(new_comment, bayes): '''takes a comment string (to be classified) and a trained bayes returns string 'pos' (normal) or 'neg' (crazy)''' analyze = TextBlob(new_comment,classifier = bayes) return analyze.classify()