-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.py
executable file
·67 lines (52 loc) · 1.58 KB
/
label.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
#!/usr/bin/python2
###################################
# CS B551 Fall 2017, Assignment #3
# D. Crandall
#
# There should be no need to modify this file, although you
# can if you really want. Edit pos_solver.py instead!
#
# To get started, try running:
#
# python ./label.py bc.train bc.test.tiny
#
from pos_scorer import Score
from pos_solver import *
import sys
# Read in training or test data file
#
def read_data(fname):
exemplars = []
file = open(fname, 'r');
for line in file:
data = tuple([w.lower() for w in line.split()])
exemplars += [ (data[0::2], data[1::2]), ]
return exemplars
####################
# Main program
#
if len(sys.argv) < 3:
print "Usage: "
print " ./label.py training_file test_file"
sys.exit()
(train_file, test_file) = sys.argv[1:3]
print "Learning model..."
solver = Solver()
train_data = read_data(train_file)
solver.train(train_data)
print "Loading test data..."
test_data = read_data(test_file)
print "Testing classifiers..."
scorer = Score()
Algorithms = ("Simplified", "HMM VE", "HMM MAP")
Algorithm_labels = [ str(i+1) + ". " + Algorithms[i] for i in range(0, len(Algorithms) ) ]
for (s, gt) in test_data:
outputs = {"0. Ground truth" : gt}
# run all algorithms on the sentence
for (algo, label) in zip(Algorithms, Algorithm_labels):
outputs[label] = solver.solve( algo, s)
posteriors = { o: solver.posterior( s, outputs[o] ) for o in outputs }
Score.print_results(s, outputs, posteriors)
scorer.score(outputs)
scorer.print_scores()
print "----"