forked from LR-POR/cl-conllu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.lisp
429 lines (370 loc) · 13.1 KB
/
evaluate.lisp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
(in-package :cl-conllu)
;; Functions for evaluating parsers
(defvar *deprel-value-list*
'("nsubj"
"obj" "iobj"
"csubj" "ccomp" "xcomp"
"obl" "vocative"
"expl" "dislocated"
"advcl" "advmod"
"discourse"
"aux" "cop"
"mark" "nmod" "appos"
"nummod"
"acl"
"amod"
"det"
"clf"
"case"
"conj"
"cc"
"fixed"
"flat"
"compound"
"list"
"parataxis"
"orphan"
"goeswith"
"reparandum"
"punct"
"root"
"dep")
"List of the 37 universal syntactic relations in UD.")
(defun token-diff (tk1 tk2 &key (fields *token-fields*) (test #'equal) (simple-dep nil))
(loop for field in fields
for res = (if (and (equal field 'deprel) simple-dep)
(funcall test
(simple-deprel (slot-value tk1 field))
(simple-deprel (slot-value tk2 field)))
(funcall test
(slot-value tk1 field)
(slot-value tk2 field)))
unless res
collect (list field (slot-value tk1 field) (slot-value tk2 field))))
(defun sentence-diff (sent1 sent2 &key (fields *token-fields*)
(test #'equal) (simple-dep nil) (ignore-punct nil))
"Returns a list of differences in SENT1 and SENT2.
They must have the same size.
If IGNORE-PUNCT, tokens which have 'PUNCT' as upostag
in sent2 are ignored."
(assert (equal (sentence-size sent1) (sentence-size sent2)))
(let ((complete-diff
(loop for tk1 in (sentence-tokens sent1)
for tk2 in (sentence-tokens sent2)
for diff = (token-diff tk1 tk2 :fields fields :test test :simple-dep simple-dep)
when diff
collect (list (token-id tk1) diff))))
(if ignore-punct
(remove-if
#'(lambda (diff-entry)
(let ((diff-entry-id (first diff-entry)))
(equal "PUNCT"
(token-upostag (find diff-entry-id
(sentence-tokens sent2)
:key #'token-id)))))
complete-diff)
complete-diff)))
(defun sentence-average-score (list-sent1 list-sent2
&key (fields *token-fields*)
(ignore-punct t) (simple-dep nil))
"Score by sentence (macro-average).
This is the mean of the percentage of words in each sentence
that are correct with respect to the fields in FIELDS.
We assume that LIST-SENT1 is the classified result
and LIST-SENT2 is the list of golden (correct) sentences.
If IGNORE-PUNCT, tokens which have 'PUNCT' as upostag
in the golden sentences are ignored."
(let ((list-of-scores
(mapcar
#'(lambda (x y)
(- 1.0
(/ (length (sentence-diff
x y
:fields fields
:ignore-punct ignore-punct
:simple-dep simple-dep))
(sentence-size y))))
list-sent1 list-sent2)))
(/ (apply #'+ list-of-scores)
(float (length list-of-scores)))))
(defun word-average-score (list-sent1 list-sent2
&key (fields *token-fields*)
(ignore-punct t) (simple-dep nil))
"Score by word (micro-average).
This is the total mean of words in all sentences that
are correct with respect to the fields in FIELDS.
We assume that LIST-SENT1 is the classified result
and LIST-SENT2 is the list of golden (correct) sentences.
If IGNORE-PUNCT, tokens which have 'PUNCT' as upostag
in the golden sentences are ignored."
(let ((total-words (apply #'+ (mapcar #'sentence-size list-sent1)))
(wrong-words (reduce #'+
(mapcar
#'(lambda (x y)
(sentence-diff
x y
:fields fields
:ignore-punct ignore-punct
:simple-dep simple-dep))
list-sent1
list-sent2)
:key #'length
:initial-value 0)))
(- 1.0 (/ wrong-words total-words))))
(defun attachment-score-by-sentence (list-sent1 list-sent2
&key (labeled t)
(ignore-punct nil) (simple-dep nil))
"Attachment score by sentence (macro-average).
The attachment score is the percentage of words that have correct
arcs to their heads. The unlabeled attachment score (UAS) considers
only who is the head of the token, while the labeled attachment
score (LAS) considers both the head and the arc label (dependency
label / syntactic class).
In order to choose between labeled or unlabeled,
set the key argument LABELED.
References:
- Dependency Parsing. Kubler, Mcdonald and Nivre (pp.79-80)"
(sentence-average-score list-sent1 list-sent2
:fields (if labeled
'(head deprel)
'(head))
:ignore-punct ignore-punct
:simple-dep simple-dep))
(defun attachment-score-by-word (list-sent1 list-sent2
&key (labeled t)
(ignore-punct nil) (simple-dep nil))
"Attachment score by word (micro-average).
The attachment score is the percentage of words that have correct
arcs to their heads. The unlabeled attachment score (UAS) considers
only who is the head of the token, while the labeled attachment
score (LAS) considers both the head and the arc label (dependency
label / syntactic class).
In order to choose between labeled or unlabeled,
set the key argument LABELED.
References:
- Dependency Parsing. Kubler, Mcdonald and Nivre (pp.79-80)"
(word-average-score list-sent1 list-sent2
:fields (if labeled
'(head deprel)
'(head))
:ignore-punct ignore-punct
:simple-dep simple-dep))
(defun recall (list-sent1 list-sent2 deprel
&key (error-type '(deprel)) (simple-dep nil))
"Restricted to words which are originally of syntactic class
(dependency type to head) DEPREL, returns the recall:
the number of true positives divided by the number of words
originally positive (that is, originally of class DEPREL).
We assume that LIST-SENT1 is the classified result
and LIST-SENT2 is the list of golden (correct) sentences.
ERROR-TYPE defines what is considered an error (a false negative).
Some usual values are:
- '(deprel) :: for the deprel tagging task only
- '(head) :: for considering errors for each syntactic class
- '(deprel head) :: for considering correct only when both deprel
and head are correct."
(labels ((token-deprel-chosen (tk)
(if simple-dep
(simple-deprel (token-deprel tk))
(token-deprel tk))))
(assert
(and (listp error-type)
(not (null error-type)))
()
"Error: ERROR-TYPE should be a list and at least one error must be used!")
(let ((total-words
(length
(remove-if-not
#'(lambda (x)
(equal x deprel))
(mappend #'sentence-tokens
list-sent2)
:key #'token-deprel-chosen)))
(wrong-words
(length
(mapcar
#'(lambda (sent1 sent2)
(remove-if-not
#'(lambda (x)
(equal x deprel))
(sentence-diff
sent1 sent2
:fields error-type
:simple-dep simple-dep)
:key
;; deprel in the original (SENT2) sentence
#'(lambda (diff-entry)
(let ((diff-entry-id (first diff-entry)))
(token-deprel-chosen
(find diff-entry-id
(sentence-tokens sent2)
:key #'token-id))))))
list-sent1
list-sent2))))
(if (eq total-words
0)
(warn "There are no tokens correctly of deprel ~a." deprel)
(/ (float (- total-words wrong-words))
total-words)))))
(defun precision (list-sent1 list-sent2 deprel
&key (error-type '(deprel)) (simple-dep nil))
"Restricted to words which are classified as of syntactical class
(dependency type to head) DEPREL, returns the precision:
the number of true positives divided by the number of words
predicted positive (that is, predicted as of class DEPREL).
We assume that LIST-SENT1 is the classified (predicted) result
and LIST-SENT2 is the list of golden (correct) sentences.
ERROR-TYPE defines what is considered an error (a false negative).
Some usual values are:
- '(deprel) :: for the deprel tagging task only
- '(head) :: for considering errors for each syntactic class
- '(deprel head) :: for considering correct only when both deprel
and head are correct."
(labels ((token-deprel-chosen (tk)
(if simple-dep
(simple-deprel (token-deprel tk))
(token-deprel tk))))
(assert
(and (listp error-type)
(not (null error-type)))
()
"Error: ERROR-TYPE should be a list and at least one error must be used!")
(let ((classified-words
(length
(remove-if-not
#'(lambda (x)
(equal x deprel))
(mappend #'sentence-tokens
list-sent1)
:key #'token-deprel-chosen)))
(wrong-words
(length
(mapcar
#'(lambda (sent1 sent2)
(remove-if-not
#'(lambda (x)
(equal x deprel))
(sentence-diff
sent1 sent2
:fields error-type
:simple-dep simple-dep)
:key
;; deprel in the predicted (SENT1) sentence
#'(lambda (diff-entry)
(let ((diff-entry-id (first diff-entry)))
(token-deprel-chosen
(find diff-entry-id
(sentence-tokens sent1)
:key #'token-id))))))
list-sent1
list-sent2))))
(if (eq classified-words
0)
(warn "There are no tokens predicted as of deprel ~a." deprel)
(/ (float (- classified-words wrong-words))
classified-words)))))
(defun non-projectivity-accuracy (list-sent1 list-sent2)
(let ((N (length list-sent1))
(correct 0))
(mapcar
#'(lambda (x y)
(if (eq (non-projective? x)
(non-projective? y))
(incf correct)))
list-sent1
list-sent2)
(if (eq N 0)
(error "LIST-SENT1 is empty!")
(/ (float correct)
N))))
(defun non-projectivity-precision (list-sent1 list-sent2)
(let ((number-of-positives
(length (remove-if-not
#'non-projective?
list-sent1)))
(true-positives 0))
(mapcar
#'(lambda (x y)
(if (and
(eq (non-projective? x) t)
(eq (non-projective? y) t))
(incf true-positives)))
list-sent1
list-sent2)
(if (eq 0
number-of-positives)
(warn "There are no non-projective sentences in LIST-SENT1")
(/ (float true-positives)
number-of-positives))))
(defun non-projectivity-recall (list-sent1 list-sent2)
(let ((number-of-non-projectives
(length (remove-if-not
#'non-projective?
list-sent2)))
(true-positives 0))
(mapcar
#'(lambda (x y)
(if (and
(non-projective? x)
(non-projective? y))
(incf true-positives)))
list-sent1
list-sent2)
(if (eq 0
number-of-non-projectives)
(warn "There are no non-projective sentences in LIST-SENT2")
(/ (float true-positives)
number-of-non-projectives))))
(defun confusion-matrix (list-sent1 list-sent2 &key (normalize t))
"Returns a hash table where keys are lists (deprel1 deprel2) and
values are fraction of classifications as deprel1 of a word that
originally was deprel2.
We assume that LIST-SENT1 is the classified result
and LIST-SENT2 is the list of golden (correct) sentences."
(let* ((M (make-hash-table :test #'equal))
(all-words-pair-list
(mapcar
#'list
(mappend #'sentence-tokens list-sent1)
(mappend #'sentence-tokens list-sent2)))
(N (coerce (length all-words-pair-list) 'float)))
(assert
(every #'identity
(mapcar
#'(lambda (pair)
(let ((tk1 (first pair))
(tk2 (second pair)))
(and (equal (token-id tk1)
(token-id tk2))
(equal (token-form tk1)
(token-form tk2)))))
all-words-pair-list))
()
"Error: Sentence words do not match.")
(dolist (rel1 *deprel-value-list*)
(dolist (rel2 *deprel-value-list*)
(setf (gethash `(,rel1 ,rel2) M) 0)))
(dolist (pair all-words-pair-list)
(incf (gethash
(mapcar #'(lambda (tk)
(simple-deprel (token-deprel tk)))
pair)
M)))
(if normalize
(dolist (rel1 *deprel-value-list* M)
(dolist (rel2 *deprel-value-list*)
(if (not
(eq 0
(gethash `(,rel1 ,rel2) M)))
(setf (gethash `(,rel1 ,rel2) M)
(/ (gethash `(,rel1 ,rel2) M)
N))))))
M))
(defun format-matrix (matrix)
(let ((M (alexandria:hash-table-alist matrix)))
(format t "~{~15a |~^ ~}~%" (cons " " *deprel-value-list*))
(dolist (dep1 *deprel-value-list*)
(let ((L (reverse (remove-if-not #'(lambda (x) (equal x dep1)) M
:key #'(lambda (x) (first (car x)))))))
(format t "~{~15a |~^ ~}~%"
(cons dep1 (mapcar #'(lambda (x) (cdr x)) L)))))))