/
experiments.py
175 lines (141 loc) · 6.22 KB
/
experiments.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
import cPickle as pickle
import os, time
import re
from bayesmax.corpus import DSLCC
from bayesmax import bayesline
from bayesmax.evaluate import breakdown_evaluation
def test_MNB_5grams(version=2.0):
print '''
###########################################################################
# Bayesline.py : Multinomial Naive Bayes Classifier with 5-grams features.
###########################################################################
'''
dslcc = DSLCC(version)
vectorizer_file = 'bayesline-dslcc2.vectorizer'
classifier_file = "bayesline-dslcc2.clf"
if os.path.exists(vectorizer_file) and os.path.exists(classifier_file):
print 'Loading...',
start = time.time()
# Loads vectorizer and classifier.
with open(vectorizer_file, "rb") as fin:
ngram_vectorizer = pickle.load(fin)
with open(classifier_file, "rb") as fin:
classifier = pickle.load(fin)
print 'took', time.time() - start, 'secs'
else: # Trains vectorizer and classifer
print 'Training...',
start = time.time()
train_docs, train_labels = dslcc.data('train')
ngram_vectorizer, classifier = bayesline.train(train_docs, train_labels)
# Pickle dump
with open(vectorizer_file, "wb") as fout:
pickle.dump(ngram_vectorizer, fout)
with open(classifier_file, "wb") as fout:
pickle.dump(classifier, fout)
print 'took', time.time() - start, 'secs'
print '''
####################################################################
# Bayesline.py :Evaluating on dev set
####################################################################'''
print 'Predicting...',
start = time.time()
dev_docs, dev_labels = dslcc.data('devel')
results = bayesline.test(dev_docs, ngram_vectorizer, classifier)
print 'took', time.time() - start, 'secs'
print 'Evaluating...\n'
breakdown_evaluation(results, dev_labels)
print '''
####################################################################
# Evaluating on test set
####################################################################'''
print 'Predicting...',
start = time.time()
test_docs = dslcc.data('test')
results = bayesline.test(test_docs, ngram_vectorizer, classifier)
print 'took', time.time() - start, 'secs'
print 'Evaluating...\n'
_, gold_labels = dslcc.data('gold')
breakdown_evaluation(results, gold_labels)
print '''
####################################################################
# Bayesline.py :Evaluating on test-none set
####################################################################'''
print 'Predicting...',
start = time.time()
test_docs = dslcc.data('test-none')
results = bayesline.test(test_docs, ngram_vectorizer, classifier)
print 'took', time.time() - start, 'secs'
print 'Evaluating...\n'
_, gold_labels = dslcc.data('gold-none')
breakdown_evaluation(results, gold_labels)
print '#######################################################'
print'''
####################################################################
# Bayesline.py : Retraining/Reloading classifier with blinded NEs
####################################################################'''
vectorizer_none_file = 'bayesline-dslcc2-none.vectorizer'
classifier_none_file = "bayesline-dslcc2-none.clf"
if os.path.exists(vectorizer_none_file) and os.path.exists(classifier_none_file):
print 'Loading...',
start = time.time()
# Loads vectorizer and classifier.
with open(vectorizer_file, "rb") as fin:
ngram_vectorizer_none = pickle.load(fin)
with open(classifier_file, "rb") as fin:
classifier_none = pickle.load(fin)
print 'took', time.time() - start, 'secs'
else: # Trains vectorizer and classifer
print 'Training...',
start = time.time()
train_docs, train_labels = dslcc.data('train', blindne=True)
ngram_vectorizer_none, classifier_none = bayesline.train(train_docs, train_labels)
# Pickle dump
with open(vectorizer_file, "wb") as fout:
pickle.dump(ngram_vectorizer_none, fout)
with open(classifier_file, "wb") as fout:
pickle.dump(classifier_none, fout)
print 'took', time.time() - start, 'secs'
####################################################################
# Evaluating on test-none set with new classifier trained on blinded NE
####################################################################'''
print 'Predicting...',
start = time.time()
test_docs = dslcc.data('test-none')
results = bayesline.test(test_docs, ngram_vectorizer_none, classifier_none)
print 'took', time.time() - start, 'secs'
print 'Evaluating...\n'
_, gold_labels = dslcc.data('gold-none')
breakdown_evaluation(results, gold_labels)
print '#######################################################'
def test_MNB_Ngram(min_n, max_n, version=2.0):
dslcc = DSLCC(version)
print 'Training n=('+','.join([str(min_n), str(max_n)])+')...',
start = time.time()
train_docs, train_labels = dslcc.data('train')
ngram_vectorizer, classifier = bayesline.train(train_docs, train_labels,
min_n, max_n)
print 'took', time.time() - start, 'secs'
print 'Predicting...',
start = time.time()
test_docs = dslcc.data('test')
results = bayesline.test(test_docs, ngram_vectorizer, classifier)
print 'took', time.time() - start, 'secs'
print 'Evaluating...\n'
_, gold_labels = dslcc.data('gold')
print breakdown_evaluation(results, gold_labels, human_readable=False,
overall_only=True)
print '#######################################################'
def test_MNB_126grams(version=2.0):
for i in range(1,7):
test_MNB_Ngram(i,i)
if i != 1:
test_MNB_Ngram(1,i)
#test_MNB_5grams()
test_MNB_Ngram(1,6)
'''
Training n=(5,6)... took 138.935706854 secs
Predicting... took 4.82505702972 secs
Evaluating...
0.940142857143
'''