-
Notifications
You must be signed in to change notification settings - Fork 7
/
hanjpautomata.c
567 lines (489 loc) · 15 KB
/
hanjpautomata.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include "hanjpautomata.h"
#include "hanjpunicode.h"
#include <gmodule.h>
#include <hangul.h>
/* Extern function signature which isn't exported in libhangul header */
extern ucschar hangul_choseong_to_jongseong(ucschar c);
extern ucschar hangul_jongseong_to_choseong(ucschar c);
/* HanjpBuffer Implementation */
static gunichar
hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) {
if(hangul_is_choseong(ch)) {
if(buffer->cho == 0) {
buffer->cho = ch;
}
else {
return ch;
}
}
else if(hangul_is_jungseong(ch)) {
if(buffer->jung == 0) {
buffer->jung = ch;
}
else {
if(buffer->jung2 == 0) {
buffer->jung2 = ch;
}
else {
return ch;
}
}
}
else if(hangul_is_jongseong(ch)) {
if(buffer->jong == 0) {
buffer->jong = ch;
}
else {
return ch;
}
}
else {
return ch;
}
return 0;
}
static gint
hanjp_buffer_peek(HanjpBuffer *buffer, GArray *dest) {
int i;
for(i = 0; i < 4; i++) {
if(buffer->stack[i] != 0) {
g_array_append_val(dest, buffer->stack[i]);
}
}
}
static gunichar
hanjp_buffer_pop(HanjpBuffer *buffer) {
int i;
gunichar r;
for(i = 3; i >= 0; i--) {
r = buffer->stack[i];
if(r != 0) {
buffer->stack[i] = 0;
break;
}
}
return r;
}
static void
hanjp_buffer_flush(HanjpBuffer *buffer) {
buffer->cho = 0;
buffer->jung = 0;
buffer->jung2 = 0;
buffer->jong = 0;
}
/**/
/* Consts about to_kana convertion */
enum {
HANJP_VOWEL_A,
HANJP_VOWEL_I,
HANJP_VOWEL_U,
HANJP_VOWEL_E,
HANJP_VOWEL_O
};
enum {
HANJP_CONSONANT__,
HANJP_CONSONANT_K,
HANJP_CONSONANT_S,
HANJP_CONSONANT_T,
HANJP_CONSONANT_N,
HANJP_CONSONANT_H,
HANJP_CONSONANT_M,
HANJP_CONSONANT_Y,
HANJP_CONSONANT_R,
HANJP_CONSONANT_W
};
// Fifty notes
// For example か(ka) = kana_table[HANJP_CONSONANT_K][HANJP_VOWEL_A]
static const gunichar kana_table[][5] = {
// A, I, U, E, O
{0x3042, 0x3044, 0x3046, 0x3048, 0x304A}, // A
{0x304B, 0x304D, 0x304F, 0x3051, 0x3053}, // KA
{0x3055, 0x3057, 0x3059, 0x305B, 0x305D}, // SA
{0x305F, 0x3061, 0x3064, 0x3066, 0x3068}, // TA
{0x306A, 0x306B, 0x306C, 0x306D, 0x306E}, // NA
{0x306F, 0x3072, 0x3075, 0x3078, 0x307B}, // HA
{0x307E, 0x307F, 0x3080, 0x3081, 0x3082}, // MO
{0x3084, 0x0000, 0x3086, 0x0000, 0x3088}, // YA
{0x3089, 0x308A, 0x308B, 0x308C, 0x308D}, // RA
{0x308F, 0x3090, 0x0000, 0x3091, 0x3092} // WA
};
static const gunichar kana_nn = 0x3093;
/* Combine multiple JungSeong int single code */
typedef union {
struct {
gunichar jung;
gunichar jung2;
};
guint64 value;
} JungBox;
#define N_COMBINE_TABLE_ELEMENTS 30
/* Interface Definition */
G_DEFINE_INTERFACE(HanjpAutomata, hanjp_am, G_TYPE_OBJECT)
static void
hanjp_am_default_init(HanjpAutomataInterface *iface) {
/* add properties and signals to the interface here */
}
gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) {
HanjpAutomataInterface *iface;
g_return_if_fail(HANJP_IS_AUTOMATA(am));
iface = HANJP_AUTOMATA_GET_IFACE(am);
g_return_if_fail(iface->to_kana != NULL);
return iface->to_kana(am, dest, buffer);
}
gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar ch) {
HanjpAutomataInterface *iface;
g_return_if_fail(HANJP_IS_AUTOMATA(am));
iface = HANJP_AUTOMATA_GET_IFACE(am);
g_return_if_fail(iface->push != NULL);
return iface->push(am, result, hangul, ch);
}
gboolean hanjp_am_backspace(HanjpAutomata *am) {
HanjpAutomataInterface *iface;
g_return_if_fail(HANJP_IS_AUTOMATA(am));
iface = HANJP_AUTOMATA_GET_IFACE(am);
g_return_if_fail(iface->backspace != NULL);
return iface->backspace(am);
}
void hanjp_am_flush(HanjpAutomata *am) {
HanjpAutomataInterface *iface;
g_return_if_fail(HANJP_IS_AUTOMATA(am));
iface = HANJP_AUTOMATA_GET_IFACE(am);
g_return_if_fail(iface->flush != NULL);
iface->flush(am);
}
/**/
typedef struct {
HanjpBuffer buffer;
GHashTable *combine_table;
JungBox combine_table_keys[N_COMBINE_TABLE_ELEMENTS];
guint32 combine_table_vals[N_COMBINE_TABLE_ELEMENTS];
} HanjpAutomataBasePrivate;
G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_ambase, G_TYPE_OBJECT)
static gint
hanjp_ambase_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer)
{
gint r = 0;
gint adj;
gint i, j;
JungBox jungkey;
gunichar ch;
gunichar *val;
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
for(i = 0; i < 4; i++) {
if(buffer->stack[i] != 0 && !hangul_is_jamo(buffer->stack[i])) {
return -1;
}
}
ch = buffer->cho;
if(ch == HANJP_CHOSEONG_FILLER) {
buffer->cho = 0;
}
ch = buffer->jung;
if(ch == HANJP_JUNGSEONG_FILLER) {
buffer->jung = 0;
}
ch = buffer->jung2;
if(ch == HANJP_JUNGSEONG_FILLER) {
buffer->jung2 = 0;
}
// check batchim is available and move choseong to jongseong
if(buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) {
ch = g_array_index(dest, gunichar, dest->len - 1);
if(ch != kana_nn && ch != kana_table[HANJP_CONSONANT_T][HANJP_VOWEL_U] - 1) {
buffer->jong = hangul_choseong_to_jongseong(buffer->cho);
buffer->cho = 0;
}
}
//eat choseong, jungseong
while(buffer->cho || buffer->jung) {
adj = 0;
ch = buffer->cho;
buffer->cho = 0;
switch(ch) {
case 0: // VOID
adj = -1;
case HANJP_CHOSEONG_IEUNG: // ㅇ
i = HANJP_CONSONANT__; break;
case HANJP_CHOSEONG_KIYEOK: // ㄱ
adj = 1;
case HANJP_CHOSEONG_KHIEUKH: // ㅋ
case HANJP_CHOSEONG_SSANGKIYEOK: // ㄲ
i = HANJP_CONSONANT_K; break; // K
case HANJP_CHOSEONG_CIEUC: // ㅈ
adj = 1;
case HANJP_CHOSEONG_SIOS: // ㅅ
case HANJP_CHOSEONG_SSANGSIOS: // ㅆ
i = HANJP_CONSONANT_S; break; // S
case HANJP_CHOSEONG_TIKEUT: // ㄷ
adj = 1;
case HANJP_CHOSEONG_SSANGTIKEUT: // ㄸ
case HANJP_CHOSEONG_CHIEUCH: // ㅊ
case HANJP_CHOSEONG_THIEUTH: // ㅌ
i = HANJP_CONSONANT_T; break; // T
case HANJP_CHOSEONG_NIEUN: // ㄴ
i = HANJP_CONSONANT_N; break; // N
case HANJP_CHOSEONG_PHIEUPH: // ㅍ
adj = 1;
case HANJP_CHOSEONG_SSANGPIEUP: // ㅃ
adj += 1;
case HANJP_CHOSEONG_HIEUH: // ㅎ
i = HANJP_CONSONANT_H; break; // H
case HANJP_CHOSEONG_MIEUM: // ㅁ
i = HANJP_CONSONANT_M; break; // M
case HANJP_CHOSEONG_RIEUL: // ㄹ
i = HANJP_CONSONANT_R; break; // R
case HANJP_CHOSEONG_SSANGNIEUN:
g_array_append_val(dest, kana_nn);
r++;
continue;
default:
return -1;
}
// reduce jung
ch = buffer->jung2;
buffer->jung2 = 0;
if(buffer->jung == 0) {
buffer->jung = ch;
}
else {
jungkey.jung = buffer->jung;
jungkey.jung2 = ch;
val = (gunichar *)g_hash_table_lookup(priv->combine_table, &jungkey.value);
if(val != NULL) {
buffer->jung = *val;
}
else {
buffer->jung2 = ch;
}
}
// divide jungseong
ch = buffer->jung;
switch(ch) {
case HANJP_JUNGSEONG_WA:
if(i == HANJP_CONSONANT__) {
i = HANJP_CONSONANT_W;
}
else {
buffer->jung = HANJP_JUNGSEONG_O;
buffer->jung2 = HANJP_JUNGSEONG_A;
}
break;
case HANJP_JUNGSEONG_YA:
case HANJP_JUNGSEONG_YU:
case HANJP_JUNGSEONG_YO:
case HANJP_JUNGSEONG_YEO:
if(i == HANJP_CONSONANT__) {
i = HANJP_CONSONANT_Y;
}
else{
buffer->jung = HANJP_JUNGSEONG_I;
buffer->jung2 = ch;
}
break;
case HANJP_JUNGSEONG_YE:
case HANJP_JUNGSEONG_YAE:
buffer->jung = HANJP_JUNGSEONG_I;
buffer->jung2 = HANJP_JUNGSEONG_E;
break;
}
//select column index
ch = buffer->jung;
buffer->jung = 0;
switch(ch) {
case HANJP_JUNGSEONG_WA:
case HANJP_JUNGSEONG_YA:
case HANJP_JUNGSEONG_A:
j = HANJP_VOWEL_A; break;
case HANJP_JUNGSEONG_I:
j = HANJP_VOWEL_I; break;
case HANJP_JUNGSEONG_YU:
case HANJP_JUNGSEONG_EU:
case HANJP_JUNGSEONG_U:
case 0:
j = HANJP_VOWEL_U; break;
case HANJP_JUNGSEONG_AE:
case HANJP_JUNGSEONG_E:
j = HANJP_VOWEL_E; break;
case HANJP_JUNGSEONG_YO:
case HANJP_JUNGSEONG_YEO:
case HANJP_JUNGSEONG_EO:
case HANJP_JUNGSEONG_O:
j = HANJP_VOWEL_O; break;
default:
return FALSE;
}
ch = kana_table[i][j] + adj;
g_array_append_val(dest, ch);
r++;
}
// eat jongseong
ch = buffer->jong;
buffer->jong = 0;
if(ch != 0) {
switch(ch) {
case HANJP_JONGSEONG_KIYEOK:
case HANJP_JONGSEONG_SSANGKIYEOK:
case HANJP_JONGSEONG_SIOS:
case HANJP_JONGSEONG_SSANGSIOS:
case HANJP_JONGSEONG_KHIEUKH:
ch = kana_table[HANJP_CONSONANT_T][HANJP_VOWEL_U] - 1; break;
case HANJP_JONGSEONG_NIEUN:
case HANJP_JONGSEONG_MIEUM:
case HANJP_JONGSEONG_PIEUP:
case HANJP_JONGSEONG_PHIEUPH:
case HANJP_JONGSEONG_IEUNG:
ch = kana_nn; break;
default:
buffer->cho = hangul_jongseong_to_choseong(buffer->jong);
return r + hanjp_ambase_to_kana(am, dest, buffer);
}
g_array_append_val(dest, ch);
r++;
}
return r;
}
static void
hanjp_ambase_peek(HanjpAutomata *am, GArray *hangul)
{
JungBox jungkey;
gunichar c;
gunichar *val;
gint i;
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
if(priv->buffer.jung == 0) {
priv->buffer.jung = priv->buffer.jung2;
priv->buffer.jung2 = 0;
}
if(priv->buffer.jung != 0 && priv->buffer.jung2 != 0) {
jungkey.jung = priv->buffer.jung;
jungkey.jung2 = priv->buffer.jung2;
val = (gunichar*)g_hash_table_lookup(priv->combine_table, &jungkey.value);
if(val != NULL) {
priv->buffer.jung = *val;
priv->buffer.jung2 = 0;
}
}
c = hangul_jamo_to_syllable(priv->buffer.cho, priv->buffer.jung, priv->buffer.jong);
if(c == 0) {
for(i = 0; i < 4; i++) {
c = priv->buffer.stack[i];
if(c != 0) {
c = hangul_jamo_to_cjamo(c);
g_array_append_val(hangul, c);
break;
}
}
}
else {
g_array_append_val(hangul, c);
}
}
static gboolean
hanjp_ambase_backspace(HanjpAutomata *am)
{
int i;
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
return (hanjp_buffer_pop(&priv->buffer) != 0);
}
static void
hanjp_ambase_flush(HanjpAutomata *am)
{
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
hanjp_buffer_flush(&priv->buffer);
}
static void
hanjp_ambase_init(HanjpAutomataBase *am)
{
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(am);
priv->buffer.cho = 0;
priv->buffer.jung = 0;
priv->buffer.jung2 = 0;
priv->buffer.jong = 0;
priv->combine_table = g_hash_table_new(g_int64_hash, g_int64_equal);
}
static void
hanjp_ambase_finalize(GObject *gobject)
{
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(gobject));
g_hash_table_destroy(priv->combine_table);
G_OBJECT_CLASS(hanjp_ambase_parent_class)->finalize(gobject);
}
static void
hanjp_ambase_class_init(HanjpAutomataBaseClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = hanjp_ambase_finalize;
klass->to_kana = hanjp_ambase_to_kana;
klass->push = NULL;
klass->backspace = hanjp_ambase_backspace;
klass->flush = hanjp_ambase_flush;
}
static void hanjp_default_am_interface_init(HanjpAutomataInterface *iface);
G_DEFINE_TYPE_WITH_CODE(HanjpAutomataDefault, hanjp_amdefault, HANJP_TYPE_AUTOMATABASE,
G_IMPLEMENT_INTERFACE(HANJP_TYPE_AUTOMATA,
hanjp_default_am_interface_init))
static void
hanjp_amdefault_init(HanjpAutomataDefault *am)
{
HanjpAutomataBasePrivate *priv;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
priv->combine_table_keys[0].jung = HANJP_JUNGSEONG_O;
priv->combine_table_keys[0].jung2 = HANJP_JUNGSEONG_A;
priv->combine_table_vals[0] = HANJP_JUNGSEONG_WA;
g_hash_table_insert(priv->combine_table, &priv->combine_table_keys[0].value, &priv->combine_table_vals[0]);
}
static gint
hanjp_amdefault_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunichar ch)
{
gint r = 0;
HanjpAutomataBasePrivate *priv;
HanjpBuffer *buffer;
priv = hanjp_ambase_get_instance_private(HANJP_AUTOMATABASE(am));
buffer = &priv->buffer;
if(ch == 0) {
return 0;
}
// push jaso to buffer
ch = hanjp_buffer_push(buffer, ch);
// post-step
// convert poped string to kana
if(ch != 0 && buffer->jong == 0) {
hanjp_ambase_peek(am, hangul);
r = hanjp_ambase_to_kana(am, preedit, buffer);
if(!hangul_is_jamo(ch)) {
g_array_append_val(hangul, ch);
g_array_append_val(preedit, ch);
r++;
r = -r;
}
else {
hanjp_buffer_push(buffer, ch);
}
}
hanjp_ambase_peek(am, preedit);
return r;
}
static void
hanjp_amdefault_class_init(HanjpAutomataDefaultClass *klass)
{
HanjpAutomataBaseClass *base_class = HANJP_AUTOMATABASE_CLASS(klass);
base_class->push = hanjp_amdefault_push;
}
HanjpAutomataDefault *hanjp_amdefault_new()
{
return g_object_new(HANJP_TYPE_AUTOMATADEFAULT, NULL);
}
static void
hanjp_default_am_interface_init(HanjpAutomataInterface *iface)
{
iface->to_kana = hanjp_ambase_to_kana;
iface->push = hanjp_amdefault_push;
iface->backspace = hanjp_ambase_backspace;
iface->flush = hanjp_ambase_flush;
}