-
Notifications
You must be signed in to change notification settings - Fork 66
/
script.go
207 lines (174 loc) · 4.77 KB
/
script.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
package whatlanggo
import "unicode"
type scriptCounter struct {
checkFunc func(r rune) bool
script *unicode.RangeTable
count int
}
// Scripts is the set of Unicode script tables.
var Scripts = map[*unicode.RangeTable]string{
unicode.Arabic: "Arabic",
unicode.Bengali: "Bengali",
unicode.Cyrillic: "Cyrillic",
unicode.Ethiopic: "Ethiopic",
unicode.Devanagari: "Devanagari",
unicode.Han: "Han",
unicode.Georgian: "Georgian",
unicode.Greek: "Greek",
unicode.Gujarati: "Gujarati",
unicode.Gurmukhi: "Gurmukhi",
unicode.Hangul: "Hangul",
unicode.Hebrew: "Hebrew",
unicode.Hiragana: "Hiragana",
unicode.Kannada: "Kannada",
unicode.Katakana: "Katakana",
unicode.Khmer: "Khmer",
unicode.Latin: "Latin",
unicode.Malayalam: "Malayalam",
unicode.Myanmar: "Myanmar",
unicode.Oriya: "Oriya",
unicode.Sinhala: "Sinhala",
unicode.Tamil: "Tamil",
unicode.Telugu: "Telugu",
unicode.Thai: "Thai",
}
// DetectScript returns only the script of the given text.
func DetectScript(text string) *unicode.RangeTable {
halfLen := len(text) / 2
scriptCounter := []scriptCounter{
{isLatin, unicode.Latin, 0},
{isCyrillic, unicode.Cyrillic, 0},
{isArabic, unicode.Arabic, 0},
{isDevanagari, unicode.Devanagari, 0},
{isHiraganaKatakana, _HiraganaKatakana, 0},
{isEthiopic, unicode.Ethiopic, 0},
{isHebrew, unicode.Hebrew, 0},
{isBengali, unicode.Bengali, 0},
{isGeorgian, unicode.Georgian, 0},
{isHan, unicode.Han, 0},
{isHangul, unicode.Hangul, 0},
{isGreek, unicode.Greek, 0},
{isKannada, unicode.Kannada, 0},
{isTamil, unicode.Tamil, 0},
{isThai, unicode.Thai, 0},
{isGujarati, unicode.Gujarati, 0},
{isGurmukhi, unicode.Gurmukhi, 0},
{isTelugu, unicode.Telugu, 0},
{isMalayalam, unicode.Malayalam, 0},
{isOriya, unicode.Oriya, 0},
{isMyanmar, unicode.Myanmar, 0},
{isSinhala, unicode.Sinhala, 0},
{isKhmer, unicode.Khmer, 0},
}
for _, ch := range text {
if isStopChar(ch) {
continue
}
for i, sc := range scriptCounter {
if sc.checkFunc(ch) {
scriptCounter[i].count++
if scriptCounter[i].count > halfLen {
return sc.script
}
//if script is found, move it closer to the front so that it be checked first.
if i > 0 {
scriptCounter[i], scriptCounter[i-1] = scriptCounter[i-1], scriptCounter[i]
}
}
}
}
//find the script that occurs the most in the text and return it.
jpCount := 0
max := 0
maxScript := &unicode.RangeTable{}
for _, script := range scriptCounter {
if script.count > max {
max = script.count
maxScript = script.script
if script.script == _HiraganaKatakana {
jpCount = max
}
}
}
switch {
case max == 0:
//if no valid script is detected, return nil.
return nil
case max != 0 && (maxScript == unicode.Han && jpCount > 0):
// If Hiragana or Katakana is included, even if judged as Mandarin,
// it is regarded as Japanese. Japanese uses Kanji (unicode.Han)
// in addition to Hiragana and Katakana.
return _HiraganaKatakana
default:
return maxScript
}
}
var isCyrillic = func(r rune) bool {
return unicode.Is(unicode.Cyrillic, r)
}
var isLatin = func(r rune) bool {
return unicode.Is(unicode.Latin, r)
}
var isArabic = func(r rune) bool {
return unicode.Is(unicode.Arabic, r)
}
var isDevanagari = func(r rune) bool {
return unicode.Is(unicode.Devanagari, r)
}
var isEthiopic = func(r rune) bool {
return unicode.Is(unicode.Ethiopic, r)
}
var isHebrew = func(r rune) bool {
return unicode.Is(unicode.Hebrew, r)
}
var isHan = func(r rune) bool {
return unicode.Is(unicode.Han, r)
}
var isBengali = func(r rune) bool {
return unicode.Is(unicode.Bengali, r)
}
var isHiraganaKatakana = func(r rune) bool {
return unicode.Is(_HiraganaKatakana, r)
}
var isHangul = func(r rune) bool {
return unicode.Is(unicode.Hangul, r)
}
var isGreek = func(r rune) bool {
return unicode.Is(unicode.Greek, r)
}
var isKannada = func(r rune) bool {
return unicode.Is(unicode.Kannada, r)
}
var isTamil = func(r rune) bool {
return unicode.Is(unicode.Tamil, r)
}
var isThai = func(r rune) bool {
return unicode.Is(unicode.Thai, r)
}
var isGujarati = func(r rune) bool {
return unicode.Is(unicode.Gujarati, r)
}
var isGurmukhi = func(r rune) bool {
return unicode.Is(unicode.Gurmukhi, r)
}
var isTelugu = func(r rune) bool {
return unicode.Is(unicode.Telugu, r)
}
var isMalayalam = func(r rune) bool {
return unicode.Is(unicode.Malayalam, r)
}
var isOriya = func(r rune) bool {
return unicode.Is(unicode.Oriya, r)
}
var isMyanmar = func(r rune) bool {
return unicode.Is(unicode.Myanmar, r)
}
var isSinhala = func(r rune) bool {
return unicode.Is(unicode.Sinhala, r)
}
var isKhmer = func(r rune) bool {
return unicode.Is(unicode.Khmer, r)
}
var isGeorgian = func(r rune) bool {
return unicode.Is(unicode.Georgian, r)
}