forked from pemistahl/lingua-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
685 lines (664 loc) · 11.6 KB
/
language.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* Copyright © 2021 Peter M. Stahl [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lingua
import (
"encoding/json"
"fmt"
"strings"
)
//go:generate stringer -type=Language
// Language is the type used for enumerating the so far 75 languages which can
// be detected by Lingua.
type Language int
const (
Afrikaans Language = iota
Albanian
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bengali
Bokmal
Bosnian
Bulgarian
Catalan
Chinese
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Finnish
French
Ganda
Georgian
German
Greek
Gujarati
Hebrew
Hindi
Hungarian
Icelandic
Indonesian
Irish
Italian
Japanese
Kazakh
Korean
Latin
Latvian
Lithuanian
Macedonian
Malay
Maori
Marathi
Mongolian
Nynorsk
Persian
Polish
Portuguese
Punjabi
Romanian
Russian
Serbian
Shona
Slovak
Slovene
Somali
Sotho
Spanish
Swahili
Swedish
Tagalog
Tamil
Telugu
Thai
Tsonga
Tswana
Turkish
Ukrainian
Urdu
Vietnamese
Welsh
Xhosa
Yoruba
Zulu
Unknown
)
// AllLanguages returns a sorted slice of all currently supported languages.
func AllLanguages() []Language {
languages := make([]Language, amountOfSupportedLanguages())
for i := 0; i < amountOfSupportedLanguages(); i++ {
languages[i] = Language(i)
}
return languages
}
// AllSpokenLanguages returns a sorted slice of all supported languages
// that are not extinct but still spoken.
func AllSpokenLanguages() (languages []Language) {
for _, language := range AllLanguages() {
if language != Latin {
languages = append(languages, language)
}
}
return
}
// AllLanguagesWithArabicScript returns a sorted slice of all built-in
// languages supporting the Arabic script.
func AllLanguagesWithArabicScript() []Language {
return allLanguagesWithScript(arabic)
}
// AllLanguagesWithCyrillicScript returns a sorted slice of all built-in
// languages supporting the Cyrillic script.
func AllLanguagesWithCyrillicScript() []Language {
return allLanguagesWithScript(cyrillic)
}
// AllLanguagesWithDevanagariScript returns a sorted slice of all built-in
// languages supporting the Devanagari script.
func AllLanguagesWithDevanagariScript() []Language {
return allLanguagesWithScript(devanagari)
}
// AllLanguagesWithLatinScript returns a sorted slice of all built-in
// languages supporting the Latin script.
func AllLanguagesWithLatinScript() []Language {
return allLanguagesWithScript(latin)
}
// GetLanguageFromIsoCode639_1 returns a sorted slice of all built-in
// languages whose ISO 639-1 codes are specified as arguments passed to
// this method.
func GetLanguageFromIsoCode639_1(isoCode IsoCode639_1) Language {
for _, language := range AllLanguages() {
if language.IsoCode639_1() == isoCode {
return language
}
}
return -1
}
// GetLanguageFromIsoCode639_3 returns a sorted slice of all built-in
// languages whose ISO 639-3 codes are specified as arguments passed to
// this method.
func GetLanguageFromIsoCode639_3(isoCode IsoCode639_3) Language {
for _, language := range AllLanguages() {
if language.IsoCode639_3() == isoCode {
return language
}
}
return -1
}
func allLanguagesWithScript(script alphabet) (languages []Language) {
for _, language := range AllLanguages() {
if language.alphabets()[0] == script {
languages = append(languages, language)
}
}
return
}
func amountOfSupportedLanguages() int {
return int(Zulu + 1)
}
// IsoCode639_1 returns a language's ISO 639-1 code.
func (language Language) IsoCode639_1() IsoCode639_1 {
switch language {
case Afrikaans:
return AF
case Albanian:
return SQ
case Arabic:
return AR
case Armenian:
return HY
case Azerbaijani:
return AZ
case Basque:
return EU
case Belarusian:
return BE
case Bengali:
return BN
case Bokmal:
return NB
case Bosnian:
return BS
case Bulgarian:
return BG
case Catalan:
return CA
case Chinese:
return ZH
case Croatian:
return HR
case Czech:
return CS
case Danish:
return DA
case Dutch:
return NL
case English:
return EN
case Esperanto:
return EO
case Estonian:
return ET
case Finnish:
return FI
case French:
return FR
case Ganda:
return LG
case Georgian:
return KA
case German:
return DE
case Greek:
return EL
case Gujarati:
return GU
case Hebrew:
return HE
case Hindi:
return HI
case Hungarian:
return HU
case Icelandic:
return IS
case Indonesian:
return ID
case Irish:
return GA
case Italian:
return IT
case Japanese:
return JA
case Kazakh:
return KK
case Korean:
return KO
case Latin:
return LA
case Latvian:
return LV
case Lithuanian:
return LT
case Macedonian:
return MK
case Malay:
return MS
case Maori:
return MI
case Marathi:
return MR
case Mongolian:
return MN
case Nynorsk:
return NN
case Persian:
return FA
case Polish:
return PL
case Portuguese:
return PT
case Punjabi:
return PA
case Romanian:
return RO
case Russian:
return RU
case Serbian:
return SR
case Shona:
return SN
case Slovak:
return SK
case Slovene:
return SL
case Somali:
return SO
case Sotho:
return ST
case Spanish:
return ES
case Swahili:
return SW
case Swedish:
return SV
case Tagalog:
return TL
case Tamil:
return TA
case Telugu:
return TE
case Thai:
return TH
case Tsonga:
return TS
case Tswana:
return TN
case Turkish:
return TR
case Ukrainian:
return UK
case Urdu:
return UR
case Vietnamese:
return VI
case Welsh:
return CY
case Xhosa:
return XH
case Yoruba:
return YO
case Zulu:
return ZU
case Unknown:
return UnknownIsoCode639_1
default:
return -1
}
}
// IsoCode639_3 returns a language's ISO 639-3 code.
func (language Language) IsoCode639_3() IsoCode639_3 {
switch language {
case Afrikaans:
return AFR
case Albanian:
return SQI
case Arabic:
return ARA
case Armenian:
return HYE
case Azerbaijani:
return AZE
case Basque:
return EUS
case Belarusian:
return BEL
case Bengali:
return BEN
case Bokmal:
return NOB
case Bosnian:
return BOS
case Bulgarian:
return BUL
case Catalan:
return CAT
case Chinese:
return ZHO
case Croatian:
return HRV
case Czech:
return CES
case Danish:
return DAN
case Dutch:
return NLD
case English:
return ENG
case Esperanto:
return EPO
case Estonian:
return EST
case Finnish:
return FIN
case French:
return FRA
case Ganda:
return LUG
case Georgian:
return KAT
case German:
return DEU
case Greek:
return ELL
case Gujarati:
return GUJ
case Hebrew:
return HEB
case Hindi:
return HIN
case Hungarian:
return HUN
case Icelandic:
return ISL
case Indonesian:
return IND
case Irish:
return GLE
case Italian:
return ITA
case Japanese:
return JPN
case Kazakh:
return KAZ
case Korean:
return KOR
case Latin:
return LAT
case Latvian:
return LAV
case Lithuanian:
return LIT
case Macedonian:
return MKD
case Malay:
return MSA
case Maori:
return MRI
case Marathi:
return MAR
case Mongolian:
return MON
case Nynorsk:
return NNO
case Persian:
return FAS
case Polish:
return POL
case Portuguese:
return POR
case Punjabi:
return PAN
case Romanian:
return RON
case Russian:
return RUS
case Serbian:
return SRP
case Shona:
return SNA
case Slovak:
return SLK
case Slovene:
return SLV
case Somali:
return SOM
case Sotho:
return SOT
case Spanish:
return SPA
case Swahili:
return SWA
case Swedish:
return SWE
case Tagalog:
return TGL
case Tamil:
return TAM
case Telugu:
return TEL
case Thai:
return THA
case Tsonga:
return TSO
case Tswana:
return TSN
case Turkish:
return TUR
case Ukrainian:
return UKR
case Urdu:
return URD
case Vietnamese:
return VIE
case Welsh:
return CYM
case Xhosa:
return XHO
case Yoruba:
return YOR
case Zulu:
return ZUL
case Unknown:
return UnknownIsoCode639_3
default:
return -1
}
}
func (language Language) alphabets() []alphabet {
switch language {
case
Afrikaans,
Albanian,
Azerbaijani,
Basque,
Bokmal,
Bosnian,
Catalan,
Croatian,
Czech,
Danish,
Dutch,
English,
Esperanto,
Estonian,
Finnish,
French,
Ganda,
German,
Hungarian,
Icelandic,
Indonesian,
Irish,
Italian,
Latin,
Latvian,
Lithuanian,
Malay,
Maori,
Nynorsk,
Polish,
Portuguese,
Romanian,
Shona,
Slovak,
Slovene,
Somali,
Sotho,
Spanish,
Swahili,
Swedish,
Tagalog,
Tsonga,
Tswana,
Turkish,
Vietnamese,
Welsh,
Xhosa,
Yoruba,
Zulu:
return []alphabet{latin}
case
Belarusian,
Bulgarian,
Kazakh,
Macedonian,
Mongolian,
Russian,
Serbian,
Ukrainian:
return []alphabet{cyrillic}
case Arabic, Persian, Urdu:
return []alphabet{arabic}
case Hindi, Marathi:
return []alphabet{devanagari}
case Armenian:
return []alphabet{armenian}
case Bengali:
return []alphabet{bengali}
case Chinese:
return []alphabet{han}
case Georgian:
return []alphabet{georgian}
case Greek:
return []alphabet{greek}
case Gujarati:
return []alphabet{gujarati}
case Hebrew:
return []alphabet{hebrew}
case Japanese:
return []alphabet{hiragana, katakana, han}
case Korean:
return []alphabet{hangul}
case Punjabi:
return []alphabet{gurmukhi}
case Tamil:
return []alphabet{tamil}
case Telugu:
return []alphabet{telugu}
case Thai:
return []alphabet{thai}
default:
return []alphabet{}
}
}
func (language Language) uniqueCharacters() string {
switch language {
case Azerbaijani:
return "Əə"
case Catalan:
return "Ïï"
case Czech:
return "ĚěŘřŮů"
case Esperanto:
return "ĈĉĜĝĤĥĴĵŜŝŬŭ"
case German:
return "ß"
case Hungarian:
return "ŐőŰű"
case Kazakh:
return "ӘәҒғҚқҢңҰұ"
case Latvian:
return "ĢģĶķĻļŅņ"
case Lithuanian:
return "ĖėĮįŲų"
case Macedonian:
return "ЃѓЅѕЌќЏџ"
case Marathi:
return "ळ"
case Mongolian:
return "ӨөҮү"
case Polish:
return "ŁłŃńŚśŹź"
case Romanian:
return "Țţ"
case Serbian:
return "ЂђЋћ"
case Slovak:
return "Ĺ弾Ŕŕ"
case Spanish:
return "¿¡"
case Ukrainian:
return "ҐґЄєЇї"
case Vietnamese:
return "ẰằẦầẲẳẨẩẴẵẪẫẮắẤấẠạẶặẬậỀềẺẻỂểẼẽỄễẾếỆệỈỉĨĩỊịƠơỒồỜờỎỏỔổỞởỖỗỠỡỐốỚớỘộỢợƯưỪừỦủỬửŨũỮữỨứỤụỰựỲỳỶỷỸỹỴỵ"
case Yoruba:
return "ŌōṢṣ"
default:
return ""
}
}
// MarshalJSON returns a language's JSON representation
// which is its full name written in uppercase.
func (language Language) MarshalJSON() ([]byte, error) {
return json.Marshal(strings.ToUpper(language.String()))
}
// UnmarshalJSON converts a language's JSON representation
// back to its instance of type Language.
func (language *Language) UnmarshalJSON(bytes []byte) error {
var s string
if err := json.Unmarshal(bytes, &s); err != nil {
return err
}
for _, l := range AllLanguages() {
str := strings.ToUpper(l.String())
if str == s {
*language = l
return nil
}
}
return fmt.Errorf("string \"%v\" cannot be unmarshalled to an instance of type Language", s)
}
func containsLanguage(languages []Language, language Language) bool {
for _, l := range languages {
if l == language {
return true
}
}
return false
}