-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.go
214 lines (191 loc) · 5.2 KB
/
metrics.go
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
package gnlp
import (
"math"
)
// BLEU computes a sentence-level BLEU score.
//
// Papineni, Kishore, Salim Roukos, Todd Ward, and Wei-Jing Zhu. 2002.
// "BLEU: a method for automatic evaluation of machine translation."
// In Proceedings of ACL. https://www.aclweb.org/anthology/P02-1040.pdf
//
// The candidate parameter is a sequence of token and the references parameter is
// a set of sequences of token.
// This method returns zero if there's no refernece.
func BLEU[T comparable](candidate []T, references [][]T) float64 {
return CorpusBLEU([][]T{candidate}, [][][]T{references})
}
// CorpusBLEU computes a corpus-level BLEU score.
//
// Papineni, Kishore, Salim Roukos, Todd Ward, and Wei-Jing Zhu. 2002.
// "BLEU: a method for automatic evaluation of machine translation."
// In Proceedings of ACL. https://www.aclweb.org/anthology/P02-1040.pdf
//
// The candidate list and references list should be the same length.
// Otherwise it returns zero.
//
// Note that this method doesn't return the average of sentence-level BLEU score.
// It calculates the micro-average of precision as the original BLEU paper.
func CorpusBLEU[T comparable](candidateList [][]T, referencesList [][][]T) float64 {
if len(candidateList) != len(referencesList) {
// candidate list and references list should be the same length
return 0
}
numerators := make([]int64, 4+1)
denominators := make([]int64, 4+1)
totalLength := 0
totalRefLength := 0
for idx := 0; idx < len(candidateList); idx++ {
candidate := candidateList[idx]
references := referencesList[idx]
if len(references) == 0 {
continue
}
for n := 1; n <= 4; n++ {
nn, dn := bleuModifiedPrecision(candidate, references, n)
numerators[n] += nn // no smoothing
denominators[n] += dn
}
length := len(candidate)
refLength := len(references[0])
for _, refernece := range references[1:] {
rlength := len(refernece)
if abs(length-rlength) < abs(length-refLength) ||
(abs(length-rlength) == abs(length-refLength) && rlength < refLength) {
refLength = rlength
}
}
totalLength += length
totalRefLength += refLength
}
var numerator, denominator int64 = 1, 1
for n := 1; n <= 4; n++ {
numerator *= numerators[n]
denominator *= denominators[n]
}
if numerator == 0 || denominator == 0 {
return 0
}
var precision float64 = float64(numerator) / float64(denominator)
bp := bleuBrevityPenalty(totalLength, totalRefLength)
return bp * math.Pow(precision, 0.25)
}
func bleuModifiedPrecision[T comparable](candidate []T, references [][]T, n int) (int64, int64) {
c := NGrams(candidate, n)
if len(c) == 0 {
return 0, 0
}
helper := func(a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
match := make([]bool, len(c))
for _, reference := range references {
r := NGrams(reference, n)
refMatch := make([]bool, len(r))
for i := 0; i < len(c); i++ {
for j := 0; j < len(r); j++ {
if refMatch[j] {
continue
}
if !helper(c[i], r[j]) {
continue
}
match[i] = true
refMatch[j] = true
break
}
}
}
var numerator int64 = 0
for _, b := range match {
if b {
numerator++
}
}
return numerator, int64(len(c))
}
func bleuBrevityPenalty(length, refLength int) float64 {
if length == 0 {
return 0 // avoid zero division
}
if length >= refLength {
return 1
}
return math.Exp(1 - float64(refLength)/float64(length))
}
// ROUGEN computes a ROUGE-N score,
// which is a recall-oriented text summarization metrics.
//
// Chin-Yew Lin. 2004.
// "ROUGE: A Package for Automatic Evaluation of Summaries."
// In Proceedings of ACL. https://aclanthology.org/W04-1013.pdf
func ROUGEN[T comparable](candidate []T, references [][]T, n int) float64 {
c := NGrams(candidate, n)
helper := func(a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
var numerator, denominator int
for _, reference := range references {
r := NGrams(reference, n)
if len(r) == 0 {
continue
}
match := make([]bool, len(c))
for j := 0; j < len(r); j++ {
for i := 0; i < len(c); i++ {
if match[i] {
continue
}
if !helper(c[i], r[j]) {
continue
}
match[i] = true
numerator++
}
}
denominator += len(r)
}
if denominator == 0 {
return 0
}
return float64(numerator) / float64(denominator)
}
// ROUGEL computes a ROUGE-L score.
// which is a text summarization metrics based on the longest common subsequence.
//
// Chin-Yew Lin. 2004.
// "ROUGE: A Package for Automatic Evaluation of Summaries."
// In Proceedings of ACL. https://aclanthology.org/W04-1013.pdf
func ROUGEL[T comparable](candidate []T, references [][]T) (recall, precision float64) {
if len(candidate) == 0 || len(references) == 0 {
return
}
lcsLength := 0
refLength := 0
for _, reference := range references {
lcss := LongestCommonSubsequences(candidate, reference)
lcsLength += len(lcss[0]) // lcss contains at least one sequence
refLength += len(reference)
}
if lcsLength == 0 {
return
}
recall = float64(lcsLength) / float64(refLength)
precision = float64(lcsLength) / (float64(len(candidate) * len(references)))
return
}