-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup-lemmatizer.py
170 lines (128 loc) · 6.51 KB
/
lookup-lemmatizer.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
### This program is a very simple lemmatizer, which learns a
### lemmatization function from an annotated corpus. The function is
### so basic I wouldn't even consider it machine learning: it's
### basically just a big lookup table, which maps every word form
### attested in the training data to the most common lemma associated
### with that form. At test time, the program checks if a form is in
### the lookup table, and if so, it gives the associated lemma; if the
### form is not in the lookup table, it gives the form itself as the
### lemma (identity mapping).
### The program performs training and testing in one run: it reads the
### training data, learns the lookup table and keeps it in memory,
### then reads the test data, runs the testing, and reports the
### results.
### The program takes two command line arguments, which are the paths
### to the training and test files. Both files are assumed to be
### already tokenized, in Universal Dependencies format, that is: each
### token on a separate line, each line consisting of fields separated
### by tab characters, with word form in the second field, and lemma
### in the third field. Tab characters are assumed to occur only in
### lines corresponding to tokens; other lines are ignored.
import sys
import re
### Global variables
# Paths for data are read from command line
train_file = sys.argv[1]
test_file = sys.argv[2]
# train_file = 'hi_hdtb-ud-train.conllu'
# test_file = 'hi_hdtb-ud-test.conllu'
# Counters for lemmas in the training data: word form -> lemma -> count
lemma_count = {}
# Lookup table learned from the training data: word form -> lemma
lemma_max = {}
# Variables for reporting results
training_stats = ['Wordform types', 'Wordform tokens', 'Unambiguous types', 'Unambiguous tokens', 'Ambiguous types',
'Ambiguous tokens', 'Ambiguous most common tokens', 'Identity tokens']
training_counts = dict.fromkeys(training_stats, 0)
test_outcomes = ['Total test items', 'Found in lookup table', 'Lookup match', 'Lookup mismatch',
'Not found in lookup table', 'Identity match', 'Identity mismatch']
test_counts = dict.fromkeys(test_outcomes, 0)
accuracies = {}
### Training: read training data and populate lemma counters
train_data = open(train_file, 'r', encoding='utf8')
for line in train_data:
# Tab character identifies lines containing tokens
if re.search('\t', line):
# Tokens represented as tab-separated fields
field = line.strip().split('\t')
# Word form in second field, lemma in third field
form = field[1]
lemma = field[2]
######################################################
### Insert code for populating the lemma counts ###
######################################################
training_counts[training_stats[1]] += 1
if form in lemma_count:
lemma_count[form][lemma] = lemma_count[form].get(lemma, 0) + 1
else:
lemma_count[form] = {lemma: 1}
if form == lemma:
training_counts[training_stats[7]] += 1
# print(lemma_count)
### Model building and training statistics
for form in lemma_count.keys():
######################################################
### Insert code for building the lookup table ###
######################################################
if len(lemma_count.get(form)) == 1:
training_counts[training_stats[2]] += 1
training_counts[training_stats[3]] += sum(lemma_count[form].values())
else:
training_counts[training_stats[4]] += 1
training_counts[training_stats[5]] += sum(lemma_count[form].values())
training_counts[training_stats[6]] += max(lemma_count[form].values())
lemma_max[form] = max(lemma_count[form], key=lemma_count[form].get)
# print(lemma_max)
######################################################
### Insert code for populating the training counts ###
######################################################
training_counts[training_stats[0]] = len(lemma_count)
### Calculate expected accuracy if we used lookup on all items ###
accuracies['Expected lookup'] = (training_counts[training_stats[3]] + training_counts[training_stats[6]])/training_counts[training_stats[1]]
### Calculate expected accuracy if we used identity mapping on all items ###
accuracies['Expected identity'] = training_counts[training_stats[7]]/training_counts[training_stats[1]]
### Testing: read test data, and compare lemmatizer output to actual lemma
test_data = open(test_file, 'r', encoding='utf8')
for line in test_data:
# Tab character identifies lines containing tokens
if re.search('\t', line):
# Tokens represented as tab-separated fields
field = line.strip().split('\t')
# Word form in second field, lemma in third field
form = field[1]
lemma = field[2]
######################################################
### Insert code for populating the test counts ###
######################################################
test_counts[test_outcomes[0]] += 1
if form in lemma_max:
test_counts[test_outcomes[1]] += 1
if lemma_max.get(form) == lemma:
test_counts[test_outcomes[2]] += 1
else:
test_counts[test_outcomes[3]] += 1
else:
test_counts[test_outcomes[4]] += 1
if form == lemma:
test_counts[test_outcomes[5]] += 1
else:
test_counts[test_outcomes[6]] += 1
### Calculate accuracy on the items that used the lookup table ###
accuracies['Lookup'] = test_counts[test_outcomes[2]] / test_counts[test_outcomes[1]]
### Calculate accuracy on the items that used identity mapping ###
accuracies['Identity'] = test_counts[test_outcomes[5]] / test_counts[test_outcomes[4]]
### Calculate overall accuracy ###
accuracies['Overall'] = (test_counts[test_outcomes[2]] + test_counts[test_outcomes[5]]) / test_counts[test_outcomes[0]]
### Report training statistics and test results
output = open('lookup-output.txt', 'w')
output.write('Training statistics\n')
for stat in training_stats:
output.write(stat + ': ' + str(training_counts[stat]) + '\n')
for model in ['Expected lookup', 'Expected identity']:
output.write(model + ' accuracy: ' + str(accuracies[model]) + '\n')
output.write('Test results\n')
for outcome in test_outcomes:
output.write(outcome + ': ' + str(test_counts[outcome]) + '\n')
for model in ['Lookup', 'Identity', 'Overall']:
output.write(model + ' accuracy: ' + str(accuracies[model]) + '\n')
output.close