20
20
# along with ibus-bogo. If not, see <http://www.gnu.org/licenses/>.
21
21
#
22
22
23
+ """
24
+ Read the docstring for process_sequence() and process_key() first.
25
+ """
26
+
23
27
from __future__ import unicode_literals
24
28
from bogo .validation import is_valid_combination
25
29
from bogo import utils , accent , mark
30
34
Accent = accent .Accent
31
35
32
36
33
- class Action :
37
+ class _Action :
34
38
UNDO = 3
35
39
ADD_MARK = 2
36
40
ADD_ACCENT = 1
@@ -93,14 +97,21 @@ def get_vni_definition():
93
97
}
94
98
95
99
96
- def is_processable (comps ):
100
+ def _is_processable (comps ):
97
101
# For now only check the last 2 components
98
102
return is_valid_combination (('' , comps [1 ], comps [2 ]), final_form = False )
99
103
100
104
101
105
def process_sequence (sequence ,
102
106
rules = None ,
103
107
skip_non_vietnamese = True ):
108
+ """\
109
+ Convert a key sequence into a Vietnamese string with diacritical marks.
110
+
111
+ Args:
112
+ rules (optional): see docstring for process_key().
113
+ skip_non_vietnamese (optional): see docstring for process_key().
114
+ """
104
115
result = ""
105
116
raw = result
106
117
@@ -177,37 +188,37 @@ def default_return():
177
188
178
189
comps = utils .separate (string )
179
190
180
- # if not is_processable (comps):
191
+ # if not _is_processable (comps):
181
192
# return default_return()
182
193
183
194
# Find all possible transformations this keypress can generate
184
- trans_list = get_transformation_list (
195
+ trans_list = _get_transformation_list (
185
196
key , rules , fallback_sequence )
186
197
187
198
# Then apply them one by one
188
199
new_comps = list (comps )
189
200
for trans in trans_list :
190
- new_comps = transform (new_comps , trans )
201
+ new_comps = _transform (new_comps , trans )
191
202
192
203
if new_comps == comps :
193
204
tmp = list (new_comps )
194
205
195
206
# If none of the transformations (if any) work
196
207
# then this keystroke is probably an undo key.
197
- if can_undo (new_comps , trans_list ):
208
+ if _can_undo (new_comps , trans_list ):
198
209
# The prefix "_" means undo.
199
210
for trans in map (lambda x : "_" + x , trans_list ):
200
- new_comps = transform (new_comps , trans )
211
+ new_comps = _transform (new_comps , trans )
201
212
202
213
# Undoing the w key with the TELEX input method with the
203
214
# w:<ư extension requires some care.
204
215
#
205
216
# The input (ư, w) should be undone as w
206
217
# on the other hand, (ư, uw) should return uw.
207
218
#
208
- # transform () is not aware of the 2 ways to generate
219
+ # _transform () is not aware of the 2 ways to generate
209
220
# ư in TELEX and always think ư was created by uw.
210
- # Therefore, after calling transform () to undo ư,
221
+ # Therefore, after calling _transform () to undo ư,
211
222
# we always get ['', 'u', ''].
212
223
#
213
224
# So we have to clean it up a bit.
@@ -251,14 +262,14 @@ def user_didnt_type_uww():
251
262
return result
252
263
253
264
254
- def get_transformation_list (key , im , fallback_sequence ):
265
+ def _get_transformation_list (key , im , fallback_sequence ):
255
266
"""
256
- Return the list of transformations inferred from the entered key. The
257
- map between transform types and keys is given by module
258
- bogo_config (if exists) or by variable simple_telex_im
267
+ Return the list of transformations inferred from the entered key. The
268
+ map between transform types and keys is given by module
269
+ bogo_config (if exists) or by variable simple_telex_im
259
270
260
- if entered key is not in im, return "+key", meaning appending
261
- the entered key to current text
271
+ if entered key is not in im, return "+key", meaning appending
272
+ the entered key to current text
262
273
"""
263
274
# if key in im:
264
275
# lkey = key
@@ -282,7 +293,7 @@ def get_transformation_list(key, im, fallback_sequence):
282
293
# TODO Use takewhile()/dropwhile() to process the last IM keypress
283
294
# instead of assuming it's the last key in fallback_sequence.
284
295
t = list (map (lambda x : "_" + x ,
285
- get_transformation_list (fallback_sequence [- 2 ], im ,
296
+ _get_transformation_list (fallback_sequence [- 2 ], im ,
286
297
fallback_sequence [:- 1 ])))
287
298
# print(t)
288
299
trans_list = t
@@ -294,61 +305,61 @@ def get_transformation_list(key, im, fallback_sequence):
294
305
return ['+' + key ]
295
306
296
307
297
- def get_action (trans ):
308
+ def _get_action (trans ):
298
309
"""
299
310
Return the action inferred from the transformation `trans`.
300
311
and the parameter going with this action
301
- An Action .ADD_MARK goes with a Mark
302
- while an Action .ADD_ACCENT goes with an Accent
312
+ An _Action .ADD_MARK goes with a Mark
313
+ while an _Action .ADD_ACCENT goes with an Accent
303
314
"""
304
315
# TODO: VIQR-like convention
305
316
if trans [0 ] in ('<' , '+' ):
306
- return Action .ADD_CHAR , trans [1 ]
317
+ return _Action .ADD_CHAR , trans [1 ]
307
318
if trans [0 ] == "_" :
308
- return Action .UNDO , trans [1 :]
319
+ return _Action .UNDO , trans [1 :]
309
320
if len (trans ) == 2 :
310
321
if trans [1 ] == '^' :
311
- return Action .ADD_MARK , Mark .HAT
322
+ return _Action .ADD_MARK , Mark .HAT
312
323
if trans [1 ] == '+' :
313
- return Action .ADD_MARK , Mark .BREVE
324
+ return _Action .ADD_MARK , Mark .BREVE
314
325
if trans [1 ] == '*' :
315
- return Action .ADD_MARK , Mark .HORN
326
+ return _Action .ADD_MARK , Mark .HORN
316
327
if trans [1 ] == "-" :
317
- return Action .ADD_MARK , Mark .BAR
328
+ return _Action .ADD_MARK , Mark .BAR
318
329
# if trans[1] == "_":
319
- # return Action .ADD_MARK, Mark.NONE
330
+ # return _Action .ADD_MARK, Mark.NONE
320
331
else :
321
332
if trans [0 ] == "\\ " :
322
- return Action .ADD_ACCENT , Accent .GRAVE
333
+ return _Action .ADD_ACCENT , Accent .GRAVE
323
334
if trans [0 ] == "/" :
324
- return Action .ADD_ACCENT , Accent .ACUTE
335
+ return _Action .ADD_ACCENT , Accent .ACUTE
325
336
if trans [0 ] == "?" :
326
- return Action .ADD_ACCENT , Accent .HOOK
337
+ return _Action .ADD_ACCENT , Accent .HOOK
327
338
if trans [0 ] == "~" :
328
- return Action .ADD_ACCENT , Accent .TIDLE
339
+ return _Action .ADD_ACCENT , Accent .TIDLE
329
340
if trans [0 ] == "." :
330
- return Action .ADD_ACCENT , Accent .DOT
341
+ return _Action .ADD_ACCENT , Accent .DOT
331
342
# if trans[0] == "_":
332
- # return Action .ADD_ACCENT, Accent.NONE
343
+ # return _Action .ADD_ACCENT, Accent.NONE
333
344
334
345
335
- def transform (comps , trans ):
346
+ def _transform (comps , trans ):
336
347
"""
337
348
Transform the given string with transform type trans
338
349
"""
339
- logging .debug ("== In transform (%s, %s) ==" , comps , trans )
350
+ logging .debug ("== In _transform (%s, %s) ==" , comps , trans )
340
351
components = list (comps )
341
352
342
- action , parameter = get_action (trans )
343
- if action == Action .ADD_MARK and \
353
+ action , parameter = _get_action (trans )
354
+ if action == _Action .ADD_MARK and \
344
355
components [2 ] == "" and \
345
356
mark .strip (components [1 ]).lower () in ['oe' , 'oa' ] and trans == "o^" :
346
- action , parameter = Action .ADD_CHAR , trans [0 ]
357
+ action , parameter = _Action .ADD_CHAR , trans [0 ]
347
358
348
- if action == Action .ADD_ACCENT :
359
+ if action == _Action .ADD_ACCENT :
349
360
logging .debug ("add_accent(%s, %s)" , components , parameter )
350
361
components = accent .add_accent (components , parameter )
351
- elif action == Action .ADD_MARK and mark .is_valid_mark (components , trans ):
362
+ elif action == _Action .ADD_MARK and mark .is_valid_mark (components , trans ):
352
363
logging .debug ("add_mark(%s, %s)" , components , parameter )
353
364
components = mark .add_mark (components , parameter )
354
365
@@ -367,7 +378,7 @@ def transform(comps, trans):
367
378
components [1 ] = ("u" , "U" )[components [1 ][0 ].isupper ()] + components [1 ][1 ]
368
379
components = accent .add_accent (components , ac )
369
380
370
- elif action == Action .ADD_CHAR :
381
+ elif action == _Action .ADD_CHAR :
371
382
if trans [0 ] == "<" :
372
383
if not components [2 ]:
373
384
# Only allow ư, ơ or ươ sitting alone in the middle part
@@ -388,10 +399,10 @@ def transform(comps, trans):
388
399
components [1 ] = ('ư' , 'Ư' )[components [1 ][0 ].isupper ()] + \
389
400
('ơ' , 'Ơ' )[components [1 ][1 ].isupper ()] + components [1 ][2 :]
390
401
components = accent .add_accent (components , ac )
391
- elif action == Action .UNDO :
392
- components = reverse (components , trans [1 :])
402
+ elif action == _Action .UNDO :
403
+ components = _reverse (components , trans [1 :])
393
404
394
- if action == Action .ADD_MARK or (action == Action .ADD_CHAR and parameter .isalpha ()):
405
+ if action == _Action .ADD_MARK or (action == _Action .ADD_CHAR and parameter .isalpha ()):
395
406
# If there is any accent, remove and reapply it
396
407
# because it is likely to be misplaced in previous transformations
397
408
ac = accent .get_accent_string (components [1 ])
@@ -404,28 +415,28 @@ def transform(comps, trans):
404
415
return components
405
416
406
417
407
- def reverse (components , trans ):
418
+ def _reverse (components , trans ):
408
419
"""
409
420
Reverse the effect of transformation 'trans' on 'components'
410
421
If the transformation does not affect the components, return the original
411
422
string.
412
423
"""
413
424
414
- action , parameter = get_action (trans )
425
+ action , parameter = _get_action (trans )
415
426
comps = list (components )
416
427
string = utils .join (comps )
417
428
418
- if action == Action .ADD_CHAR and string [- 1 ].lower () == parameter .lower ():
429
+ if action == _Action .ADD_CHAR and string [- 1 ].lower () == parameter .lower ():
419
430
if comps [2 ]:
420
431
i = 2
421
432
elif comps [1 ]:
422
433
i = 1
423
434
else :
424
435
i = 0
425
436
comps [i ] = comps [i ][:- 1 ]
426
- elif action == Action .ADD_ACCENT :
437
+ elif action == _Action .ADD_ACCENT :
427
438
comps = accent .add_accent (comps , Accent .NONE )
428
- elif action == Action .ADD_MARK :
439
+ elif action == _Action .ADD_MARK :
429
440
if parameter == Mark .BAR :
430
441
comps [0 ] = comps [0 ][:- 1 ] + \
431
442
mark .add_mark_char (comps [0 ][- 1 :], Mark .NONE )
@@ -436,24 +447,24 @@ def reverse(components, trans):
436
447
return comps
437
448
438
449
439
- def can_undo (comps , trans_list ):
450
+ def _can_undo (comps , trans_list ):
440
451
"""
441
452
Return whether a components can be undone with one of the transformation in
442
453
trans_list.
443
454
"""
444
455
comps = list (comps )
445
456
accent_list = list (map (accent .get_accent_char , comps [1 ]))
446
457
mark_list = list (map (mark .get_mark_char , utils .join (comps )))
447
- action_list = list (map (lambda x : get_action (x ), trans_list ))
458
+ action_list = list (map (lambda x : _get_action (x ), trans_list ))
448
459
449
460
def atomic_check (action ):
450
461
"""
451
462
Check if the `action` created one of the marks, accents, or characters
452
463
in `comps`.
453
464
"""
454
- return (action [0 ] == Action .ADD_ACCENT and action [1 ] in accent_list ) \
455
- or (action [0 ] == Action .ADD_MARK and action [1 ] in mark_list ) \
456
- or (action [0 ] == Action .ADD_CHAR and action [1 ] == \
465
+ return (action [0 ] == _Action .ADD_ACCENT and action [1 ] in accent_list ) \
466
+ or (action [0 ] == _Action .ADD_MARK and action [1 ] in mark_list ) \
467
+ or (action [0 ] == _Action .ADD_CHAR and action [1 ] == \
457
468
accent .remove_accent_char (comps [1 ][- 1 ])) # ơ, ư
458
469
459
470
return any (map (atomic_check , action_list ))
0 commit comments