-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathakshara_test.go
240 lines (222 loc) · 5.36 KB
/
akshara_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
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
package aksharamukha
import (
"testing"
"time"
"strings"
)
func TestRomanization(t *testing.T) {
// Initialize Aksharamukha
a, err := NewAksharamukha()
if err != nil {
t.Fatalf("Failed to create Aksharamukha instance: %v", err)
}
if err := a.Init(); err != nil {
t.Fatalf("Failed to initialize Aksharamukha: %v", err)
}
//defer a.Close()
// Wait for service to be ready
time.Sleep(5 * time.Second)
tests := []struct {
name string
text string
lang string
expected string
}{
{
name: "Hindi",
text: "नमस्ते",
lang: "hin",
expected: "namastē",
},
{
name: "Bengali",
text: "নমস্কার",
lang: "ben",
expected: "namaskāra",
},
{
name: "Arabic",
text: "السَّلامُ عَلَيْكُمْ",
lang: "ara",
expected: "ʾls꞉alʾmu ʿalaŷkum",
},
{
name: "Sanskrit (Devanagari)",
text: "संस्कृतम्",
lang: "san",
expected: "saṁskr̥tam",
},
{
name: "Tamil",
text: "வணக்கம்",
lang: "tam",
expected: "vaṇakkam",
},
{
name: "Telugu",
text: "నమస్కారం",
lang: "tel",
expected: "namaskāraṁ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := a.Romanize(tt.text, tt.lang)
if err != nil {
t.Errorf("Romanize() error = %v", err)
return
}
if result != tt.expected {
t.Errorf("Romanize() = %v, want %v", result, tt.expected)
}
})
}
}
func TestTransliteration(t *testing.T) {
// Initialize Aksharamukha
a, err := NewAksharamukha()
if err != nil {
t.Fatalf("Failed to create Aksharamukha instance: %v", err)
}
if err := a.Init(); err != nil {
t.Fatalf("Failed to initialize Aksharamukha: %v", err)
}
//defer a.Close()
// Wait for service to be ready
time.Sleep(5 * time.Second)
tests := []struct {
name string
text string
from Script
to Script
expected string
}{
{
name: "Devanagari to Tamil",
text: "नमस्ते",
from: Devanagari,
to: Tamil,
expected: "நமஸ்தே",
},
{
name: "Bengali to Devanagari",
text: "নমস্কার",
from: Bengali,
to: Devanagari,
expected: "नमस्कार",
},
{
name: "IAST to Devanagari",
text: "namaste",
from: IAST,
to: Devanagari,
expected: "नमस्ते",
},
{
name: "Tamil to Telugu",
text: "வணக்கம்",
from: Tamil,
to: Telugu,
expected: "వణక్కమ్",
},
{
name: "Devanagari to Malayalam",
text: "नमस्कार",
from: Devanagari,
to: Malayalam,
expected: "നമസ്കാര",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := a.TransliterateSimple(tt.text, tt.from, tt.to)
if err != nil {
t.Errorf("TransliterateSimple() error = %v", err)
return
}
if result != tt.expected {
t.Errorf("TransliterateSimple() = %v, want %v", result, tt.expected)
}
})
}
}
func TestTransliterationWithOptions(t *testing.T) {
// Initialize Aksharamukha
a, err := NewAksharamukha()
if err != nil {
t.Fatalf("Failed to create Aksharamukha instance: %v", err)
}
if err := a.Init(); err != nil {
t.Fatalf("Failed to initialize Aksharamukha: %v", err)
}
//defer a.Close()
// Wait for service to be ready
time.Sleep(5 * time.Second)
tests := []struct {
name string
text string
from Script
to Script
opts TransliterationOptions
expected string
}{
{
name: "Sanskrit to Telugu without nativization",
text: "भगवद्गीता",
from: Devanagari,
to: Telugu,
opts: TransliterationOptions{
Nativize: false,
},
expected: "భగవద్గీతా",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := a.Transliterate(tt.text, tt.from, tt.to, tt.opts)
if err != nil {
t.Errorf("Transliterate() error = %v", err)
return
}
if result != tt.expected {
t.Errorf("Transliterate() = %v, want %v", result, tt.expected)
}
})
}
}
func TestInvalidInputs(t *testing.T) {
a, err := NewAksharamukha()
if err != nil {
t.Fatalf("Failed to create Aksharamukha instance: %v", err)
}
if err := a.Init(); err != nil {
t.Fatalf("Failed to initialize Aksharamukha: %v", err)
}
//defer a.Close()
// Invalid language code
_, err = a.Romanize("test", "invalid")
if err == nil {
t.Error("Expected error for invalid language code, got nil")
}
// Empty text
_, err = a.Romanize("", "hin")
if err == nil {
t.Error("Expected error for empty text, got nil")
}
// Invalid script combination
_, err = a.TransliterateSimple("test", Script("InvalidScript"), Devanagari)
if err == nil {
t.Error("Expected error for invalid script, got nil")
}
if err != nil && !strings.Contains(err.Error(), "invalid source script") {
t.Errorf("Expected 'invalid source script' error, got: %v", err)
}
// Invalid target script
_, err = a.TransliterateSimple("test", Devanagari, Script("InvalidScript"))
if err == nil {
t.Error("Expected error for invalid target script, got nil")
}
if err != nil && !strings.Contains(err.Error(), "invalid target script") {
t.Errorf("Expected 'invalid target script' error, got: %v", err)
}
}