Skip to content

Commit f03892b

Browse files
committed
Merge pull request #3 from BoGoEngine/new-api
Refactor bogo.py. New API.
2 parents cfa1dc6 + 7757430 commit f03892b

4 files changed

Lines changed: 164 additions & 164 deletions

File tree

README.md

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,34 @@ Usage
1818
('cán', 'casn')
1919
```
2020

21-
`process_key()` is intended to be called iteratively on each keystroke
22-
generated by the user with the following arguments:
21+
`process_key()` is intended to be called successively on each keystroke with
22+
the following arguments.
2323

24-
- `string`: the previously processed Vietnamese string, or ""
25-
- `key`: the keystroke
26-
- `fallback_sequence`: the previous key strokes, or ""
27-
- an optional `config`: a dictionary-like object (see below)
24+
- `string`: The previously processed string or "".
25+
- `key`: The keystroke.
26+
- `fallback_sequence`: The previous keystrokes.
27+
- `input_method_definition` (optional): A dictionary listing
28+
transformation rules. Defaults to the value returned by `get_telex_definition()`.
29+
- `skip_non_vietnamese` (optional): Whether to skip results that
30+
doesn't seem like Vietnamese. Defaults to True.
2831

29-
This function returns a tuple, the first item of which is the processed
30-
Vietnamese string, the second item is the next fallback sequence. The two
31-
items are to be fed back into the next call of `process_key()` as `string`
32-
and `fallback_sequence`.
32+
It returns a tuple. The first item of which is the processed
33+
Vietnamese string, the second item is the next fallback sequence.
34+
The two items are to be fed back into the next call of process_key()
35+
as `string` and `fallback_sequence`. If `skip_non_vietnamese` is
36+
True and the resulting string doesn't look like Vietnamese,
37+
both items contain the `fallback_sequence`.
3338

34-
`process_key()`'s behavior is controlled by the `config` argument, which takes
35-
the value of `bogo.get_default_config()` if none is specified:
39+
Note that when a key is an undo key, it won't get appended to
40+
`fallback_sequence`.
3641

3742
```python
38-
{
39-
"input-method": "telex",
40-
"skip-non-vietnamese": True,
41-
"default-input-methods": {
42-
"simple-telex": {
43-
"a": "a^",
44-
"o": "o^",
45-
"e": "e^",
46-
"w": ["u*", "o*", "a+"],
47-
"d": "d-",
48-
"f": "\\",
49-
"s": "/",
50-
"r": "?",
51-
"x": "~",
52-
"j": "."
53-
},
54-
"telex": {
55-
"a": "a^",
56-
"o": "o^",
57-
"e": "e^",
58-
"w": ["u*", "o*", "a+", ""],
59-
"d": "d-",
60-
"f": "\\",
61-
"s": "/",
62-
"r": "?",
63-
"x": "~",
64-
"j": ".",
65-
"]": "",
66-
"[": "",
67-
"}": "",
68-
"{": ""
69-
},
70-
"vni": {
71-
"6": ["a^", "o^", "e^"],
72-
"7": ["u*", "o*"],
73-
"8": "a+",
74-
"9": "d-",
75-
"2": "\\",
76-
"1": "/",
77-
"3": "?",
78-
"4": "~",
79-
"5": "."
80-
}
81-
}
82-
}
83-
43+
>>> process_key('â', 'a', 'aa')
44+
(aa, aa)
8445
```
8546

86-
`input-method` is one of the `default-input-methods`, each of which is a
87-
dictionary of keystrokes and their effects.
47+
`input_method_definition` is a dictionary that maps keystrokes to
48+
their effect string. The effects can be one of the following:
8849

8950
- 'a^': a with circumflex (â), only affect an existing 'a family'
9051
- 'a+': a with breve (ă), only affect an existing 'a family'
@@ -100,8 +61,13 @@ dictionary of keystrokes and their effects.
10061
- '<ư': append ư
10162
- '<ơ': append ơ
10263

103-
If `skip-non-vietnamese` is True and `process_key()` deems a key sequence to
104-
yield non-Vietnamese result then it will return the `fallback_sequence`.
64+
A keystroke entry can have multiple effects, in which case the
65+
dictionary entry's value should be a list of the possible
66+
effect strings. Although you should try to avoid this if
67+
you are defining a custom input method rule.
68+
69+
We have already defined input method definitions for TELEX and VNI with the
70+
`get_telex_definition()` and `get_vni_definition()` functions.
10571

10672
More help is available as docstring for each module and function.
10773

bogo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from bogo.bogo import process_key, get_default_config
1+
from bogo.bogo import process_key

0 commit comments

Comments
 (0)