forked from pemistahl/lingua-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabet.go
143 lines (133 loc) · 3.5 KB
/
alphabet.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
/*
* Copyright © 2021-present 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 (
"fmt"
"regexp"
)
type alphabet int
const (
arabic alphabet = iota
armenian
bengali
cyrillic
devanagari
georgian
greek
gujarati
gurmukhi
han
hangul
hebrew
hiragana
katakana
latin
tamil
telugu
thai
)
func (alphabet alphabet) matches(text string) bool {
switch alphabet {
case arabic:
return arabicChars.MatchString(text)
case armenian:
return armenianChars.MatchString(text)
case bengali:
return bengaliChars.MatchString(text)
case cyrillic:
return cyrillicChars.MatchString(text)
case devanagari:
return devanagariChars.MatchString(text)
case georgian:
return georgianChars.MatchString(text)
case greek:
return greekChars.MatchString(text)
case gujarati:
return gujaratiChars.MatchString(text)
case gurmukhi:
return gurmukhiChars.MatchString(text)
case han:
return hanChars.MatchString(text)
case hangul:
return hangulChars.MatchString(text)
case hebrew:
return hebrewChars.MatchString(text)
case hiragana:
return hiraganaChars.MatchString(text)
case katakana:
return katakanaChars.MatchString(text)
case latin:
return latinChars.MatchString(text)
case tamil:
return tamilChars.MatchString(text)
case telugu:
return teluguChars.MatchString(text)
case thai:
return thaiChars.MatchString(text)
default:
return false
}
}
func (alphabet alphabet) supportedLanguages() (languages []Language) {
for _, language := range AllLanguages() {
for _, script := range language.alphabets() {
if script == alphabet {
languages = append(languages, language)
}
}
}
return
}
func allAlphabetsSupportingSingleLanguage() map[alphabet]Language {
alphabets := make(map[alphabet]Language)
for _, alphabet := range allAlphabets() {
supportedLanguages := alphabet.supportedLanguages()
if len(supportedLanguages) == 1 {
alphabets[alphabet] = supportedLanguages[0]
}
}
return alphabets
}
func allAlphabets() []alphabet {
alphabets := make([]alphabet, thai+1)
for i := 0; i <= int(thai); i++ {
alphabets[i] = alphabet(i)
}
return alphabets
}
var (
arabicChars = createRegexp("Arabic")
armenianChars = createRegexp("Armenian")
bengaliChars = createRegexp("Bengali")
cyrillicChars = createRegexp("Cyrillic")
devanagariChars = createRegexp("Devanagari")
georgianChars = createRegexp("Georgian")
greekChars = createRegexp("Greek")
gujaratiChars = createRegexp("Gujarati")
gurmukhiChars = createRegexp("Gurmukhi")
hanChars = createRegexp("Han")
hangulChars = createRegexp("Hangul")
hebrewChars = createRegexp("Hebrew")
hiraganaChars = createRegexp("Hiragana")
katakanaChars = createRegexp("Katakana")
latinChars = createRegexp("Latin")
tamilChars = createRegexp("Tamil")
teluguChars = createRegexp("Telugu")
thaiChars = createRegexp("Thai")
)
func createRegexp(charClass string) *regexp.Regexp {
return regexp.MustCompile(fmt.Sprintf("^\\p{%v}+$", charClass))
}