Skip to content

Instantly share code, notes, and snippets.

@dangayle
Forked from shivkanthb/classify_startstop.py
Created May 24, 2017 23:56
Show Gist options
  • Save dangayle/597bbd43db01bf11a6685c28d469fcc5 to your computer and use it in GitHub Desktop.
Save dangayle/597bbd43db01bf11a6685c28d469fcc5 to your computer and use it in GitHub Desktop.
Subscribe and unsubscribe to messages
from textblob.classifiers import NaiveBayesClassifier
from textblob import TextBlob
train = [
('Take me off', 'stop'),
('Stop texting','stop'),
('stop messaging','stop'),
('Don\'t talk', 'stop'),
('Stop messaging','stop'),
('dont want to talk anymore','stop'),
('Put back on the list','join'),
('Talk again','join'),
('Start texting', 'join'),
('Keep texting','join'),
('Add to the list','join'),
('start messaging', 'join'),
('Keep messaging', 'join'),
("Dont talk", 'stop'),
("Stop sending texts", 'stop'),
('Dont spam','stop'),
('stop spamming','stop'),
('unsubscribe','stop'),
('want to join the list','join'),
('stop','stop'),
('join','join'),
('Start texting','join'),
('Do not message','stop'),
('remove from list','stop'),
('message again','join'),
('text again','join'),
('talk to me','join'),
('respond to me','join'),
('remove me from the list','stop'),
('Can you put me back on your list?','join'),
('Can you talk to me again?','join'),
('Please respond to me.','join'),
('Start talking to me.','join'),
('Talk to me.','join'),
('I want you to talk to me.','join'),
('Start messaging me.','join'),
('Start texting me.','join'),
('I want you to text me again.','join'),
('I want you to message me again.','join'),
('Keep texting me.','join'),
('Keep messaging me.','join'),
]
test = [
('Start messaging again please', 'join'),
('Keep talking to me', 'join'),
("Do not text me again", 'stop'),
("Stop the spam", 'stop'),
]
cl = NaiveBayesClassifier(train)
# Classify some text
print(cl.classify("Add me to the list")) # "join"
print(cl.classify("stop it")) # "stop"
print(cl.extract_features("Keep messaging me"))
# Classify a TextBlob
blob = TextBlob("Start", classifier=cl)
print(blob)
print(blob.classify())
# Compute accuracy
print("Accuracy: {0}".format(cl.accuracy(test)))
# Show 5 most informative features
cl.show_informative_features(5)
dist = cl.prob_classify("keep texting")
for label in dist.samples():
print("%s: %f" % (label, dist.prob(label)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment