Search code examples
pythonnltktextblob

How to get the accuracy of new phrases with TextBlob?


I am making a classifier using the NaiveBayes model to classify a user's requests for location, weather, etc...

The classifications I am returning look like this:

("What is the weather in Copenhagen", "weather")

These requests are then trained and compared against a test set, which return the accuracy. This a works well.

c = NaiveBayesClassifier(train_set)
self.classifier = Blobber(analyzer=NaiveBayesAnalyzer(), classifier=c)
print(c.accuracy(test_set))

I am running this method to classify new phrases

    def classify_phrase(self, tb_phrase):
        return self.classifier(tb_phrase).classify()

However, when I try to classify a new phrase that doesn't fall into one of my classification or is an error from the user it still tries to classify it as a request. Example below:

("Where is Bob", "location")

This should return an error, but it does not. Is there a way to get the accuracy from the Textblob on a newly entered phrase? So that when I enter a phrase, it will tell me the accuracy of that phrase. I am using the Textblob Package and Python 3. If more information is needed, the full code is under the NaturalLanguage.py file on my GitHub. Thank you in advance.


Solution

  • I resolved my own problem using the prob_classify method in textBlob. This returns the probability of a given classification. Phrase is a string and classification is class-action from a NaiveBayes Classifier.

        def classifier_prob(self, phrase, classification):
             return self.classifier.prob_classify(phrase).prob(classification)