Debian Codegraph @

texmf-dist/tex/luatex/kanaparser/kanaparser.lua

raw
1 -- Kana Parser lua engine
2
3 local vowels = {'a', 'e', 'i', 'o', 'u'} -- latin vowels
4 local vowelsK = {'ア', 'エ', 'イ', 'オ', 'ウ'} -- katakana vowels
5 local ambigousToN = {'あ', 'え', 'い', 'お', 'う', 'や', 'よ', 'ゆ'} -- characters ambiguous to preceding "n"
6 local littleTsuWL = {'s', 't', 'k', 'p', 'c'} -- whitelist for little tsu gemination
7 local transRaw = { -- latin -> hiragana
8 n = 'ん', a = 'あ', e = 'え', i = 'い', o = 'お', u = 'う',
9 ba = 'ば', be = 'べ', bi = 'び', bo = 'ぼ', bu = 'ぶ',
10 bya = 'びゃ', byo = 'びょ', byu = 'びゅ',
11 cha = 'ちゃ', che = 'ちぇ', chi = 'ち', cho = 'ちょ', chu = 'ちゅ',
12 da = 'だ', de = 'で', di = 'でぃ', ['do'] = 'ど', du = { 'づ', 'どぅ' },
13 dya = 'でゃ', dyo = 'でょ', dyu = 'でゅ',
14 fa = 'ふぁ', fe = 'ふぇ', fi = 'ふぃ', fo = 'ふぉ',
15 fya = 'ふゃ', fyo = 'ふょ', fyu = 'ふゅ',
16 ga = 'が', ge = 'げ', gi = 'ぎ', go = 'ご', gu = 'ぐ',
17 gwa = 'ぐぁ', gwe = 'ぐぇ', gwi = 'ぐぃ', gwo = 'ぐぉ', gya = 'ぎゃ', gyo = 'ぎょ', gyu = 'ぎゅ',
18 ha = 'は', he = 'へ', hi = 'ひ', ho = 'ほ', hu = 'ふ',
19 hya = 'ひゃ', hyo = 'ひょ', hyu = 'ひゅ',
20 ja = { 'じゃ', 'ぢゃ' }, je = 'じぇ', ji = { 'じ', 'ぢ' }, jo = { 'じょ', 'ぢょ' }, ju = { 'じゅ', 'ぢゅ' },
21 ka = 'か', ke = 'け', ki = 'き', ko = 'こ', ku = 'く',
22 kwa = 'くぁ', kwe = 'くぇ', kwi = 'くぃ', kwo = 'くぉ', kya = 'きゃ', kyo = 'きょ', kyu = 'きゅ',
23 ma = 'ま', me = 'め', mi = 'み', mo = 'も', mu = 'む',
24 mya = 'みゃ', myo = 'みょ', myu = 'みゅ',
25 na = 'な', ne = 'ね', ni = 'に', no = 'の', nu = 'ぬ',
26 nya = 'にゃ', nyo = 'にょ', nyu = 'にゅ',
27 pa = 'ぱ', pe = 'ぺ', pi = 'ぴ', po = 'ぽ', pu = 'ぷ',
28 pya = 'ぴゃ', pyo = 'ぴょ', pyu = 'ぴゅ',
29 ra = 'ら', re = 'れ', ri = 'り', ro = 'ろ', ru = 'る',
30 rya = 'りゃ', ryo = 'りょ', ryu = 'りゅ',
31 sa = 'さ', se = 'せ', si = 'し', so = 'そ', su = 'す',
32 sha = 'しゃ', she = 'しぇ', shi = 'し', sho = 'しょ', shu = 'しゅ',
33 ta = 'た', te = 'て', ti = 'てぃ', to = 'と',
34 tha = 'てゃ', tho = 'てょ', thu = 'てゅ',
35 tsa = 'つぁ', tse = 'つぇ', tsu = 'つ', tsi = 'つぃ', tso = 'つぉ',
36 tu = 'つ',
37 va = 'ゔぁ', ve = 'ゔぇ', vi = 'ゔぃ', vo = 'ゔぉ', vu = 'ゔぅ',
38 vya = 'ゔゃ', vyo = 'ゔょ', vyu = 'ゔゅ',
39 wa = 'わ', we = { 'うぇ', 'ゑ' }, wi = 'ゐ', wo = { 'を', 'うぉ' },
40 ya = 'や', ye = 'いぇ', yo = 'よ', yu = 'ゆ',
41 za = 'ざ', ze = 'ぜ', zo = 'ぞ', zu = 'ず'
42 }
43 local transK = { -- hiragana -> katakana
44 ['ん'] = 'ン', ['あ'] = 'ア', ['え'] = 'エ', ['い'] = 'イ', ['お'] = 'オ', ['う'] = 'ウ',
45 ['ぁ'] = 'ァ', ['ぃ'] = 'ィ', ['ぅ'] = 'ゥ', ['ぇ'] = 'ェ', ['ぉ'] = 'ォ',
46 ['ゃ'] = 'ャ', ['ゅ'] = 'ュ', ['ょ'] = 'ョ',
47 ['は'] = 'ハ', ['へ'] = 'ヘ', ['ひ'] = 'ヒ', ['ほ'] = 'ホ', ['ふ'] = 'フ',
48 ['ば'] = 'バ', ['べ'] = 'ベ', ['び'] = 'ビ', ['ぼ'] = 'ボ', ['ぶ'] = 'ブ',
49 ['ぱ'] = 'パ', ['ぺ'] = 'ペ', ['ぴ'] = 'ピ', ['ぽ'] = 'ポ', ['ぷ'] = 'プ',
50 ['た'] = 'タ', ['て'] = 'テ', ['ち'] = 'チ', ['と'] = 'ト', ['つ'] = 'ツ',
51 ['だ'] = 'ダ', ['で'] = 'デ', ['ぢ'] = 'ヂ', ['ど'] = 'ド', ['づ'] = 'ヅ',
52 ['か'] = 'カ', ['け'] = 'ケ', ['き'] = 'キ', ['こ'] = 'コ', ['く'] = 'ク',
53 ['が'] = 'ガ', ['げ'] = 'ゲ', ['ぎ'] = 'ギ', ['ご'] = 'ゴ', ['ぐ'] = 'グ',
54 ['ま'] = 'マ', ['め'] = 'マ', ['み'] = 'マ', ['も'] = 'モ', ['む'] = 'マ',
55 ['な'] = 'ナ', ['ね'] = 'ネ', ['に'] = 'ニ', ['の'] = 'ノ', ['ぬ'] = 'ヌ',
56 ['ら'] = 'ラ', ['れ'] = 'レ', ['り'] = 'リ', ['ろ'] = 'ロ', ['る'] = 'ル',
57 ['さ'] = 'サ', ['せ'] = 'セ', ['し'] = 'シ', ['そ'] = 'ソ', ['す'] = 'ス',
58 ['ざ'] = 'ザ', ['ぜ'] = 'ゼ', ['じ'] = 'ジ', ['ぞ'] = 'ゾ', ['ず'] = 'ズ',
59 ['わ'] = 'ワ', ['ゑ'] = 'ヱ', ['ゐ'] = 'ヰ', ['を'] = 'ヲ',
60 ['や'] = 'ヤ', ['よ'] = 'ヨ', ['ゆ'] = 'ユ',
61 ['ゔ'] = 'ヴ', ['っ'] = 'ッ'
62 }
63 local correctionsFromKana = { -- manual transliteration choices
64 ['し'] = 'shi'
65 }
66 local longK = 'ー'
67 local isolator = '\''
68 local prolongRules = { -- special rules for prolonging syllables
69 o = 'u',
70 e = 'i'
71 }
72
73 -- builds a reverse table
74 local function rev(t)
75 local res = {}
76 for k, v in pairs(t) do
77 if (type(v) == 'table') then
78 res[v[1]] = k
79 res[v[2]] = k
80 else
81 res[v] = k
82 end
83 end
84 return res
85 end
86
87 -- builds the default translation tables latin <-> kana from transRaw
88 local function buildDefaultTransTables()
89 local tr, rtr = {}, {}
90
91 for k, v in pairs(transRaw) do
92 tr[k] = type(v) == 'table' and v[1] or v
93 end
94
95 rtr = rev(tr)
96
97 -- apply corrections
98 for i, v in pairs(correctionsFromKana) do
99 rtr[i] = v
100 end
101
102 return tr, rtr, rev(transK)
103 end
104
105 -- decides which wovel should prolong the given vowel
106 local function prolong(c)
107 for i, v in ipairs(vowels) do
108 if c == v then
109 if prolongRules[c] then return prolongRules[c] else return c end
110 end
111 end
112 return nil
113 end
114
115 -- checks if a katakana token is a vowel and returns its latin representation
116 local function getWovelK(c)
117 for i, v in ipairs(vowelsK) do
118 if c == v then return vowels[i] end
119 end
120 return nil
121 end
122
123 -- checks if a given symbol is ambiguous to preceding n
124 local function isAmbiguous(c)
125 for i, v in ipairs(ambigousToN) do
126 if c == v then return true end
127 end
128 return false
129 end
130
131 -- init translation tables
132 local trans, revTrans, revTransK = buildDefaultTransTables()
133
134 -- init default transliteration choices (everything default to first alternative)
135 local transChoices = {}
136
137 -- checks if two characters are valid candidates for little tsu
138 local function isValidTsuCandidate(a, b)
139 if a ~= b then return false end
140 for i, v in ipairs(littleTsuWL) do
141 if a == v then return true end
142 end
143 return false
144 end
145
146 -- checks if two characters are a little tsu used correctly and returns the gemination consonant if true
147 local function getGeminationConsonant(a, b)
148 if a ~= 'っ' then return nil end -- disregard katakana, only hiragana is processed in romanization
149 local tr = revTrans[b]
150 if not tr then return nil end -- invalid hiragana character
151 local fst = string.sub(tr, 1, 1) -- get first character of the transliteration
152 for i, v in ipairs(littleTsuWL) do
153 if fst == v then return fst end
154 end
155 return nil -- invalid gemination
156 end
157
158 -- parses an utf8 string into utf8 chars (tokens)
159 local function tokenize(utf8str)
160 assert(type(utf8str) == 'string')
161 local res, seq, val = {}, 0, ''
162 for i = 1, #utf8str do
163 local c = string.byte(utf8str, i)
164 if seq == 0 then
165 if i ~= 1 then table.insert(res, val) end
166 seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
167 c < 0xF8 and 4 or error('invalid UTF-8 character sequence')
168 val = string.char(c)
169 else
170 val = val .. string.char(c)
171 end
172 seq = seq - 1
173 end
174 table.insert(res, val)
175 return res
176 end
177
178 -- PUBLIC API SECTION
179
180 -- toggles used characters for supplied syllables (whitespace-separated)
181 function toggleChars(input)
182 local cur, choices = '', {}
183 for s in string.gmatch(input, '%S+') do -- split by whitespaces
184 cur = trans[s]
185 if cur then -- don't process unknown syllables
186 choices = transRaw[s]
187 if type(choices) == 'table' then -- only process syllables with alternatives
188 trans[s] = cur == choices[1] and choices[2] or choices[1] -- toggle between alternatives
189 end
190 end
191 end
192 end
193
194 -- any kana to latin
195 function toLatin(input)
196 if input == '' then return end
197 local tbl = tokenize(input)
198 local buffer, res = {}, ''
199
200 -- read tokenized input
201 local tjoin, tfst, last, gc = '', '', 0, '' -- last is the last valid transliterated vowel, gc is the last gemination consonant
202 for i, v in ipairs(tbl) do
203 if revTransK[v] ~= nil then v = revTransK[v] end -- convert all katakana to hiragana
204 table.insert(buffer, v)
205
206 if #buffer == 2 then -- kana can be formed with up to two characters, always keep two in buffer
207 tjoin, tfst, gc = revTrans[ buffer[1] .. buffer[2] ], revTrans[ buffer[1] ], getGeminationConsonant(buffer[1], buffer[2])
208 if tjoin ~= nil then -- double character
209 res = res .. tjoin
210 buffer, last = {}, string.sub(tjoin, -1)
211 elseif gc then -- check for little tsu
212 res = res .. gc
213 buffer, last = {buffer[2]}, 0
214 elseif tfst ~= nil then -- single character
215 res = res .. tfst
216 if tfst == 'n' and isAmbiguous(buffer[2]) then -- ambiguous character succeeding an "n"
217 res = res .. isolator
218 end
219 buffer, last = {buffer[2]}, string.sub(tfst, -1)
220 elseif buffer[1] == longK and prolong(last) ~= nil then -- prolonging dash
221 res = res .. prolong(last)
222 buffer, last = {buffer[2]}, 0
223 else -- cannot transliterate, output as-is
224 res = res .. buffer[1]
225 buffer, last = {buffer[2]}, 0
226 end
227 end
228 end
229
230 if #buffer == 1 then -- trailing character
231 if revTrans[ buffer[1] ] ~= nil then -- single character
232 res = res .. revTrans [ buffer[1] ]
233 elseif buffer[1] == longK and prolong(last) ~= nil then -- prolonging dash
234 res = res .. prolong(last)
235 else -- cannot transliterate, output as-is
236 res = res .. buffer[1]
237 end
238 end
239
240 tex.print(res)
241 end
242
243 -- latin or katakana to hiragana, 'raw' parameter is for internal use, leave it blank to get output to TeX
244 function toHiragana(input, raw)
245 if input == '' then return end
246 local tbl = tokenize(input)
247 local buffer, res = {}, ''
248 local t3, t2, t1, last, lastsym, lastcnd = '', '', '', 0, nil, nil
249
250 for i, v in ipairs(tbl) do
251 if revTransK[v] then v = revTransK[v] end -- translate katakana to hiragana on the go
252 table.insert(buffer, v)
253
254 if #buffer == 3 then
255 t3, t2, t1 = trans[ buffer[1] .. buffer[2] .. buffer[3] ], trans[ buffer[1] .. buffer[2] ], trans[ buffer[1] ]
256 if t3 ~= nil then -- all three letters yield translation
257 if lastcnd then -- add little tsu
258 res = res .. 'っ'
259 lastcnd = nil
260 end
261 res = res .. t3
262 last = buffer[3]
263 buffer = {}
264 elseif t2 ~= nil then -- first two letters yield translation
265 if lastcnd then -- add little tsu
266 res = res .. 'っ'
267 lastcnd = nil
268 end
269 res = res .. t2
270 last = buffer[2]
271 buffer = {buffer[3]}
272 elseif isValidTsuCandidate(buffer[1], buffer[2]) then -- test little tsu candidates
273 if lastcnd then res = res .. lastcnd end -- add last consonant in raw form
274 lastcnd = buffer[1] -- set last candidate consonant
275 last = 0 -- is not vowel
276 buffer = {buffer[2], buffer[3]}
277 elseif t1 ~= nil then -- first letter yields translation : a, e, i, o, u, n
278 res = res .. t1
279 last = buffer[1]
280 buffer = {buffer[2], buffer[3]}
281 elseif buffer[1] == longK and prolong(last) ~= nil then -- valid prolonger sign
282 res = res .. trans[prolong(last)]
283 buffer, last = {buffer[2], buffer[3]}, 0
284 elseif buffer[1] == isolator then -- isolating apostrophe, consume it
285 buffer = {buffer[2], buffer[3]}
286 else
287 if lastcnd then -- add last consonant in raw form
288 res = res .. lastcnd
289 lastcnd = nil
290 end
291
292 -- this code allows for proper conversion of katakana's prolongation dash to hiragana
293 t1 = revTrans[ buffer[1] ]
294 if t1 then -- symbol is standalone hiragana
295 last = string.sub(t1, -1)
296 lastsym = buffer[1]
297 elseif lastsym then -- attempt to merge symbol with previous symbol
298 t1 = revTrans[ lastsym .. buffer[1] ]
299 if t1 then -- symbol is a valid non-standalone hiragana compound
300 last = string.sub(t1, -1)
301 else -- symbol is an invalid non-standalone hiragana compound
302 last = nil
303 end
304 lastsym = nil
305 else
306 last, lastsym = 0, nil
307 end
308
309 res = res .. buffer[1]
310 buffer = {buffer[2], buffer[3]}
311 end
312 end
313 end
314
315 if #buffer == 2 then
316 if trans[ buffer[1] .. buffer[2] ] ~= nil then -- first two symbols yield translation
317 if lastcnd then res = res .. 'っ' end -- add little tsu
318 res = res .. trans[ buffer[1] .. buffer[2] ]
319 last = buffer[2]
320 buffer = {}
321 elseif trans[ buffer[1] ] ~= nil then -- first symbol yields translation
322 res = res .. trans[ buffer[1] ]
323 last = buffer[1]
324 buffer = {buffer[2]}
325 elseif buffer[1] == longK and prolong(last) ~= nil then -- valid prolonger
326 res = res .. trans[prolong(last)]
327 buffer, last = {buffer[2]}, 0
328 elseif buffer[1] == isolator then -- consume isolator
329 buffer = {buffer[2]}
330 else
331 if lastcnd then res = res .. lastcnd end -- add last consonant in raw form
332
333 -- this code allows for proper conversion of katakana's prolongation dash to hiragana
334 t1 = revTrans[ buffer[1] ]
335 if t1 then -- symbol is standalone hiragana
336 last = string.sub(t1, -1)
337 lastsym = buffer[1]
338 elseif lastsym then -- attempt to merge symbol with previous symbol
339 t1 = revTrans[ lastsym .. buffer[1] ]
340 if t1 then -- symbol is a valid non-standalone hiragana compound
341 last = string.sub(t1, -1)
342 else -- symbol is an invalid non-standalone hiragana compound
343 last = nil
344 end
345 lastsym = nil -- erase last valid symbol
346 else
347 last, lastsym = 0, nil
348 end
349
350 res = res .. buffer[1]
351 buffer = {buffer[2]}
352 end
353 end
354
355 if #buffer == 1 then -- remaining symbol
356 if trans[ buffer[1] ] ~= nil then
357 res = res .. trans[ buffer[1] ]
358 elseif buffer[1] == longK and prolong(last) ~= nil then
359 res = res .. trans[prolong(last)]
360 elseif buffer[1] ~= isolator then
361 res = res .. buffer[1]
362 end
363 end
364
365 if not raw then
366 tex.print(res)
367 else
368 return res -- for internal use
369 end
370 end
371
372 -- latin or hiragana to katakana
373 function toKatakana(input)
374 if input == '' then return end
375 local hiraganized = tokenize(toHiragana(input, true)) -- convert everything to hiragana
376
377 -- replace hiragana with katakana
378 for i, v in ipairs(hiraganized) do
379 if transK[v] ~= nil then
380 hiraganized[i] = transK[v]
381 end
382 end
383
384 -- insert prolonging symbols and prepare output
385 local prev, nxt, vowel, tprev, tnext, res = hiraganized[1], '', '', '', '', hiraganized[1]
386 local merge, toprolong = '', nil
387 for i = 2, #hiraganized do
388 nxt = hiraganized[i]
389
390 vowel = getWovelK(nxt)
391
392 if not toprolong then -- check prev for ending vowel
393 tprev = revTransK[prev]
394 if tprev then
395 tprev = revTrans[tprev]
396 if tprev then
397 toprolong = prolong(string.sub(tprev, -1))
398 end
399 end
400 end
401
402 if toprolong then -- check nxt for matching prolonger
403 if toprolong == vowel then
404 nxt = longK
405 toprolong = nil
406 elseif vowel then
407 toprolong = prolong(vowel)
408 else
409 toprolong = nil
410 end
411 end
412
413 -- try merging prev and nxt for a single token
414 tprev, tnext = revTransK[prev], revTransK[nxt]
415 if tprev and tnext then
416 merge = revTrans[tprev .. tnext]
417 if merge then
418 toprolong = prolong(string.sub(merge, -1))
419 end
420 end
421
422 res = res .. nxt
423 prev = nxt
424 end
425
426 tex.print(res)
427 end