File: create_keystroke_from_text.py

package info (click to toggle)
libchewing 0.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,036 kB
  • sloc: ansic: 7,031; python: 190; sh: 127; makefile: 44
file content (53 lines) | stat: -rwxr-xr-x 1,441 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
import argparse
import os
import sys


def get_args():
    parser = argparse.ArgumentParser(
        description='Create keystroke from text.')
    parser.add_argument('input', metavar='input', type=str, nargs=1,
        help='Input Chinese text file')
    parser.add_argument('output', metavar='output', type=str, nargs=1,
        help='Output keystroke file')
    parser.add_argument('phone', metavar='phone', type=str, nargs='?',
        default=os.path.join(os.path.dirname(sys.argv[0]), '..', 'data', 'phone.cin'),
        help='phone.cin')
    return parser.parse_args()


def read_phone(phone):
    phone_table = {}
    with open(phone) as f:
        for l in f:
            if l.startswith('%chardef  begin'):
                break
        for l in f:
            if l.startswith('%chardef  end'):
                break
            item = l.split()
            phone_table[item[1]] = item[0]
    return phone_table


def main():
    args = get_args()
    phone_table = read_phone(args.phone)

    in_file = args.input[0]
    out_file = args.output[0]
    word_count = 0

    with open(in_file) as in_, open(out_file, "w") as out_:
        for l in in_:
            for c in l:
                if c in phone_table:
                    out_.write(phone_table[c])
                    word_count += 1
        out_.write('\n')
    print('word count = {}'.format(word_count))


if __name__ == '__main__':
    main()