-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics_test.go
112 lines (102 loc) · 3.38 KB
/
metrics_test.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
package gnlp_test
import (
"fmt"
"math"
"strings"
"testing"
"github.com/shota3506/gnlp"
)
func TestBLEU(t *testing.T) {
candidate := strings.Split("The NASA Opportunity rover is battling a massive dust storm on Mars .", " ")
references := [][]string{
strings.Split("The Opportunity rover is combating a big sandstorm on Mars .", " "),
strings.Split("A NASA rover is fighting a massive storm on Mars .", " "),
}
bleu := gnlp.BLEU(candidate, references)
expected := 0.3277456805
if math.Abs(expected-bleu) > 1e-9 {
t.Errorf("epected: %f, actual: %f", expected, bleu)
}
}
func TestCorpusBLEU(t *testing.T) {
for i, tc := range []struct {
candidateList [][]string
referencesList [][][]string
expected float64
}{
{
candidateList: [][]string{
strings.Split("The NASA Opportunity rover is battling a massive dust storm on Mars .", " "),
},
referencesList: [][][]string{
{
strings.Split("The Opportunity rover is combating a big sandstorm on Mars .", " "),
strings.Split("A NASA rover is fighting a massive storm on Mars .", " "),
},
},
expected: 0.3277456805,
},
{
candidateList: [][]string{
strings.Split("It is a guide to action which ensures that the military always obeys the commands of the party", " "),
strings.Split("he read the book because he was interested in world history", " "),
},
referencesList: [][][]string{
{
strings.Split("It is a guide to action that ensures that the military will forever heed Party commands", " "),
strings.Split("It is the guiding principle which guarantees the military forces always being under the command of the Party", " "),
strings.Split("It is the practical guide for the army always to heed the directions of the party", " "),
},
{
strings.Split("he was interested in world history because he read the book", " "),
},
},
expected: 0.5920778869,
},
} {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
bleu := gnlp.CorpusBLEU(tc.candidateList, tc.referencesList)
if math.Abs(tc.expected-bleu) > 1e-9 {
t.Errorf("epected: %f, actual: %f", tc.expected, bleu)
}
})
}
}
func TestROUGEN(t *testing.T) {
candidate := strings.Split("How we can travel faster than light ?", " ")
references := [][]string{
strings.Split("Is faster than light travel possible ?", " "),
strings.Split("Is there any way to travel faster than light ?", " "),
}
for i, tc := range []struct {
n int
expected float64
}{
{1, 0.5882352941},
{2, 0.4},
{3, 0.3076923077},
} {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
rouge := gnlp.ROUGEN(candidate, references, tc.n)
if math.Abs(tc.expected-rouge) > 1e-9 {
t.Errorf("epected: %f, actual: %f", tc.expected, rouge)
}
})
}
}
func TestROUGEL(t *testing.T) {
candidate := strings.Split("How we can travel faster than light ?", " ")
references := [][]string{
strings.Split("Is faster than light travel possible ?", " "),
strings.Split("Is there any way to travel faster than light ?", " "),
}
var expectedReall float64 = 0.5294117647
var expectedPrecision float64 = 0.5625
recall, precision := gnlp.ROUGEL(candidate, references)
if math.Abs(expectedReall-recall) > 1e-9 {
t.Errorf("epected: %f, actual: %f", expectedReall, recall)
}
if math.Abs(expectedPrecision-precision) > 1e-9 {
t.Errorf("epected: %f, actual: %f", expectedPrecision, precision)
}
}