-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
189 lines (166 loc) · 3.6 KB
/
utils.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
/*
* Bamboo - A Vietnamese Input method editor
* Copyright (C) Luong Thanh Lam <ltlam93@gmail.com>
*
* This software is licensed under the MIT license. For more information,
* see <https://github.com/BambooEngine/bamboo-core/blob/master/LICENSE>.
*/
package bamboo
import (
"unicode"
)
var Vowels = []rune("aàáảãạăằắẳẵặâầấẩẫậeèéẻẽẹêềếểễệiìíỉĩịoòóỏõọôồốổỗộơờớởỡợuùúủũụưừứửữựyỳýỷỹỵ")
var PunctuationMarks = []rune{
',', ';', ':', '.', '"', '\'', '!', '?', ' ',
'<', '>', '=', '+', '-', '*', '/', '\\',
'_', '~', '`', '@', '#', '$', '%', '^', '&', '(', ')', '{', '}', '[', ']',
'|',
}
func IsSpace(key rune) bool {
return key == ' '
}
func IsPunctuationMark(key rune) bool {
for _, c := range PunctuationMarks {
if c == key {
return true
}
}
return false
}
func IsWordBreakSymbol(key rune) bool {
return IsPunctuationMark(key) || ('0' <= key && '9' >= key)
}
func IsVowel(chr rune) bool {
isVowel := false
for _, v := range Vowels {
if v == chr {
isVowel = true
}
}
return isVowel
}
func FindVowelPosition(chr rune) int {
for pos, v := range Vowels {
if v == chr {
return pos
}
}
return -1
}
var marksMaps = map[rune]string{
'a': "aâă__",
'â': "aâă__",
'ă': "aâă__",
'e': "eê___",
'ê': "eê___",
'o': "oô_ơ_",
'ô': "oô_ơ_",
'ơ': "oô_ơ_",
'u': "u__ư_",
'ư': "u__ư_",
'd': "d___đ",
'đ': "d___đ",
}
func getMarkFamily(chr rune) []rune {
var result []rune
if s, found := marksMaps[chr]; found {
for _, c := range s {
if c != '_' {
result = append(result, c)
}
}
}
return result
}
func FindMarkPosition(chr rune) int {
if str, found := marksMaps[chr]; found {
for pos, v := range []rune(str) {
if v == chr {
return pos
}
}
}
return -1
}
func FindMarkFromChar(chr rune) (Mark, bool) {
var pos = FindMarkPosition(chr)
if pos >= 0 {
return Mark(pos), true
}
return 0, false
}
func AddMarkToTonelessChar(chr rune, mark uint8) rune {
if str, found := marksMaps[chr]; found {
marks := []rune(str)
if marks[mark] != '_' {
return marks[mark]
}
}
return chr
}
func AddMarkToChar(chr rune, mark uint8) rune {
tone := FindToneFromChar(chr)
chr = AddToneToChar(chr, 0)
chr = AddMarkToTonelessChar(chr, mark)
return AddToneToChar(chr, uint8(tone))
}
func IsAlpha(c rune) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func inKeyList(keys []rune, key rune) bool {
for _, k := range keys {
if k == key {
return true
}
}
return false
}
func FindToneFromChar(chr rune) Tone {
pos := FindVowelPosition(chr)
if pos == -1 {
return ToneNone
}
return Tone(pos % 6)
}
func AddToneToChar(chr rune, tone uint8) rune {
pos := FindVowelPosition(chr)
if pos > -1 {
currentTone := pos % 6
offset := int(tone) - currentTone
return Vowels[pos+offset]
} else {
return chr
}
}
func canProcessKey(lowerKey rune, effectKeys []rune) bool {
if IsAlpha(lowerKey) || inKeyList(effectKeys, lowerKey) {
return true
}
if IsWordBreakSymbol(lowerKey) {
return false
}
return IsVietnameseRune(lowerKey)
}
func IsVietnameseRune(lowerKey rune) bool {
// lowerKey = unicode.ToLower(lowerKey)
if FindToneFromChar(lowerKey) != ToneNone {
return true
}
return lowerKey != AddMarkToTonelessChar(lowerKey, 0)
}
func HasAnyVietnameseRune(word string) bool {
for _, chr := range word {
if IsVietnameseRune(unicode.ToLower(chr)) {
return true
}
}
return false
}
func HasAnyVietnameseVower(word string) bool {
for _, chr := range word {
if IsVowel(unicode.ToLower(chr)) {
return true
}
}
return false
}