-
Notifications
You must be signed in to change notification settings - Fork 1
/
label.py
73 lines (52 loc) · 1.67 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
68
69
70
71
72
73
#!/usr/bin/env python3
###################################
# CS B551 Fall 2018, 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: \n./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 = ("Simple", "HMM", "Complex")
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)
# calculate posteriors for each output under each model
posteriors = { o: { a: solver.posterior( a, s, outputs[o] ) for a in Algorithms } for o in outputs }
Score.print_results(s, outputs, posteriors, Algorithms)
scorer.score(outputs, gt)
scorer.print_scores()
print("----")