-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorae_creator.go
57 lines (50 loc) · 1.35 KB
/
morae_creator.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
package kanatrans
import (
"unicode/utf8"
"fmt"
)
// moraeCreator creates morae from phonetics.
type moraeCreator struct {
vowels string
}
// newMoraeCreator creates a new instance of moraeCreator.
func newMoraeCreator() *moraeCreator {
return &moraeCreator{
vowels: "aeiou",
}
}
// CreateMorae creates morae from phonetics.
func (mc *moraeCreator) CreateMorae(ph string) string {
result := string(ph[0]) // Add the first character to the result
for pIdx, r := range ph[1:] {
// Increment pIdx by 1 to match the index of the current rune
pIdx++
// Get previous character
runeAtIndex, size := utf8.DecodeRuneInString(ph[pIdx-1:])
if runeAtIndex == utf8.RuneError && size == 0 {
fmt.Println("Invalid UTF-8 encoding or index out of range")
return ""
}
// Previous character is vowel
if mc.isVowel(string(runeAtIndex)) {
result += "." + string(r) // Add a dot before the current character
} else {
// Previous character is consonant
if runeAtIndex == r || runeAtIndex == 'N' {
result += "." + string(r) // Add a dot before the current character
} else {
result += string(r) // Add the current character as is
}
}
}
return result
}
// isVowel checks if a character is a vowel.
func (mc *moraeCreator) isVowel(char string) bool {
for _, v := range mc.vowels {
if string(v) == char {
return true
}
}
return false
}