-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadings.go
161 lines (139 loc) · 3.17 KB
/
readings.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
package unihand
import (
"fmt"
"log"
"regexp"
)
var (
rxRad *regexp.Regexp
)
func init() {
// Convert Pinyin from impossible-to-type to easy-to-type format
fileFilters = append(fileFilters, fileFilter{
Filename: "Unihan_Readings.txt",
RecordFilter: pinyin,
})
// English translation
fileFilters = append(fileFilters, fileFilter{
Filename: "Unihan_Readings.txt",
RecordFilter: englishDefinition,
})
rxRad = regexp.MustCompile("(^|[,;]) ?(KangXi )?radical( number)? ?(\\d+)\\s*([,;]?)\\s*")
}
func englishDefinition(char *Character, code uint32, fields []string) error {
if len(fields) < 2 {
return fmt.Errorf("got invalid record [%s]", fields)
}
if fields[0] == "kDefinition" {
char.English = fields[1]
match := rxRad.FindStringSubmatchIndex(char.English)
if match != nil {
fmt.Sscan(char.English[match[8]:match[9]], &char.KangXiRadicalNumber)
// Remove 'KangXi radical number x' from the definition
if match[2] != match[3] && match[10] != match[11] {
char.English = char.English[:match[0]] + "; " + char.English[match[1]:]
} else {
char.English = char.English[:match[0]] + char.English[match[1]:]
}
}
}
return nil
}
func pinyin(char *Character, code uint32, fields []string) error {
if len(fields) < 2 {
return fmt.Errorf("got invalid record [%s]", fields)
}
if fields[0] == "kMandarin" {
var hans, hant string
n, _ := fmt.Sscan(fields[1], &hans, &hant)
if n >= 1 {
var t error
char.Pinyin, t = toneNumber(hans)
if t != nil {
log.Printf("odd tone for U+%04X (%c): %s", code, rune(code), t.Error())
return nil
}
if n == 1 {
// char.PinyinTW = hans
} else {
char.PinyinTW, _ = toneNumber(hant)
}
}
}
return nil
}
var toneChars = []struct {
Char rune
Tone int
Word string
}{
// Combining characters
{0x0304, 1, ""},
{0x0301, 2, ""},
{0x030c, 3, ""},
{0x0300, 4, ""},
// Non-combining characters
{0x0101, 1, "a"},
{0x00E1, 2, "a"},
{0x01CE, 3, "a"},
{0x00E0, 4, "a"},
{0x0113, 1, "e"},
{0x00E9, 2, "e"},
{0x011B, 3, "e"},
{0x00E8, 4, "e"},
{0x012B, 1, "i"},
{0x00ED, 2, "i"},
{0x01D0, 3, "i"},
{0x00EC, 4, "i"},
{0x014D, 1, "o"},
{0x00F3, 2, "o"},
{0x01D2, 3, "o"},
{0x00F2, 4, "o"},
{0x016B, 1, "u"},
{0x00FA, 2, "u"},
{0x01D4, 3, "u"},
{0x00F9, 4, "u"},
// U-umlaut -> v
{0x00FC, 0, "v"},
{0x01D6, 1, "v"},
{0x01D8, 2, "v"},
{0x01DA, 3, "v"},
{0x01DC, 4, "v"},
// Whatever, man
{0x0144, 2, "n"},
{0x0148, 3, "n"},
{0x01f9, 4, "n"},
{0x1e3f, 2, "m"},
}
func toneNumber(pinyin string) (string, error) {
tone := 5
word := ""
for _, p := range pinyin {
found := false
for _, py := range toneChars {
if py.Char == p {
found = true
if py.Tone != 0 {
tone = py.Tone
}
word += py.Word
break
}
}
if found {
// Yay!
} else if p == 0x0308 {
// Combining Umlaut - change 'u' to 'v'
if len(word) > 0 && word[len(word)-1] == 'u' {
word = word[:len(word)-1] + "v"
} else {
log.Printf("umlaut? %02x", []byte(pinyin))
}
} else if p >= 'a' && p <= 'z' {
word += string(p)
} else {
return "", fmt.Errorf("invalid character U+%04x in pinyin '%s'", p, pinyin)
}
}
return fmt.Sprintf("%s%d", word, tone), nil
}