-
Notifications
You must be signed in to change notification settings - Fork 0
/
All_to_kana.go
51 lines (45 loc) · 1.4 KB
/
All_to_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
// Package kanatrans converts English phrases into phonetic Japanese kana approximations; also known as Englishru
package kanatrans
// AllToKana struct holds the necessary functions for All to Katakana conversion
type AllToKana struct {
e2k *EngToKana
k2k *KanjiToKana
h2k *HiraganaToKana
ks *KanjiSplitter
}
// NewAllToKana creates a new instance of AllToKana
func NewAllToKana(strictPunct ...bool) *AllToKana {
// Set optional strict bool
var strict bool
if len(strictPunct) > 0 {
strict = strictPunct[0]
}
// Instantiate class
a2k := AllToKana{}
// Create an instance of EngToKana
a2k.e2k = NewEngToKana(true)
// Create an instance of KanjiToKana
a2k.k2k = NewKanjiToKana()
// Create an instance of GanaToKana
a2k.h2k = NewHiraganaToKana()
// Determine punctuation converter to use
var puncFP func(string) string
if strict {
puncFP = ConvertToJapanesePunctuationRestricted
} else {
puncFP = ConvertToJapanesePunctuation
}
// Create an instance of KanjiSplitter
a2k.ks = NewKanjiSplitter(
a2k.k2k.Convert, // Kanji callback
a2k.h2k.Convert, // Gana & Kana callback
a2k.e2k.TranscriptSentence, // English callback
puncFP, // Punctuation callback
)
// Return instance
return &a2k
}
// Convert converts English, Romaji, Hiragana, Kanji to Katakana, leaves Katakana unchanged.
func (a2k *AllToKana) Convert(s string) string {
return a2k.ks.SeparateAndProcess(s)
}