def _get_classifier(self): from twentiment.naivebayes import NaiveBayesClassifier # from nltk.classify.naivebayes import NaiveBayesClassifier training_features = [ ({'nice': True, 'pretty': True}, 'pos'), ({'ugly': True, 'bald': True}, 'neg') ] return NaiveBayesClassifier.train(training_features)
def setUp(self): pos_tweets = [('I love this car', 'positive'), ('This view is amazing', 'positive'), ('I feel great this morning', 'positive'), ('I am so excited about the concert', 'positive'), ('He is my best friend', 'positive')] neg_tweets = [('I do not like this car', 'negative'), ('This view is horrible', 'negative'), ('I feel tired this morning', 'negative'), ('I am not looking forward to the concert', 'negative'), ('He is my enemy', 'negative')] tweets = [] for (words, sentiment) in pos_tweets + neg_tweets: tweets.append((normalize_text(words), sentiment)) training_set = [(extract_features(doc), label) for (doc, label) in tweets] self.classifier = NaiveBayesClassifier.train(training_set)
def from_training_set(cls, training_set): """Creates a new instance from the given training set.""" classifier = NaiveBayesClassifier.train(training_set) return cls(classifier)