-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinit.lua
201 lines (188 loc) · 4.9 KB
/
init.lua
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
190
191
192
193
194
195
196
197
198
199
200
201
local dictionary = require("./data/hanzi")
local find, sub = string.find, string.sub
local split = function(str, sep, nmax)
if sep == nil then
sep = '%s+'
end
local r = { }
if #str <= 0 then
return r
end
local plain = false
nmax = nmax or -1
local nf = 1
local ns = 1
local nfr, nl = find(str, sep, ns, plain)
while nfr and nmax ~= 0 do
r[nf] = sub(str, ns, nfr - 1)
nf = nf + 1
ns = nl + 1
nmax = nmax - 1
nfr, nl = find(str, sep, ns, plain)
end
r[nf] = sub(str, ns)
return r
end
local phoneticTable = {
["ā"] = "a1",
["á"] = "a2",
["ǎ"] = "a3",
["à"] = "a4",
["ē"] = "e1",
["é"] = "e2",
["ě"] = "e3",
["è"] = "e4",
["ō"] = "o1",
["ó"] = "o2",
["ǒ"] = "o3",
["ò"] = "o4",
["ī"] = "i1",
["í"] = "i2",
["ǐ"] = "i3",
["ì"] = "i4",
["ū"] = "u1",
["ú"] = "u2",
["ǔ"] = "u3",
["ù"] = "u4",
["ü"] = "v0",
["ǘ"] = "v2",
["ǚ"] = "v3",
["ǜ"] = "v4",
["ń"] = "n2",
["ň"] = "n3",
[""] = "m2",
};
local accentMap = {
["à"] = "a",
["á"] = "a",
["ä"] = "a",
["â"] = "a",
["è"] = "e",
["é"] = "e",
["ë"] = "e",
["ê"] = "e",
["ì"] = "i",
["í"] = "i",
["ï"] = "i",
["î"] = "i",
["ò"] = "o",
["ó"] = "o",
["ö"] = "o",
["ô"] = "o",
["ù"] = "u",
["ú"] = "u",
["ü"] = "u",
["û"] = "u",
["ñ"] = "n",
["ç"] = "c",
["ā"] = "a1",
["ǎ"] = "a3",
["ē"] = "e1",
["ě"] = "e3",
["ō"] = "o1",
["ǒ"] = "o3",
["ī"] = "i1",
["ǐ"] = "i3",
["ū"] = "u1",
["ǔ"] = "u3",
["ü"] = "v0",
["ǘ"] = "v2",
["ǚ"] = "v3",
["ǜ"] = "v4",
["ń"] = "n2",
["ň"] = "n3",
[""] = "m2",
}
function Utf8to32(utf8str)
local bit
if type(bit32) == "table" then
bit = bit32
end
assert(type(utf8str) == "string")
local res, seq, val = {}, 0, nil
for i = 1, #utf8str do
local c = string.byte(utf8str, i)
if seq == 0 then
table.insert(res, val)
seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
error("invalid UTF-8 character sequence")
val = bit.band(c, 2^(8-seq) - 1)
else
val = bit.bor(bit.lshift(val, 6), bit.band(c, 0x3F))
end
seq = seq - 1
end
table.insert(res, val)
return res
end
function num2hex(num)
local hexstr = '0123456789abcdef'
local s = ''
while num > 0 do
local mod = math.fmod(num, 16)
s = string.sub(hexstr, mod+1, mod+1) .. s
num = math.floor(num / 16)
end
if s == '' then s = '0' end
return s
end
function Pinyin (ustring, flat, keepNull)
local stringArray = {}
local tempAlphas = {}
string.gsub(ustring, "([%z\1-\127\194-\244][\128-\191]*)", function(singleAlpha, b)
if #singleAlpha == 2 then
if accentMap[singleAlpha] then
singleAlpha = accentMap[singleAlpha]
end
end
if #singleAlpha > 1 then
local hex = num2hex(Utf8to32(singleAlpha)[1])
local pinyin = dictionary[hex]
if #tempAlphas > 0 then
table.insert(stringArray, table.concat(tempAlphas))
tempAlphas = {}
end
if pinyin then
pinyin = split(pinyin, ",")[1]
if flat then
pinyin = pinyin:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(phonetic)
if #phonetic > 1 then
return (phoneticTable[phonetic]):sub(0, 1)
else
return phonetic
end
end)
end
table.insert(stringArray, pinyin)
else
if keepNull then
table.insert(stringArray, "")
end
end
else
local hasEmptyStr = singleAlpha:find("[\n%s\t]")
if hasEmptyStr and #tempAlphas > 0 then
table.insert(stringArray, table.concat(tempAlphas))
tempAlphas = {}
else
table.insert(tempAlphas, singleAlpha:lower())
end
end
end)
if #tempAlphas > 0 then
table.insert(stringArray, table.concat(tempAlphas))
tempAlphas = {}
end
return stringArray
end
-- print(table.concat(Pinyin("你好,世界!Hello World"), " "))
-- print(Pinyin("你好,世界!Hello World!", true))
-- print(table.concat(Pinyin("你好,世界!Hello World!", true), "-"))
-- Welcome
-- print(table.concat(Pinyin("汉语拼音"), " "))
if type(module) == "table" then
module.exports = Pinyin
else
return Pinyin
end