Last active
January 13, 2016 03:51
-
-
Save sloria/6342158 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
from textblob.classifiers import NaiveBayesClassifier | |
train = [ | |
('amor', "spanish"), | |
("perro", "spanish"), | |
("playa", "spanish"), | |
("sal", "spanish"), | |
("oceano", "spanish"), | |
("love", "english"), | |
("dog", "english"), | |
("beach", "english"), | |
("salt", "english"), | |
("ocean", "english") | |
] | |
test = [ | |
("ropa", "spanish"), | |
("comprar", "spanish"), | |
("camisa", "spanish"), | |
("agua", "spanish"), | |
("telefono", "spanish"), | |
("clothes", "english"), | |
("buy", "english"), | |
("shirt", "english"), | |
("water", "english"), | |
("telephone", "english") | |
] | |
def extractor(word): | |
'''Extract the last letter of a word as the only feature.''' | |
feats = {} | |
last_letter = word[-1] | |
feats["last_letter({0})".format(last_letter)] = True | |
return feats | |
lang_detector = NaiveBayesClassifier(train, feature_extractor=extractor) | |
print(lang_detector.accuracy(test)) | |
print(lang_detector.show_informative_features(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from textblob.classifiers import NaiveBayesClassifier