-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_maxent.py
60 lines (50 loc) · 2.11 KB
/
test_maxent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from corpus import Document, NamesCorpus, ReviewCorpus
from maxent import MaxEnt
from unittest import TestCase, main
from random import shuffle, seed
import sys
class BagOfWords(Document):
def features(self):
"""Trivially tokenized words."""
return self.data.split()
class Name(Document):
def features(self):
name = self.data
return ['First=%s' % name[0], 'Last=%s' % name[-1]]
def accuracy(classifier, test, verbose=sys.stderr):
correct = [classifier.classify(x) == x.label for x in test]
if verbose:
print >> verbose, "%.2d%% " % (100 * sum(correct) / len(correct)),
return float(sum(correct)) / len(correct)
class MaxEntTest(TestCase):
u"""Tests for the MaxEnt classifier."""
# def split_names_corpus(self, document_class=Name):
# """Split the names corpus into training, dev, and test sets"""
# names = NamesCorpus(document_class=document_class)
# self.assertEqual(len(names), 5001 + 2943) # see names/README
# seed(hash("names"))
# shuffle(names)
# return (names[:5000], names[5000:6000], names[6000:])
#
# def test_names_nltk(self):
# """Classify names using NLTK features"""
# train, dev, test = self.split_names_corpus()
# classifier = MaxEnt()
# classifier.train(train, dev)
# acc = accuracy(classifier, test)
# self.assertGreater(acc, 0.70)
def split_review_corpus(self, document_class):
"""Split the yelp review corpus into training, dev, and test sets"""
reviews = ReviewCorpus('yelp_reviews.json', document_class=document_class)
seed(hash("reviews"))
shuffle(reviews)
return (reviews[:10000], reviews[10000:11000], reviews[11000:14000])
def test_reviews_bag(self):
"""Classify sentiment using bag-of-words"""
train, dev, test = self.split_review_corpus(BagOfWords)
classifier = MaxEnt()
classifier.train(train, dev)
self.assertGreater(accuracy(classifier, test), 0.55)
if __name__ == '__main__':
# Run all of the tests, print the results, and exit.
main(verbosity=2)