Skip to content

Instantly share code, notes, and snippets.

@dpnishant

dpnishant/nlp.py Secret

Last active June 1, 2016 14:45
Show Gist options
  • Save dpnishant/367cef57a8033138eb0a to your computer and use it in GitHub Desktop.
Save dpnishant/367cef57a8033138eb0a to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
import datetime
from textblob import TextBlob
from textblob import Blobber
from textblob.sentiments import NaiveBayesAnalyzer
from textblob.classifiers import NaiveBayesClassifier
from textblob.classifiers import DecisionTreeClassifier
from textblob.classifiers import MaxEntClassifier
from textblob.classifiers import BaseClassifier
from textblob.classifiers import NLTKClassifier
import nltk
class MyClassifier(NLTKClassifier):
nltk_class = nltk.classify.scikitlearn.SklearnClassifier
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')]
print("train_len: %d" % len(train))
print("test_len: %d" % len(test))
print("\n\nNaiveBayesClassifier\n====================")
ts = datetime.datetime.now()
cl = NaiveBayesClassifier(train)
print("training: %s" % (str(datetime.datetime.now() - ts)))
text = 'sandwich is good'
blob = Blobber(analyzer=NaiveBayesAnalyzer())
ss = datetime.datetime.now()
blob_sent = blob(text).sentiment
print("sentiment: %s" % (str(datetime.datetime.now() - ss)))
print("blob", blob_sent)
cs = datetime.datetime.now()
sent = cl.classify(text)
print("classify: %s" % (str(datetime.datetime.now() - cs)))
sent_prob = cl.prob_classify('sandwich is good')
accuracy = cl.accuracy(test)
print ("sentiment: %s" % sent,
"\naccuracy: %s%%" % str(round(accuracy, 2)* 100),
"\nprobability: %s%%" % str(round(sent_prob.prob(sent_prob.max()), 2) * 100))
print("\n\nDecisionTreeClassifier\n======================")
ts = datetime.datetime.now()
cl = DecisionTreeClassifier(train)
print("training: %s" % (str(datetime.datetime.now() - ts)))
cs = datetime.datetime.now()
sent = cl.classify('sandwich is good')
#sent_prob = cl.prob_classify('sandwich is good')
accuracy = cl.accuracy(test)
print ("Classification: %s" % sent,
"\naccuracy: %s%%" % str(round(accuracy, 2) * 100),
"\nprobability: N/A")
print("classify: %s" % (str(datetime.datetime.now() - ts)))
print("\n\nMaxEntClassifier\n====================")
ts = datetime.datetime.now()
cl = MaxEntClassifier(train)
print("training: %s" % (str(datetime.datetime.now() - ts)))
cs = datetime.datetime.now()
sent = cl.classify('sandwich is good')
sent_prob = cl.prob_classify('sandwich is good')
accuracy = cl.accuracy(test)
print ("sentiment: %s" % sent,
"\naccuracy: %s%%" % str(round(accuracy, 2)* 100),
"\nprobability: %s%%" % str(round(sent_prob.prob(sent_prob.max()), 2) * 100))
print("classify: %s" % (str(datetime.datetime.now() - ts)))
print("\n\nNLTKClassifier\n======================")
ts = datetime.datetime.now()
cl = MyClassifier(train)
#cl = my_classifier(train)
#cl = NLTKClassifier(train)
print("training: %s" % (str(datetime.datetime.now() - ts)))
cs = datetime.datetime.now()
sent = cl.classify('sandwich is good')
#sent_prob = cl.prob_classify('sandwich is good')
accuracy = cl.accuracy(test)
print ("sentiment: %s" % sent,
"\naccuracy: %s%%" % str(round(accuracy, 2) * 100),
"\nprobability: N/A")
print("classify: %s" % (str(datetime.datetime.now() - ts)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment