<jupyter_start><jupyter_text>p.18 Better Training Data<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo('UF-RyxOAHQw')<jupyter_output><empty_output><jupyter_text>1. A new datasetMuch shorter movie reviews at https://pythonprogramming.net/static/downloads/short_reviews/. 2. Example<jupyter_code>import nltk
import random
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.classify.scikitlearn import SklearnClassifier
import pickle

from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC

from nltk.classify import ClassifierI
from statistics import mode

class VoteClassifier(ClassifierI):
    def __init__(self, *classifiers):
        self._classifiers = classifiers
        
    def classify(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
            
        return mode(votes)
    
    def confidence(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
            
        choice_votes = votes.count(mode(votes))
        conf = choice_votes/len(votes)
        
        return conf

# If you see the "UnicodeDecodeError", add the options "encoding='utf-8', errors='replace'".
short_pos = open("short_reviews/positive.txt", "r", encoding='utf-8', errors='replace').read()
short_neg = open("short_reviews/negative.txt", "r", encoding='utf-8', errors='replace').read()

documents = []

# Note that each entry of documents is a short review, not a single word from the short review.
for r in short_pos.split('\n'):
    documents.append((r, "pos"))
    
for r in short_neg.split('\n'):
    documents.append((r, "neg"))
    
all_words = []
    
short_pos_words = word_tokenize(short_pos)
short_neg_words = word_tokenize(short_neg)

# Remove the stop words and the punctuations.
stop_words = set(stopwords.words("english"))
stop_words = stop_words.union(set(string.punctuation))
#print("stop_words:\n", stop_words)

for w in short_pos_words:
    if w.lower() not in stop_words:
        all_words.append(w.lower())
    
for w in short_neg_words:
    if w.lower() not in stop_words:
        all_words.append(w.lower())
    
all_words = nltk.FreqDist(all_words)

# Restrict our 'features' to the most common 5000 words.
word_features = all_words.most_common(5000)
word_features = [wf[0] for wf in word_features]

# Check if each of the most common 5000 words is present in one movie review.
# The input document is a short review.
def find_features(document):
    words = word_tokenize(document)
    features = {}
    for w in word_features:
        features[w] = (w in words)
        
    return features

# print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))

# Label the 'features' in all the movie reviews.
featuresets = [(find_features(rev), category) for (rev, category) in documents]

random.shuffle(featuresets)

# Partition the entire data set into training set and test set.
training_set = featuresets[:10000]
testing_set = featuresets[10000:]

## 
## Trained naive Bayes classifier
##
# Don't load this naive Bayes classfier which was trained for the long movie reviews.
#classifier_f = open("naivebayes.pickle", "rb")
#classifier = pickle.load(classifier_f)
#classifier_f.close()

#print("Naive Bayes Algo accuracy percent:", (nltk.classify.accuracy(classifier, testing_set))*100)
#classifier.show_most_informative_features(15)

##
## Scikit-Learn MultinomialNB
##
MultinomialNB_classifier = SklearnClassifier(MultinomialNB())
MultinomialNB_classifier.train(training_set)

print("MNB_classifier accuracy percent:", (nltk.classify.accuracy(MultinomialNB_classifier, testing_set))*100)

##
## Scikit-Learn GaussianNB
##
# GaussianNB_classifier = SklearnClassifier(GaussianNB())
# GaussianNB_classifier.train(training_set)

# print("GaussianNB_classifier accuracy percent:", (nltk.classify.accuracy(GaussianNB_classifier, testing_set))*100)

##
## Scikit-Learn BernoulliNB
##
BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
BernoulliNB_classifier.train(training_set)

print("BernoulliNB_classifier accuracy percent:", (nltk.classify.accuracy(BernoulliNB_classifier, testing_set))*100)

##
## Scikit-Learn LogisticRegression
##
LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
LogisticRegression_classifier.train(training_set)

print("LogisticRegression_classifier accuracy percent:", (nltk.classify.accuracy(LogisticRegression_classifier, testing_set))*100)

##
## Scikit-Learn SGDClassifier
##
SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
SGDClassifier_classifier.train(training_set)

print("SGDClassifier_classifier accuracy percent:", (nltk.classify.accuracy(SGDClassifier_classifier, testing_set))*100)

##
## Scikit-Learn SVC
##
# The performance of the classic SVC is poor, so it is NOT used.
#SVC_classifier = SklearnClassifier(SVC())
#SVC_classifier.train(training_set)

#print("SVC_classifier accuracy percent:", (nltk.classify.accuracy(SVC_classifier, testing_set))*100)

##
## Scikit-Learn LinearSVC
##
LinearSVC_classifier = SklearnClassifier(LinearSVC())
LinearSVC_classifier.train(training_set)

print("LinearSVC_classifier accuracy percent:", (nltk.classify.accuracy(LinearSVC_classifier, testing_set))*100)

##
## Scikit-Learn NuSVC
##
NuSVC_classifier = SklearnClassifier(NuSVC())
NuSVC_classifier.train(training_set)

print("NuSVC_classifier accuracy percent:", (nltk.classify.accuracy(NuSVC_classifier, testing_set))*100)

voted_classifier = VoteClassifier(#classifier, 
                                  MultinomialNB_classifier, 
                                  BernoulliNB_classifier, 
                                  LogisticRegression_classifier, 
                                  #SGDClassifier_classifier, 
                                  LinearSVC_classifier, 
                                  NuSVC_classifier)
print("voted_classifier accuracy percent:", (nltk.classify.accuracy(voted_classifier, testing_set))*100)

# print("Classification: ", voted_classifier.classify(testing_set[0][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[0][0])*100)
# print("Classification: ", voted_classifier.classify(testing_set[1][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[1][0])*100)
# print("Classification: ", voted_classifier.classify(testing_set[2][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[2][0])*100)
# print("Classification: ", voted_classifier.classify(testing_set[3][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[3][0])*100)
# print("Classification: ", voted_classifier.classify(testing_set[4][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[4][0])*100)
# print("Classification: ", voted_classifier.classify(testing_set[5][0]), 
#       "Confidence %: ", voted_classifier.confidence(testing_set[5][0])*100)<jupyter_output>MNB_classifier accuracy percent: 81.47590361445783
BernoulliNB_classifier accuracy percent: 80.87349397590361
LogisticRegression_classifier accuracy percent: 80.12048192771084
{'fraction_non_alphanumeric': 0.11275517194136184, 'fraction_numerical': 0.02192081107000959, 'mean_word_length': 4.863453815261044, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '93577', 'license': 'Apache-2.0', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2332, 'n_words': 450, 'path': 'wei/p18.ipynb', 'repo_name': 'GroupStudyInSammamishArea/pythonnltktutorial', 'size': '7299'}