Debian Codegraph @

tables/wubi-haifeng/remove_cjk_compatibility_ideographs.py

raw
1 #!/usr/bin/python3
2 #
3 # Copyright (c) 2021 Mike FABIAN <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>
17 '''
18 Improves the wubi-haifeng86 and the wubi-jidian86.txt tables to resolve:
19
20 https://github.com/kaio/ibus-table/issues/76
21
22 by removing all lines containing CJK COMPATIBILITY IDEOGRAPHs.
23 '''
24
25 from typing import Any
26 from typing import List
27 import re
28 import unicodedata
29 import logging
30
31 def parse_args() -> Any:
32 '''Parse the command line arguments'''
33 import argparse
34 parser = argparse.ArgumentParser(
35 description=(
36 'Remove lines containing CJK COMPATIBILITY IDEOGRAPHs '
37 'from a file'))
38 parser.add_argument('-i', '--inputfilename',
39 nargs='?',
40 type=str,
41 default='wubi-haifeng86.UTF-8',
42 help='input file, default is %(default)s')
43 parser.add_argument('-o', '--outputfilename',
44 nargs='?',
45 type=str,
46 default='wubi-haifeng86.UTF-8.new',
47 help='output file, default is %(default)s')
48 parser.add_argument('-d', '--debug',
49 action='store_true',
50 help='print debugging output')
51 return parser.parse_args()
52
53 def remove_cjk_compatibility_ideographs(inputfilename: str, outputfilename: str) -> None:
54 '''
55 Read the file and write a version without the lines
56 containing CJK COMPATIBILITY IDEOGRAPHs
57 '''
58 output_lines: List[str] = []
59 with open(inputfilename, 'r') as inputfile:
60 logging.info("input file=%s", inputfile)
61 for line in inputfile:
62 skip_line = False
63 for char in line:
64 unicode_name = unicodedata.name(char, '')
65 if unicode_name.startswith('CJK COMPATIBILITY IDEOGRAPH'):
66 skip_line = True
67 if not skip_line:
68 output_lines.append(line)
69 with open(outputfilename, 'w') as outputfile:
70 logging.info("output file=%s", outputfile)
71 for line in output_lines:
72 outputfile.write('%s' % line)
73
74 def main() -> None:
75 '''Main program'''
76 args = parse_args()
77 log_level = logging.INFO
78 if args.debug:
79 log_level = logging.DEBUG
80 logging.basicConfig(format="%(levelname)s: %(message)s", level=log_level)
81 remove_cjk_compatibility_ideographs(args.inputfilename, args.outputfilename)
82
83 if __name__ == '__main__':
84 main()