forked from gojp/kana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kana.go
185 lines (162 loc) · 5.41 KB
/
kana.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
package kana
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
var (
consonants = []string{"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "p", "r", "s", "t", "w", "z"}
hiraganaRe = regexp.MustCompile(`ん([あいうえおなにぬねの])`)
katakanaRe = regexp.MustCompile(`ン([アイウエオナニヌネノ])`)
kanaToRomajiTrie *Trie
romajiToHiraganaTrie *Trie
romajiToKatakanaTrie *Trie
)
// Initialize builds the Hiragana + Katakana trie.
// Because there is no overlap between the hiragana and katakana sets,
// they both use the same trie without conflict. Nice bonus!
func Initialize() {
kanaToRomajiTrie = newTrie()
romajiToHiraganaTrie = newTrie()
romajiToKatakanaTrie = newTrie()
tables := []string{HiraganaTable, KatakanaTable}
for t, table := range tables {
rows := strings.Split(table, "\n")
colNames := strings.Split(string(rows[0]), "\t")[1:]
for _, row := range rows[1:] {
cols := strings.Split(string(row), "\t")
rowName := cols[0]
for i, kana := range cols[1:] {
value := rowName + colNames[i]
kanas := strings.Split(kana, "/")
for _, singleKana := range kanas {
if singleKana != "" {
// add to tries
kanaToRomajiTrie.insert(singleKana, value)
if t == 0 {
romajiToHiraganaTrie.insert(value, singleKana)
} else if t == 1 {
romajiToKatakanaTrie.insert(value, singleKana)
}
}
}
}
}
}
}
// KanaToRomaji converts a kana string to its romaji form
func KanaToRomaji(kana string) (romaji string) {
// unfortunate hack to deal with double n's
romaji = hiraganaRe.ReplaceAllString(kana, "nn$1")
romaji = katakanaRe.ReplaceAllString(romaji, "nn$1")
romaji = kanaToRomajiTrie.convert(romaji)
// do some post-processing for the tsu and stripe characters
// maybe a bit of a hacky solution - how can we improve?
// (they act more like punctuation)
tsus := []string{"っ", "ッ"}
for _, tsu := range tsus {
if strings.Index(romaji, tsu) > -1 {
for _, c := range romaji {
ch := string(c)
if ch == tsu {
i := strings.Index(romaji, ch)
runeSize := len(ch)
followingLetter, _ := utf8.DecodeRuneInString(romaji[i+runeSize:])
followingLetterStr := string(followingLetter)
if followingLetterStr != tsu {
romaji = strings.Replace(romaji, tsu, followingLetterStr, 1)
} else {
romaji = strings.Replace(romaji, tsu, "", 1)
}
}
}
}
}
line := "ー"
for i := strings.Index(romaji, line); i > -1; i = strings.Index(romaji, line) {
if i > 0 {
romaji = strings.Replace(romaji, line, "-", 1)
} else {
romaji = strings.Replace(romaji, line, "", 1)
}
}
return romaji
}
func replaceTsus(romaji string, tsu string) (result string) {
result = romaji
for _, consonant := range consonants {
result = strings.Replace(result, consonant+consonant, tsu+consonant, -1)
}
return result
}
func replaceNs(romaji string, n string) (result string) {
return strings.Replace(romaji, "nn", n, -1)
}
// RomajiToHiragana converts a romaji string to its hiragana form
func RomajiToHiragana(romaji string) (hiragana string) {
romaji = strings.Replace(romaji, "-", "ー", -1)
romaji = replaceTsus(romaji, "っ")
romaji = replaceNs(romaji, "ん")
hiragana = romajiToHiraganaTrie.convert(romaji)
return hiragana
}
// RomajiToKatakana converts a romaji string to its katakana form
func RomajiToKatakana(romaji string) (katakana string) {
romaji = strings.Replace(romaji, "-", "ー", -1)
// convert double consonants to little tsus first
romaji = replaceTsus(romaji, "ッ")
romaji = replaceNs(romaji, "ン")
katakana = romajiToKatakanaTrie.convert(romaji)
return katakana
}
func isChar(s string, rangeTable []*unicode.RangeTable) bool {
runeForm := []rune(s)
for _, r := range runeForm {
if !unicode.IsOneOf(rangeTable, r) {
return false
}
}
return true
}
// IsLatin returns true if the string contains only Latin characters
func IsLatin(s string) bool {
return isChar(s, []*unicode.RangeTable{unicode.Latin, unicode.ASCII_Hex_Digit, unicode.White_Space, unicode.Hyphen})
}
// IsKana returns true if the string contains only kana
func IsKana(s string) bool {
return isChar(s, []*unicode.RangeTable{unicode.Hiragana, unicode.Katakana, unicode.Hyphen, unicode.Diacritic})
}
// IsHiragana returns true if the string contains only hiragana
func IsHiragana(s string) bool {
return isChar(s, []*unicode.RangeTable{unicode.Hiragana, unicode.Hyphen, unicode.Diacritic})
}
// IsKatakana returns true if the string contains only katakana
func IsKatakana(s string) bool {
return isChar(s, []*unicode.RangeTable{unicode.Katakana, unicode.Hyphen, unicode.Diacritic})
}
// IsKanji return strue if the string contains only kanji
func IsKanji(s string) bool {
return isChar(s, []*unicode.RangeTable{unicode.Ideographic})
}
func replaceAll(haystack string, needles []string, replacements []string) (replaced string) {
replaced = haystack
for i := range needles {
replaced = strings.Replace(replaced, needles[i], replacements[i], -1)
}
return replaced
}
// NormalizeRomaji transforms romaji input to one specific standard form,
// which should be as close as possible to hiragana so that
// this library gives correct output when transforming to
// hiragana/katakana
func NormalizeRomaji(s string) (romaji string) {
romaji = s
romaji = strings.ToLower(romaji)
romaji = replaceAll(
romaji,
[]string{"ā", "ē", "ī", "ō", "ū", "ee", "uu"},
[]string{"a-", "ei", "ii", "oo", "u-", "ei", "u-"},
)
return romaji
}