1 # -*- coding: utf-8 -*-
2 # vim:set et sts=4 sw=4:
4 # libpinyin - Library to deal with pinyin.
6 # Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
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)
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.
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.
24 from pinyintable import *
25 from chewingkey import gen_table_index
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))
46 global content_table, pinyin_index, bopomofo_index
48 content_table = list(set(content_table))
49 pinyin_index = list(set(pinyin_index))
50 bopomofo_index = list(set(bopomofo_index))
52 sortfunc = operator.itemgetter(0)
54 content_table = sorted(content_table, key=sortfunc)
55 #prepend zero item to reserve the invalid item
56 content_table.insert(0, ("", "", "ChewingKey()"))
58 pinyin_index = sorted(pinyin_index, key=sortfunc)
59 bopomofo_index = sorted(bopomofo_index, key=sortfunc)
61 def get_sheng_yun(pinyin):
68 for i in range(2, 0, -1):
74 def gen_content_table():
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)
80 return ',\n'.join(entries)
83 def gen_pinyin_index():
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)
89 return ',\n'.join(entries)
92 def gen_bopomofo_index():
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)
99 return ',\n'.join(entries)
102 def gen_chewing_key_table():
103 return gen_table_index(content_table)
111 ### main function ###
112 if __name__ == "__main__":
113 #s = gen_content_table() + gen_pinyin_index() + gen_bopomofo_index()
114 s = gen_chewing_key_table()