-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.go
82 lines (75 loc) · 1.61 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
package langdetect
import "unicode"
func detectScript(text string) *unicode.RangeTable {
halfLen := len(text) / 2
var scriptCount = make(map[*unicode.RangeTable]int)
for _, r := range text {
for i, script := range DefaultScripts {
if unicode.Is(script, r) {
cnt := scriptCount[script]
if cnt+1 > halfLen {
return script
}
scriptCount[script] = cnt + 1
if i > 0 {
DefaultScripts[i], DefaultScripts[i-1] = DefaultScripts[i-1], DefaultScripts[i]
}
break
}
}
}
maxCount := 0
jpCount := 0
var maxScript *unicode.RangeTable
for script, count := range scriptCount {
if count > maxCount {
maxCount = count
maxScript = script
if script == _HiraganaKatakana {
jpCount = count
}
}
}
switch {
case maxCount == 0:
return nil
case maxCount != 0 && (maxScript == unicode.Han && jpCount > 0):
return _HiraganaKatakana
default:
return maxScript
}
}
var Scripts = unicode.Scripts
var ScriptNames = map[*unicode.RangeTable]string{}
var DefaultScripts = []*unicode.RangeTable{
unicode.Latin,
unicode.Cyrillic,
unicode.Arabic,
unicode.Devanagari,
_HiraganaKatakana,
unicode.Ethiopic,
unicode.Hebrew,
unicode.Bengali,
unicode.Georgian,
unicode.Han,
unicode.Hangul,
unicode.Greek,
unicode.Kannada,
unicode.Tamil,
unicode.Thai,
unicode.Gujarati,
unicode.Gurmukhi,
unicode.Telugu,
unicode.Malayalam,
unicode.Oriya,
unicode.Myanmar,
unicode.Sinhala,
unicode.Khmer,
}
func init() {
Scripts["HiraganaKatakana"] = _HiraganaKatakana
ScriptNames = make(map[*unicode.RangeTable]string)
for name, rangetable := range Scripts {
ScriptNames[rangetable] = name
}
}