2
\$\begingroup\$

Here is my code which takes two files of positive and negative comments and creates a training and testing set for sentiment analysis using nltk, sklearn, Python and statistical algorithms. The accuracy varies between 70-80%.

How can I improve the accuracy of my code, specifically the part of the code where it is starred like **?

import nltk import random from nltk.corpus import movie_reviews from
nltk.classify.scikitlearn import SklearnClassifier import pickle

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

from nltk.classify import ClassifierI from statistics import mode

from nltk.tokenize import word_tokenize

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


def createClassifier(posFileName, negFileName):
    short_pos = open(posFileName, "r").read()
    short_neg = open(negFileName, "r").read()

    documents = []

    for r in short_pos.split('\n'):
        documents.append((r, "pos"))
    posCount = len(documents)
    print("positive reviews", posCount)

    for r in short_neg.split('\n'):
        documents.append((r, "neg"))
    print("negative reviews", len(documents) - posCount)

    all_words = []

    short_pos_words = word_tokenize(short_pos)
    short_neg_words = word_tokenize(short_neg)

    for w in short_pos_words:
        all_words.append(w.lower())

    for w in short_neg_words:
        all_words.append(w.lower())

    **all_words = nltk.FreqDist(all_words)

    word_features = [w for (w, c) in all_words.most_common(5000)]

    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'))))

    featuresets = [(find_features(rev), category)
                   for (rev, category) in documents]

    random.shuffle(featuresets)**

    # positive data example:
    lengthFeatureSet = len(featuresets)
    print("length feature set", lengthFeatureSet)
    if(lengthFeatureSet < 100):
        print("feature set must have atleast a 100 reviews")
        return -1
    # make this modifiable
    rangeTrainingSet = int(lengthFeatureSet * 0.85)
    training_set = featuresets[:rangeTrainingSet]
    testing_set = featuresets[rangeTrainingSet:]

    ##
    # negative data example:
    ##training_set = featuresets[100:]
    ##testing_set =  featuresets[:100]

    classifier = nltk.NaiveBayesClassifier.train(training_set)
    print("Original Naive Bayes Algo accuracy percent:",
          (nltk.classify.accuracy(classifier, testing_set)) * 100)
    classifier.show_most_informative_features(15)

    MNB_classifier = SklearnClassifier(MultinomialNB())
    MNB_classifier.train(training_set)
    print("MNB_classifier accuracy percent:",
          (nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)

    BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
    BernoulliNB_classifier.train(training_set)
    print("BernoulliNB_classifier accuracy percent:",
          (nltk.classify.accuracy(BernoulliNB_classifier, testing_set)) * 100)

    LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
    LogisticRegression_classifier.train(training_set)
    print("LogisticRegression_classifier accuracy percent:", (nltk.classify.accuracy(LogisticRegression_classifier,
                                                                                     testing_set)) * 100)

    SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
    SGDClassifier_classifier.train(training_set)
    print("SGDClassifier_classifier accuracy percent:",
          (nltk.classify.accuracy(SGDClassifier_classifier, testing_set)) * 100)

    ##SVC_classifier = SklearnClassifier(SVC())
    # SVC_classifier.train(training_set)
    ##print("SVC_classifier accuracy percent:", (nltk.classify.accuracy(SVC_classifier, testing_set))*100)

    LinearSVC_classifier = SklearnClassifier(LinearSVC())
    LinearSVC_classifier.train(training_set)
    print("LinearSVC_classifier accuracy percent:",
          (nltk.classify.accuracy(LinearSVC_classifier, testing_set)) * 100)

    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(
        NuSVC_classifier,
        LinearSVC_classifier,
        MNB_classifier,
        BernoulliNB_classifier,
        LogisticRegression_classifier)

    print("voted_classifier accuracy percent:",
          (nltk.classify.accuracy(voted_classifier, testing_set)) * 100)
    save_classifier = open("23MNBCustom.pickle", "wb")
    pickle.dump(MNB_classifier, save_classifier)
    save_classifier.close()
    save_classifier = open("23BernoulliCustom.pickle", "wb")
    pickle.dump(BernoulliNB_classifier, save_classifier)
    save_classifier.close()
    save_classifier = open("23LogisticRegressionCustom.pickle", "wb")
    pickle.dump(LogisticRegression_classifier, save_classifier)
    save_classifier.close()
    save_classifier = open("23LinearCustom.pickle", "wb")
    pickle.dump(LinearSVC_classifier, save_classifier)
    save_classifier.close()
    save_classifier = open("23NuSVCCustom.pickle", "wb")
    pickle.dump(NuSVC_classifier, save_classifier)
    save_classifier.close()

    print("finished building and saving classifiers") createClassifier("positive.txt", "negative.txt")
\$\endgroup\$
  • \$\begingroup\$ Your imports seem messed up. Also your whole code is indented 3 spaces too much (fixed that) and there is a linebreak missing here: return conf def createClassifier(posFileName,negFileName): (fixed that). \$\endgroup\$ – Graipher Dec 20 '16 at 10:19
  • \$\begingroup\$ ok thanks I'ill post the gist is here in nicer format \$\endgroup\$ – gimp770 Dec 20 '16 at 19:08
  • \$\begingroup\$ gist.github.com/egimple/08e8737140cc93604ef2541d803e8987 \$\endgroup\$ – gimp770 Dec 20 '16 at 19:08
  • \$\begingroup\$ Note that the code included in the question is what counts. Do not expect anybody to actually have a look at any externally hosted code not included in the question. The code, as posted, also has to work-as-intended for it to be on-topic here. Have a look at the help center to see what exactly is on or off-topic. \$\endgroup\$ – Graipher Dec 20 '16 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.