-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_tests.go
118 lines (110 loc) · 3.18 KB
/
init_tests.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
package symbolset
import (
"fmt"
"strings"
)
func isTestLine(l string) bool {
return strings.HasPrefix(l, "TEST ")
}
/// SYMBOL SET TESTS
type ssTest struct {
testType string
symbolType string
trans string
}
func parseSSTestLine(l string) (ssTest, error) {
fs := strings.Split(l, "\t")
if fs[0] != "TEST" {
return ssTest{}, fmt.Errorf("symbol set test line must start with TEST; found %s", l)
}
if len(fs) != 4 {
return ssTest{}, fmt.Errorf("mapper test line must have 4 fields, found %s", l)
}
tType := fs[1]
symType := fs[2]
trans := fs[3]
if tType != "ACCEPT" && tType != "REJECT" {
return ssTest{}, fmt.Errorf("invalid test type %s for test line %s", tType, l)
}
if symType != "IPA" && symType != "SYMBOLS" {
return ssTest{}, fmt.Errorf("invalid symbol type %s for test line %s", symType, l)
}
return ssTest{
testType: tType,
symbolType: symType,
trans: trans}, nil
}
func validateTranscription(ss SymbolSet, trans string) ([]string, error) {
var messages = make([]string, 0)
splitted, err := ss.SplitTranscription(trans)
if err != nil {
if strings.Contains(fmt.Sprint(err), "unknown phonemes") {
messages = append(messages, fmt.Sprint(err))
} else {
return messages, err
}
}
for _, symbol := range splitted {
if !ss.ValidSymbol(symbol) {
messages = append(
messages,
fmt.Sprintf("Invalid transcription symbol '%s' in /%s/", symbol, trans))
}
}
return messages, nil
}
func validateIPATranscription(ss SymbolSet, trans string) ([]string, error) {
var messages = make([]string, 0)
splitted, err := ss.SplitIPATranscription(trans)
if err != nil {
if strings.Contains(fmt.Sprint(err), "unknown phonemes") {
messages = append(messages, fmt.Sprint(err))
} else {
return messages, err
}
}
for _, symbol := range splitted {
if !ss.ValidIPASymbol(symbol) {
messages = append(
messages,
fmt.Sprintf("Invalid transcription symbol '%s' in /%s/", symbol, trans))
}
}
return messages, nil
}
type testResult struct {
ok bool
errors []string
}
func testSymbolSet(ss SymbolSet, tests []string) (testResult, error) {
for _, test := range tests {
t, err := parseSSTestLine(test)
if err != nil {
return testResult{}, err
}
if t.symbolType == "IPA" {
res, err := validateIPATranscription(ss, t.trans)
if err != nil {
return testResult{ok: false}, err
}
if t.testType == "ACCEPT" && len(res) > 0 {
return testResult{ok: false, errors: []string{fmt.Sprintf("accept test failed: /%s/ : %v", t.trans, res)}}, nil
}
if t.testType == "REJECT" && len(res) == 0 {
return testResult{ok: false, errors: []string{fmt.Sprintf("reject test failed: /%s/ : %v", t.trans, res)}}, nil
}
} else if t.symbolType == "SYMBOLS" {
res, err := validateTranscription(ss, t.trans)
if err != nil {
return testResult{ok: false}, err
}
if t.testType == "ACCEPT" && len(res) > 0 {
return testResult{ok: false, errors: []string{fmt.Sprintf("accept test failed: /%s/ : %v", t.trans, res)}}, nil
}
if t.testType == "REJECT" && len(res) == 0 {
return testResult{ok: false, errors: []string{fmt.Sprintf("reject test failed: /%s/ : %v", t.trans, res)}}, nil
}
}
}
return testResult{ok: true}, nil
}