-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_codecs_test.go
610 lines (565 loc) · 13.4 KB
/
language_codecs_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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package xl8r
import (
"fmt"
"strings"
)
/***********************************************************************
* PLEASE BE ADVISED: *
* The translations represented here are only for feature illustration *
* and testing and purposes. *
* Some of the translations may not be 100% accurate. *
***********************************************************************/
/****************
* Codecs Setup *
****************/
// all "Origins" for our demo language translator use this data type
// - when encoding, values of this type get converted to the "hub data" type
// - when decoding, "hub data" values get converted to this type
type myLanguageContentType string
func (s myLanguageContentType) toWords() []string {
return strings.Split(string(s), " ")
}
// all "Origins" for our demo language translator use this data type
// - when encoding, values of the "content type" get converted to this type
// - when decoding, values of this type get converted to the "content type"
type myLanguageHubDataType []int
func (h myLanguageHubDataType) asArray() (r []int) {
for _, v := range h {
r = append(r, v)
}
return
}
// low-level helper function
func testContentWithEncMap(v myLanguageContentType, encMap map[string]int, f ...func(word string, wordOK bool)) bool {
act := func(_ string, _ bool) {}
if len(f) > 0 {
act = f[0]
}
for _, word := range v.toWords() {
word = strings.ToLower(strings.TrimSpace(word))
if _, exists := encMap[word]; !exists {
act(word, false)
return false
}
act(word, true)
}
return true
}
// low-level helper function
func testDecodeUsingMap(v myLanguageHubDataType, decMap map[int]string) (r myLanguageContentType, e error) {
result := &strings.Builder{}
for i, n := range v {
if i > 0 {
result.WriteString(" ")
}
if word, exists := decMap[n]; exists {
result.WriteString(word)
} else {
e = fmt.Errorf("{unknown: %d}", n)
result.WriteString(e.Error())
}
}
r = myLanguageContentType(result.String())
return
}
func testFixQuoteChars(s, replaceWith string,replacing ... string) (r string) {
r = s
for _, replaceable := range replacing {
r = strings.ReplaceAll(r,replaceable,replaceWith)
}
return
}
func testAddNumericEncoding(encMap map[string]int) {
if encMap == nil {
return
}
//make sure numerals are encodable ...
for i :=0 ; i < 11 ; i++ {
hI := fmt.Sprint(i)
if _, hasAlready := encMap[hI]; ! hasAlready {
encMap[hI]=i
}
}
}
// make a *Spoke (ie. a codec) using the specified name and helper maps for encoding and decoding values
func fetchXSpoke(id string, encMap map[string]int, decMap map[int]string) (r *Spoke[myLanguageContentType, myLanguageHubDataType]) {
testAddNumericEncoding(encMap)
eval := func(v myLanguageContentType) bool {
return testContentWithEncMap(v, encMap)
}
r = &Spoke[myLanguageContentType, myLanguageHubDataType]{
Id: id,
Enc: func(v myLanguageContentType, opts0 ...Opts) (r myLanguageHubDataType, e error) {
if len(v) == 0 {
return
}
// create the hub data, from the content, using the encoder map
testContentWithEncMap(v, encMap, func(word string, wordOK bool) {
switch wordOK {
case true:
r = append(r, encMap[word])
default:
e = fmt.Errorf("unknown word: '%s'", word)
}
})
return
},
Dec: func(v myLanguageHubDataType, opts0 ...Opts) (r myLanguageContentType, e error) {
if l := len(v); l > 0 {
// create the content, from the hub data, using the decoder map
r, e = testDecodeUsingMap(v, decMap)
}
return
},
Check: eval,
}
return
}
// generate the codec for English numbers
func fetchEngCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
return fetchXSpoke(
"english",
map[string]int{
"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
},
map[int]string{
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
10: "ten",
},
)
}
// generate the codec for Spanish numbers
func fetchSpanishCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
return fetchXSpoke(
"spanish",
map[string]int{
"cero": 0,
"uno": 1,
"dos": 2,
"tres": 3,
"cuatro": 4,
"cinco": 5,
"seis": 6,
"siete": 7,
"ocho": 8,
"nueve": 9,
"diez": 10,
},
map[int]string{
0: "cero",
1: "uno",
2: "dos",
3: "tres",
4: "cuatro",
5: "cinco",
6: "seis",
7: "siete",
8: "ocho",
9: "nueve",
10: "diez",
},
)
}
// generate the codec for Haitian Creole numbers
func fetchHaitianCreoleCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
return fetchXSpoke(
"haitian creole",
map[string]int{
"zèro": 0,
"en": 1,
"de": 2,
"twa": 3,
"kat": 4,
"senk": 5,
"sis": 6,
"sèt": 7,
"wit": 8,
"nèf": 9,
"dis": 10,
},
map[int]string{
0: "zèro",
1: "en",
2: "de",
3: "twa",
4: "kat",
5: "senk",
6: "sis",
7: "sèt",
8: "wit",
9: "nèf",
10: "dis",
},
)
}
// generate the codec for Japanese numbers
func fetchJapaneseCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
/**********************************************************************
* This codec has special handling for optional translations *
* A custom decoder function is used, in place of *
* the default decoder provided by the fetchXSpoke(..) function *
* The custom decoder honors the user-defined Dec option called "use" *
* and will employ the appropriate decoder map, as directed. *
* If a value is not recognized, then the common decoder map is used.*
**********************************************************************/
/***************************************************************
* PLEASE BE ADVISED: *
* The maps represented here are only for feature illustration *
* and testing and purposes. *
* Some of the maps are not 100% accurate. *
***************************************************************/
toCommonMap := map[int]string{
0: "rei",
1: "ichi",
2: "ni",
3: "san",
4: "shi",
5: "go",
6: "roku",
7: "nana",
8: "hachi",
9: "kyu",
10: "ju",
}
toKunyomiMap := map[int]string{
0: "zero",
1: "hito",
2: "futa",
3: "mi",
4: "yon",
5: "itsu",
6: "mu",
7: "nana",
8: "ya",
9: "kokono",
10: "to",
}
toKunyomi2Map := map[int]string{
0: "zero",
1: "hito",
2: "futa",
3: "mi",
4: "yo",
5: "itsu",
6: "mu",
7: "nano",
8: "yo",
9: "kokono",
10: "so",
}
toOnyomiMap := map[int]string{
0: "rei",
1: "ichi",
2: "ni",
3: "san",
4: "shi",
5: "go",
6: "roku",
7: "shichi",
8: "hachi",
9: "kyu",
10: "ju",
}
toOnyomi2Map := map[int]string{
0: "rei",
1: "itsu",
2: "ji",
3: "zo",
4: "shi",
5: "go",
6: "riku",
7: "shichi",
8: "hachi",
9: "ku",
10: "ju",
}
toKanjiMap := map[int]string{
0: "零",
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
7: "七",
8: "八",
9: "九",
10: "十",
}
spoke := fetchXSpoke(
"japanese",
map[string]int{
"rei": 0,
"ichi": 1,
"ni": 2,
"ji": 2,
"san": 3,
"zo": 3,
"shi": 4,
"go": 5,
"roku": 6,
"riku": 6,
"shichi": 7,
"hachi": 8,
"kyu": 9,
"ku": 9,
"ju": 10,
"zero": 0,
"hito": 1,
"futa": 2,
"mi": 3,
"yon": 4,
"itsu": 5,
"mu": 6,
"nana": 7,
"nano": 7,
"ya": 8,
"yo": 8,
"kokono": 9,
"to": 10,
"so": 10,
"零": 0,
"一": 1,
"二": 2,
"三": 3,
"四": 4,
"五": 5,
"六": 6,
"七": 7,
"八": 8,
"九": 9,
"十": 10,
},
toCommonMap,
)
// customize the decoder function to honor user-defined options, when received...
spoke.Dec = func(v myLanguageHubDataType, opts0 ...Opts) (r myLanguageContentType, e error) {
if l := len(v); l > 0 {
if len(opts0) == 0 {
// no user-defined options were specified ...
return testDecodeUsingMap(v, toCommonMap)
}
// check for a recognized user-defined option
// (in this case, the decoder option, "use")
if decoderOpts := opts0[0].Dec; len(decoderOpts) > 0 {
if useOpt, exists := decoderOpts["use"]; exists {
switch useVal := useOpt.(type) {
case string:
switch useVal {
case "kunyomi":
return testDecodeUsingMap(v, toKunyomiMap) // use: "kunyomi"
case "kunyomi2":
return testDecodeUsingMap(v, toKunyomi2Map) // use: "kunyomi2"
case "onyomi":
return testDecodeUsingMap(v, toOnyomiMap) // use: "onnyomi"
case "onyomi2":
return testDecodeUsingMap(v, toOnyomi2Map) // use: "onnyomi2"
case "kanji":
return testDecodeUsingMap(v, toKanjiMap) // use: "kanji"
}
}
}
}
// we didn't recognize the options that were specified
// so use the default map, for decoding...
return testDecodeUsingMap(v, toCommonMap)
}
return
}
return spoke
}
// generate the codec for Klingon numbers
func fetchKlingonCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
fromKlingonMap := map[string]int{
"pagh": 0,
"wa’": 1,
"cha’": 2,
"wej": 3,
"loS": 4,
"vagh": 5,
"jav": 6,
"Soch": 7,
"chorgh": 8,
"Hut": 9,
"wa’maH": 10,
}
spoke := fetchXSpoke(
"klingon",
fromKlingonMap,
map[int]string{
0: "pagh",
1: "wa’",
2: "cha’",
3: "wej",
4: "loS",
5: "vagh",
6: "jav",
7: "Soch",
8: "chorgh",
9: "Hut",
10: "wa’maH",
},
)
testContentWithKlingonEncMap := func(v myLanguageContentType, f ...func(word string, wordOK bool)) bool {
act := func(_ string, _ bool) {}
if len(f) > 0 {
act = f[0]
}
for _, word := range v.toWords() {
word = testFixQuoteChars(strings.TrimSpace(word),"’","'","`")
// word = strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(word), "'", "’"), "`", "’")
if _, exists := fromKlingonMap[word]; !exists {
act(word, false)
return false
}
act(word, true)
}
return true
}
//use a custom encoder and a evaluater that handle case sensitivity differently than
// the default encoder, provided by the fetchXSpoke(...) function
spoke.Enc = func(v myLanguageContentType, opts0 ...Opts) (r myLanguageHubDataType, e error) {
if len(v) == 0 {
return
}
// create the hub data, from the content, using the encoder map
testContentWithKlingonEncMap(v, func(word string, wordOK bool) {
switch wordOK {
case true:
r = append(r, fromKlingonMap[word])
default:
e = fmt.Errorf("unknown word: '%s'", word)
}
})
return
}
spoke.Check = func(v myLanguageContentType) bool {
return testContentWithKlingonEncMap(v)
}
return spoke
}
// generate the codec for Ga numbers
func fetchGaCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
return fetchXSpoke(
"ga",
map[string]int{
"ekobɛ": 0,
"ekome": 1,
"enyɔ": 2,
"etɛ": 3,
"ejwɛ": 4,
"enumɔ": 5,
"ekpaa": 6,
"kpawo": 7,
"kpaanyɔ": 8,
"nɛɛhu": 9,
"nyɔŋma": 10,
},
map[int]string{
0: "ekobɛ",
1: "ekome",
2: "enyɔ",
3: "etɛ",
4: "ejwɛ",
5: "enumɔ",
6: "ekpaa",
7: "kpawo",
8: "kpaanyɔ",
9: "nɛɛhu",
10: "nyɔŋma",
},
)
}
// generate the codec for Ga numbers
func fetchHawaiianCodec() (r Codec[myLanguageContentType, myLanguageHubDataType]) {
fromHawaiianMap := map[string]int{
"῾ole": 0,
"῾ekahi": 1,
"akahi": 1,
"῾elua": 2,
"῾ekolu": 3,
"῾ehā": 4,
"῾elima": 5,
"῾alima": 5,
"῾eono": 6,
"῾ehiku": 7,
"῾ewalu": 8,
"῾awalu": 8,
"῾eiwa": 9,
"iwa": 9,
"῾aiwa": 9,
"῾umi": 10,
}
spoke := fetchXSpoke(
"hawaiian",
fromHawaiianMap,
map[int]string{
0: "῾ole",
1: "῾ekahi",
2: "῾elua",
3: "῾ekolu",
4: "῾ehā",
5: "῾elima",
6: "῾eono",
7: "῾ehiku",
8: "῾ewalu",
9: "῾eiwa",
10: "῾umi",
},
)
testContentWithHawaiianEncMap := func(v myLanguageContentType, f ...func(word string, wordOK bool)) bool {
act := func(_ string, _ bool) {}
if len(f) > 0 {
act = f[0]
}
for _, word := range v.toWords() {
word = strings.ToLower(testFixQuoteChars(strings.TrimSpace(word),"῾","'","’","`"))
if _, exists := fromHawaiianMap[word]; !exists {
act(word, false)
return false
}
act(word, true)
}
return true
}
//use a custom encoder and a evaluater that handle case sensitivity differently than
// the default encoder, provided by the fetchXSpoke(...) function
spoke.Enc = func(v myLanguageContentType, opts0 ...Opts) (r myLanguageHubDataType, e error) {
if len(v) == 0 {
return
}
// create the hub data, from the content, using the encoder map
testContentWithHawaiianEncMap(v, func(word string, wordOK bool) {
switch wordOK {
case true:
r = append(r, fromHawaiianMap[word])
default:
e = fmt.Errorf("unknown word: '%s'", word)
}
})
return
}
spoke.Check = func(v myLanguageContentType) bool {
return testContentWithHawaiianEncMap(v)
}
return spoke
}