def train(labeled_featuresets, label_freqdist, estimator=ELEProbDist): ... # Create the P(label) distribution label_probdist = estimator(label_freqdist) ... # Create the P(fval|label, fname) distribution feature_probdist = {} ... return NaiveBayesClassifier(label_probdist, feature_probdist)
def train(self, train): model = NaiveBayesClassifier(train) ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S') model_save = open('classifiers/models/naive_bayes' + st + '.pickle', 'wb') pickle.dump(model, model_save) model_save.close() return st
print(nb_classifier.classify(posfeat)) print(accuracy(nb_classifier, test_feats)) probs = nb_classifier.prob_classify(test_feats[0][0]) print(probs.samples()) print(probs.max()) print(probs.prob('pos')) print(probs.prob('neg')) print(nb_classifier.most_informative_features(n=5)) print("############################################################################") print(nb_classifier.show_most_informative_features(n=5)) print("############################################################################") nb_classifier = NaiveBayesClassifier.train(train_feats, estimator=LaplaceProbDist) print("Accuracy: " + str(accuracy(nb_classifier, test_feats))) # Accuracy: 0.76 label_probdist = DictionaryProbDist({'pos': 0.5, 'neg': 0.5}) true_probdist = DictionaryProbDist({True: 1}) feature_probdist = {('pos', 'yes'): true_probdist, ('neg', 'no'): true_probdist} classifier = NaiveBayesClassifier(label_probdist, feature_probdist) print(classifier.classify({'yes': True})) print(classifier.classify({'no': True}))
@author: noviayou """ from nltk.classify import NaiveBayesClassifier def word_feats(words): return dict([(word, True) for word in words]) positive_vocab = [ "awesome", "innovation", "outstanding", "fantastic", "terrific", "good", "great" ] negative_vocab = ["bad", "terrible", "useless", "hate"] netural_vocab = ["Blockchain", "is", "was", "know", "technology"] positive_features = [(word_feats(pos), "pos") for pos in positive_vocab] negative_features = [(word_feats(neg), "neg") for neg in negative_vocab] netural_features = [(word_feats(neu), "neu") for neu in netural_vocab] train = positive_features + negative_features + netural_features cl = NaiveBayesClassifier(train) twi = r"/Users/noviayou/Downloads/twitter" import json day1 = r"/Users/noviayou/Downloads/twitter/00.json" text = json.loads(day1) print(text)
def _load_bayes_clf(data: dict, version: int) -> NaiveBayesClassifier: return NaiveBayesClassifier(data['label_pdist'], data['feature_pdist'])
def __init__(self): self.model = {} # {Sentence():'label'} self.words = [] self.sentim = SentimentAnalyzer() self.classify = NaiveBayesClassifier()