-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsd2-voter.py
executable file
·172 lines (134 loc) · 6.49 KB
/
wsd2-voter.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
#! /usr/bin/env python
# -*- coding: utf8 -*-
import getopt
import sys
import os
import timbl
import wsd2
import codecs
import glob
def usage():
print >> sys.stderr,"Usage: wsd2-voter.py -c [classifierdir1 classifierdir2] -L [lang] -o [outputdir] -O [timbloptions] -I [divergencefrombestoutputthreshold]"
try:
opts, args = getopt.getopt(sys.argv[1:], "c:L:o:O:")
except getopt.GetoptError, err:
# print help information and exit:
print str(err)
usage()
sys.exit(2)
targetlang = None
outputdir = "."
timbloptions = "-a 0 -k 1"
testdir = wsd2.WSDDIR + "/data/trial"
targetwordsfile = wsd2.WSDDIR + "/data/targetwords.trial"
classifierdirs = []
divergencefrombestoutputthreshold = 0.9
for o, a in opts:
if o == "-c":
classifierdirs = a.split(' ')
elif o == '-L':
targetlang = a
elif o == '-o':
outputdir = a
elif o == '-O':
timbloptions = a
elif o == '-T':
testdir = a
elif o == '-w':
targetwordsfile = a
elif o == '-I':
divergencefrombestoutputthreshold = float(a)
else:
raise Exception("Unknown option: " + o)
if not classifierdirs or not targetwordsfile or not targetlang:
usage()
sys.exit(2)
classifiers = {}
targetwords = wsd2.loadtargetwords(targetwordsfile)
testfiles = []
for lemma, pos in targetwords:
if os.path.exists(testdir+"/" + lemma + '.data'):
testfiles.append(testdir+"/" + lemma + '.data')
else:
print >>sys.stderr, "WARNING: No testfile found for " + lemma + " (tried " + testdir+"/" + lemma + '.data)'
testset = wsd2.TestSet(testfiles)
votertraindata = {}
votertestdata = {}
for lemma,pos in testset.lemmas():
print >>sys.stderr, "Processing " + lemma.encode('utf-8')
votertraindata[(lemma,pos)] = {}
votertestdata[(lemma,pos)] = {}
for classifierdir in classifierdirs:
classifiername = os.path.basename(classifierdir)
votertraindata[(lemma,pos)][classifiername] = []
if os.path.exists(classifierdir + '/' + lemma + '.' + pos + '.' + targetlang + '.votertrain'):
f = codecs.open(classifierdir + '/' + lemma + '.' + pos + '.' + targetlang + '.votertrain','r','utf-8')
for line in f:
line = line.strip()
fields = line.split('\t')
votertraindata[(lemma,pos)][classifiername].append( (fields[0],fields[1]) ) #(train,gold)
f.close()
else:
raise Exception("No votertrain found for " + lemma.encode('utf-8') + " in " + classifierdir)
if os.path.exists(classifierdir + '/' + lemma + '.' + pos + '.votertest'):
f = codecs.open(classifierdir + '/' + lemma + '.' + pos + '.votertest','r','utf-8')
for line in f:
line = line.strip()
fields = line.split('\t')
id = fields[0]
if not id in votertestdata[(lemma,pos)]:
votertestdata[(lemma,pos)][id] = {}
votertestdata[(lemma,pos)][id][classifiername] = (fields[0], fields[1], fields[2]) #(id, focusword, sense)
f.close()
else:
raise Exception("No votertest found for " + lemma.encode('utf-8') + " in " + classifierdir)
#TODO: integrity check?
classifiers = {}
for lemma,pos in testset.lemmas():
print >>sys.stderr, "Processing " + lemma.encode('utf-8')
classifiers[(lemma,pos)] = timbl.TimblClassifier(outputdir + '/' + lemma + '.' + pos + '.' + targetlang, timbloptions)
col = {}
for i, classifierdir in enumerate(classifierdirs):
classifiername = os.path.basename(classifierdir)
if not i in col: col[i] = []
for data, classlabel in votertraindata[(lemma,pos)][classifiername]:
for train, gold in data:
col[i].append(train)
if i == 0: col[-1].append(gold)
classifiers[(lemma,pos)].append(features, classlabel)
print >>sys.stderr, "Training " + str(len(classifiers)) + " classifiers"
for classifier in classifiers:
classifiers[classifier].train()
#classifiers[classifier].save()
print >>sys.stderr, "Voter Parameter optimisation"
for f in glob.glob(outputdir + '/*.train'):
os.system("paramsearch ib1 " + f + " > " + f + ".paramsearch")
print >>sys.stderr, "Testing classifiers"
basictimbloptions = timbloptions
for lemma,pos in testset.lemmas():
print >>sys.stderr, "Processing " + lemma.encode('utf-8')
timbloptions = basictimbloptions
if os.path.exists(outputdir + '/' + lemma +'.' + pos + '.' + targetlang + '.train.paramsearch'):
o = wsd2.paramsearch2timblargs(outputdir + '/' + lemma +'.' + pos + '.' + targetlang + '.train.paramsearch')
timbloptions += " " + o
print >>sys.stderr, "Parameter optimisation loaded: " + o
else:
print >>sys.stderr, "NOTICE: No parameter optimisation found!"
out_best = codecs.open(outputdir + '/' + lemma + '.' + pos + '.best','w','utf-8')
out_oof = codecs.open(outputdir + '/' + lemma + '.' + pos + '.oof','w','utf-8')
oof_senses = []
for id in sorted(votertestdata[(lemma,pos)]):
features = []
for classifierdir in classifierdirs:
classifiername = os.path.basename(classifierdir)
features.append( votertestdata[(lemma,pos)][id][classifiername][-1] )
print >>sys.stderr, "--> Classifying " + id + " :" + repr(features)
sense, distribution, distance = classifiers[(lemma,pos)].classify(features)
wsd2.processresult(out_best, oof_senses, id, lemma, pos, targetlang, sense, distribution, distance, divergencefrombestoutputthreshold)
out_best.close()
wsd2.processresult_final(out_oof, oof_senses)
out_oof.close()
#score
os.system('perl ' + wsd2.WSDDIR + '/ScorerTask3.pl ' + outputdir + '/' + lemma + '.' + pos + '.best' + ' ' + wsd2.WSDDIR + '/data/trial/' + targetlang + '/' + lemma + '_gold.txt 2> ' + outputdir + '/' + lemma + '.' + pos + '.best.scorerr')
os.system('perl ' + wsd2.WSDDIR + '/ScorerTask3.pl ' + outputdir + '/' + lemma + '.' + pos + '.oof' + ' ' + wsd2.WSDDIR + '/data/trial/' + targetlang + '/' + lemma + '_gold.txt -t oof 2> ' + outputdir + '/' + lemma + '.' + pos + '.oof.scorerr')
wsd2.scorereport(outputdir)