-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.py
More file actions
244 lines (178 loc) · 8.43 KB
/
script.py
File metadata and controls
244 lines (178 loc) · 8.43 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from __future__ import division
import itertools
import operator
import constants
import extractor
import learner
import output
import pairer
import reader
# FUNCTIONS
### Rule-Based Baselines ###
def pairwiseDeduction(measure):
# Feature extraction
ext = extractor.Extractor()
if measure == constants.IDENTICAL_WORDS:
ext.identicalWordsBaseline(prr.examples, prr.labels)
elif measure == constants.IDENTICAL_PREFIX:
ext.identicalPrefixBaseline(prr.examples, prr.labels)
elif measure == constants.IDENTICAL_LETTER:
ext.identicalFirstLetterBaseline(prr.examples, prr.labels)
predictions = ext.testExamples.reshape((ext.testExamples.shape[0],))
# Evaluation
lrn = learner.Learner()
accuracy = lrn.computeAccuracy(ext.testLabels, predictions)
F1 = lrn.computeF1(ext.testLabels, predictions)
report = lrn.evaluatePairwise(ext.testLabels, predictions)
# Reporting
output.reportPairwiseDeduction(constants.DEDUCERS[measure], prr, accuracy, F1, report)
output.savePredictions("output/Pairwise " + constants.DEDUCERS[measure] + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions, ext.testLabels)
return predictions
def groupDeduction(measure):
# Feature extraction
ext = extractor.Extractor()
if measure == constants.IDENTICAL_WORDS:
predictedLabels, predictedSets = ext.identicalWordsGroupBaseline(prr.testMeanings, prr.testLanguages, rdr.wordforms)
elif measure == constants.IDENTICAL_PREFIX:
predictedLabels, predictedSets = ext.identicalPrefixGroupBaseline(prr.testMeanings, prr.testLanguages, rdr.wordforms)
elif measure == constants.IDENTICAL_LETTER:
predictedLabels, predictedSets = ext.identicalFirstLetterGroupBaseline(prr.testMeanings, prr.testLanguages, rdr.wordforms)
trueLabels = ext.extractGroupLabels(rdr.cognateSets, rdr.wordforms, prr.testMeanings, prr.testLanguages)
# Evaluation
lrn = learner.Learner()
V1scores = {meaningIndex: lrn.computeV1(trueLabels[meaningIndex], predictedLabels[meaningIndex]) for meaningIndex in prr.testMeanings}
# Reporting
output.reportGroup(constants.DEDUCERS[measure], V1scores, rdr.meanings)
output.saveGroup("output/Group " + constants.DEDUCERS[measure] + ".txt", predictedSets)
### Hauer & Kondrak, 2011 ###
def HK2011Pairwise(twoStage = False):
# 1st Pass
# Feature extraction
ext = extractor.Extractor()
ext.HK2011Baseline(prr.examples, prr.labels)
# Learning
lrn = learner.Learner()
lrn.initSVM(0.1)
lrn.fitSVM(ext.trainExamples, ext.trainLabels)
# Prediction
predictions1 = lrn.predictSVM(ext.testExamples)
# Evaluation
accuracy = lrn.computeAccuracy(ext.testLabels, predictions1)
F1 = lrn.computeF1(ext.testLabels, predictions1)
report = lrn.evaluatePairwise(ext.testLabels, predictions1)
# Reporting
stage = "HK2011 1st Pass"
output.reportPairwiseLearning(stage, prr, accuracy, F1, report)
output.savePredictions("output/" + stage + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions1, ext.testLabels)
# 2nd Pass
if twoStage:
# Feature extraction
ext.appendBinaryLanguageFeatures(prr.examples, prr.labels, constants.TEST, prr.testLanguages)
# Learning
lrn = learner.Learner()
lrn.initSVM(0.0001)
lrn.fitSVM(ext.testExamples, predictions1)
# Prediction
predictions2 = lrn.predictSVM(ext.testExamples)
# Evaluation
accuracy = lrn.computeAccuracy(ext.testLabels, predictions2)
F1 = lrn.computeF1(ext.testLabels, predictions2)
report = lrn.evaluatePairwise(ext.testLabels, predictions2)
# Reporting
stage = "HK2011 2nd Pass"
output.reportPairwiseLearning(stage, prr, accuracy, F1, report)
output.savePredictions("output/" + stage + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions2, ext.testLabels)
# Significance
print constants.SIGNIFICANCE.format(lrn.computeMcNemarSignificance(ext.testLabels, predictions1, predictions2))
return ext, lrn
def HK2011Clustering(ext, lrn, twoStage = False):
# Feature extraction
trueLabels = ext.extractGroupLabels(rdr.cognateSets, rdr.wordforms, prr.testMeanings, prr.testLanguages)
extractor = ext.HK2011ExtractorFull if twoStage else ext.HK2011Extractor
# Learning
threshold = constants.T2 if twoStage else constants.T1
predictedLabels, predictedSets, clusterCounts, clusterDistances = lrn.cluster(constants.SVM, threshold, rdr.wordforms, rdr.POSTags, prr.testMeanings, prr.testLanguages, extractor)
# Evaluation
V1scores = {meaningIndex: lrn.computeV1(trueLabels[meaningIndex], predictedLabels[meaningIndex]) for meaningIndex in prr.testMeanings}
# Reporting
output.reportCluster(V1scores, clusterCounts, clusterDistances, rdr.meanings)
output.saveGroup("output/Clustering.txt", predictedSets)
### Combined Approach ###
def treeFeatureSelection():
# Feature extraction
ext = extractor.Extractor()
ext.appendWordSimilarityFeatures(prr.examples, prr.labels, ext.allMeasures)
# Feature selection
lrn = learner.Learner()
lrn.initForest(250, 0)
lrn.fitForest(ext.trainExamples, ext.trainLabels)
importances = lrn.getForestImportances()
# Reporting
for i, feature in enumerate(ext.allMeasures):
print "{0}: {1:.4f}".format(feature, importances[i])
def editOperations():
# Feature extraction
ext = extractor.Extractor()
operations = ext.extractEditOps(prr.examples, prr.labels)
def pairwiseLearning(minimal = False):
# Feature extraction
ext = extractor.Extractor()
ext.consonantPrep = rdr.consonants
ext.soundClassPrep = rdr.soundClasses
if minimal:
ext.appendWordSimilarityFeatures(prr.examples, prr.labels, ext.minimalMeasures)
ext.appendPOSTags(prr.examples, prr.labels, rdr.POSTags)
else:
ext.appendWordSimilarityFeatures(prr.examples, prr.labels, [ext.commonBigramRatio, ext.commonTrigramNumber, ext.bigramDice, ext.jaroDistance])
ext.appendWordSimilarityFeatures(prr.examples, prr.labels, [ext.identicalWords], rdr.consonants)
ext.appendWordSimilarityFeatures(prr.examples, prr.labels, [ext.LCPLength, ext.commonBigramNumber, ext.identicalPrefix], rdr.soundClasses)
ext.appendPOSTags(prr.examples, prr.labels, rdr.POSTags)
ext.appendLetterFeatures(prr.examples, prr.labels)
ext.appendSameLanguageGroupFeatures(prr.examples, prr.labels)
# Learning
lrn, predictions = learn(ext, 0.0001)
# Reporting
stage = "Pairwise Learning"
accuracy = lrn.computeAccuracy(ext.testLabels, predictions)
F1 = lrn.computeF1(ext.testLabels, predictions)
report = lrn.evaluatePairwise(ext.testLabels, predictions)
output.reportPairwiseLearning(stage, prr, accuracy, F1, report)
output.savePredictions("output/" + stage + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions, ext.testLabels)
return ext, lrn
def groupLearning(ext, lrn, minimal = False):
# Feature extraction
trueLabels = ext.extractGroupLabels(rdr.cognateSets, rdr.wordforms, prr.testMeanings, prr.testLanguages)
extractor = ext.minimalExtractor if minimal else ext.combinedExtractor
# Learning
threshold = constants.T3 if minimal else constants.T4
predictedLabels, predictedSets, clusterCounts, clusterDistances = lrn.cluster(constants.LR, threshold, rdr.wordforms, rdr.POSTags, prr.testMeanings, prr.testLanguages, extractor)
# Evaluation
V1scores = {meaningIndex: lrn.computeV1(trueLabels[meaningIndex], predictedLabels[meaningIndex]) for meaningIndex in prr.testMeanings}
# Reporting
output.reportCluster(V1scores, clusterCounts, clusterDistances, rdr.meanings)
output.saveGroup("output/Clustering.txt", predictedSets)
def learn(ext, C):
# Learning
lrn = learner.Learner()
lrn.initLogisticRegression(C)
lrn.fitLogisticRegression(ext.trainExamples, ext.trainLabels)
# Prediction
predictions = lrn.predictLogisticRegression(ext.testExamples)
return lrn, predictions
# FLOW
# Reading
rdr = reader.Reader()
rdr.read()
# Data division
trainMeanings = [i for i in range(1, constants.MEANING_COUNT + 1) if (i % 10 != 0 and i % 10 != 5)]
devMeanings = [i for i in range(1, constants.MEANING_COUNT + 1) if i % 10 == 5]
testMeanings = [i for i in range(1, constants.MEANING_COUNT + 1) if i % 10 == 0]
trainLanguages = constants.LANGUAGE_GROUPS[1] + constants.LANGUAGE_GROUPS[2] + constants.LANGUAGE_GROUPS[3]
testLanguages = constants.LANGUAGE_GROUPS[0] + constants.LANGUAGE_GROUPS[4] + constants.LANGUAGE_GROUPS[5] + constants.LANGUAGE_GROUPS[6] + constants.LANGUAGE_GROUPS[8] + constants.LANGUAGE_GROUPS[7]
# Pairing
prr = pairer.Pairer()
#prr.pairBySpecificMeaning(rdr.cognateCCNs, rdr.dCognateCCNs, trainMeanings, devMeanings)
prr.pairBySpecificLanguage(rdr.cognateCCNs, rdr.dCognateCCNs, trainLanguages, testLanguages)
# Learning
ext, lrn = pairwiseLearning()
groupLearning(ext, lrn)