source: internals/2016/aptoideimagesdetector/trunk/Source Code/Linguage Extractor/Initial tests/nltk test2.py @ 16323

Last change on this file since 16323 was 16323, checked in by dferreira, 3 years ago

Initial linguage tests

File size: 5.2 KB
Line 
1import nltk
2import random
3from nltk.corpus import movie_reviews
4from nltk.classify.scikitlearn import SklearnClassifier
5import pickle
6
7from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
8from sklearn.linear_model import LogisticRegression, SGDClassifier
9from sklearn.svm import SVC, LinearSVC, NuSVC
10
11from nltk.classify import ClassifierI
12from statistics import mode
13
14class VoteClassifier(ClassifierI):
15        def __init__(self, *classifiers):
16                self._classifiers = classifiers
17
18        def classify(self, features):
19                votes = []
20                for c in self._classifiers:
21                        v = c.classify(features)
22                        votes.append(v)
23                return mode(votes)
24
25        def confidence(self, features):
26                votes = []
27                for c in self._classifiers:
28                        v = c.classify(features)
29                        votes.append(v)
30               
31                choice_votes = votes.count(mode(votes))
32                conf = choice_votes/len(votes)
33
34                return conf
35
36documents = []
37
38# Saves a list of (words in movie_reviews, category(positive or negative))
39for category in movie_reviews.categories():
40        for fileid in movie_reviews.fileids(category):
41                documents.append((list(movie_reviews.words(fileid)), category))
42
43random.shuffle(documents)
44
45all_words = []
46
47# Saves all words in reviews
48for w in movie_reviews.words():
49        all_words.append(w.lower())
50
51all_words = nltk.FreqDist(all_words)
52#print all_words.most_common(15)
53
54# 3000 most common words
55word_features = list(all_words.keys())[:3000]
56
57# Check if it finds features in words
58def find_features(document):
59        words = set(document)
60        features = {}
61        for w in word_features:
62                features[w] = (w in words)
63
64        return features
65
66#print find_features(movie_reviews.words('neg/cv000_29416.txt'))
67
68featuresets = [(find_features(rev), category) for (rev, category) in documents]
69
70training_set = featuresets[:1900]
71testing_set = featuresets[1900:]
72
73# posterior = prior ocurrences * likelihood/evidence
74
75#classifier = nltk.NaiveBayesClassifier.train(training_set)
76
77classifier_f = open("naivebayes.pickle", "rb")
78classifier = pickle.load(classifier_f)
79classifier_f.close()
80
81print "Original Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(classifier, testing_set))*100
82
83classifier.show_most_informative_features(15)
84
85#save_classifier = open("naivebayes.pickle", "wb")
86#pickle.dump(classifier, save_classifier)
87#save_classifier.close()
88
89MNB_classifier = SklearnClassifier(MultinomialNB())
90MNB_classifier.train(training_set)
91print "MNB_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(MNB_classifier, testing_set))*100
92
93#GNB_classifier = SklearnClassifier(GaussianNB())
94#GNB_classifier.train(training_set)
95#print "GNB_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(GNB_classifier, testing_set))*100
96
97BNB_classifier = SklearnClassifier(BernoulliNB())
98BNB_classifier.train(training_set)
99print "MNB_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(BNB_classifier, testing_set))*100
100
101LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
102LogisticRegression_classifier.train(training_set)
103print "LogisticRegression_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(LogisticRegression_classifier, testing_set))*100
104
105SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
106SGDClassifier_classifier.train(training_set)
107print "SGDClassifier_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(SGDClassifier_classifier, testing_set))*100
108
109SVCClassifier_classifier = SklearnClassifier(SVC())
110SVCClassifier_classifier.train(training_set)
111print "SVCClassifier_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(SVCClassifier_classifier, testing_set))*100
112
113LinearSVCClassifier_classifier = SklearnClassifier(LinearSVC())
114LinearSVCClassifier_classifier.train(training_set)
115print "LinearSVCClassifier_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(LinearSVCClassifier_classifier, testing_set))*100
116
117NuSVCClassifier_classifier = SklearnClassifier(NuSVC())
118NuSVCClassifier_classifier.train(training_set)
119print "LinearSVCClassifier_classifier Naive Bayes Algo Accuracy percent: ", (nltk.classify.accuracy(NuSVCClassifier_classifier, testing_set))*100
120
121voted_classifier = VoteClassifier(classifier, 
122        MNB_classifier, 
123        BNB_classifier, 
124        LogisticRegression_classifier, 
125        SGDClassifier_classifier, 
126        LinearSVCClassifier_classifier, 
127        NuSVCClassifier_classifier)
128print "Voted_classifier accuracy percent:", (nltk.classify.accuracy(voted_classifier, testing_set))*100
129
130print "Classification:", voted_classifier.classify(testing_set[0][0]), "Confidence %:", voted_classifier.confidence(testing_set[0][0])*100
131print "Classification:", voted_classifier.classify(testing_set[1][0]), "Confidence %:", voted_classifier.confidence(testing_set[1][0])*100
132print "Classification:", voted_classifier.classify(testing_set[2][0]), "Confidence %:", voted_classifier.confidence(testing_set[2][0])*100
133print "Classification:", voted_classifier.classify(testing_set[3][0]), "Confidence %:", voted_classifier.confidence(testing_set[3][0])*100
134print "Classification:", voted_classifier.classify(testing_set[4][0]), "Confidence %:", voted_classifier.confidence(testing_set[4][0])*100
135print "Classification:", voted_classifier.classify(testing_set[5][0]), "Confidence %:", voted_classifier.confidence(testing_set[5][0])*100
Note: See TracBrowser for help on using the repository browser.