add const modifiers for chewing table
[platform/upstream/libpinyin.git] / scripts / genpinyintable.py
1 # -*- coding: utf-8 -*-
2 # vim:set et sts=4 sw=4:
3 #
4 # libpinyin - Library to deal with pinyin.
5 #
6 # Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 import operator
23 import bopomofo
24 from pinyintable import *
25 from chewingkey import gen_table_index
26
27
28 content_table = []
29 pinyin_index = []
30 bopomofo_index = []
31
32 #pinyin table
33 def filter_pinyin_list():
34     for (correct, wrong, bopomofo, flags, chewing) in gen_pinyin_list():
35         flags = '|'.join(flags)
36         chewing = "ChewingKey({0})".format(', '.join(chewing))
37         #correct = correct.replace("v", "ΓΌ")
38         content_table.append((correct, bopomofo, chewing))
39         if "IS_PINYIN" in flags:
40             pinyin_index.append((wrong, flags, correct))
41         if "IS_CHEWING" in flags:
42             bopomofo_index.append((bopomofo, flags))
43
44
45 def sort_all():
46     global content_table, pinyin_index, bopomofo_index
47     #remove duplicates
48     content_table = list(set(content_table))
49     pinyin_index = list(set(pinyin_index))
50     bopomofo_index = list(set(bopomofo_index))
51     #define sort function
52     sortfunc = operator.itemgetter(0)
53     #begin sort
54     content_table = sorted(content_table, key=sortfunc)
55     #prepend zero item to reserve the invalid item
56     content_table.insert(0, ("", "", "ChewingKey()"))
57     #sort index
58     pinyin_index = sorted(pinyin_index, key=sortfunc)
59     bopomofo_index = sorted(bopomofo_index, key=sortfunc)
60
61 def get_sheng_yun(pinyin):
62     if pinyin == None:
63         return None, None
64     if pinyin == "":
65         return "", ""
66     if pinyin == "ng":
67         return "", "ng"
68     for i in range(2, 0, -1):
69         s = pinyin[:i]
70         if s in shengmu_list:
71             return s, pinyin[i:]
72     return "", pinyin
73
74 def gen_content_table():
75     entries = []
76     for ((correct, bopomofo, chewing)) in content_table:
77         (shengmu, yunmu) = get_sheng_yun(correct)
78         entry = '{{"{0}", "{1}", "{2}", "{3}", {4}}}'.format(correct, shengmu, yunmu, bopomofo, chewing)
79         entries.append(entry)
80     return ',\n'.join(entries)
81
82
83 def gen_pinyin_index():
84     entries = []
85     for (wrong, flags, correct) in pinyin_index:
86         index = [x[0] for x in content_table].index(correct)
87         entry = '{{"{0}", {1}, {2}}}'.format(wrong, flags, index)
88         entries.append(entry)
89     return ',\n'.join(entries)
90
91
92 def gen_bopomofo_index():
93     entries = []
94     for (bopomofo_str, flags) in bopomofo_index:
95         pinyin_str = bopomofo.BOPOMOFO_PINYIN_MAP[bopomofo_str]
96         index = [x[0] for x in content_table].index(pinyin_str)
97         entry = '{{"{0}", {1}, {2}}}'.format(bopomofo_str, flags, index)
98         entries.append(entry)
99     return ',\n'.join(entries)
100
101
102 def gen_chewing_key_table():
103     return gen_table_index(content_table)
104
105
106 #init code
107 filter_pinyin_list()
108 sort_all()
109
110
111 ### main function ###
112 if __name__ == "__main__":
113     #s = gen_content_table() + gen_pinyin_index() + gen_bopomofo_index()
114     s = gen_chewing_key_table()
115     print(s)