summaryrefslogtreecommitdiffstats
path: root/scripts/genpinyintable.py
blob: d93b81c2e6c47e1f2a2be69636b1a01597e5320f (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
# vim:set et sts=4 sw=4:
#
# libzhuyin - Library to deal with zhuyin.
#
# Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import operator
from bopomofo import BOPOMOFO_HANYU_PINYIN_MAP, BOPOMOFO_LUOMA_PINYIN_MAP, BOPOMOFO_SECOND_BOPOMOFO_MAP
from pinyintable import *
from chewingkey import gen_table_index


content_table = []
hanyu_pinyin_index = []
luoma_pinyin_index = []
bopomofo_index = []
second_bopomofo_index = []


#pinyin table
def filter_pinyin_list():
    for (pinyin, bopomofo, flags, chewing) in gen_pinyin_list():
        (luoma, second) = ("" , "")

        if bopomofo in BOPOMOFO_LUOMA_PINYIN_MAP:
            luoma = BOPOMOFO_LUOMA_PINYIN_MAP[bopomofo]
            flags.append("IS_LUOMA_PINYIN")

        if bopomofo in BOPOMOFO_SECOND_BOPOMOFO_MAP:
            second = BOPOMOFO_SECOND_BOPOMOFO_MAP[bopomofo]
            flags.append("IS_SECOND_BOPOMOFO")

        flags = '|'.join(flags)
        chewing = "ChewingKey({0})".format(', '.join(chewing))
        #correct = correct.replace("v", "ü")

        content_table.append((pinyin, bopomofo, luoma, second, chewing))

        if "IS_HANYU_PINYIN" in flags:
            hanyu_pinyin_index.append((pinyin, flags))
        if "IS_LUOMA_PINYIN" in flags:
            luoma_pinyin_index.append((luoma, flags))
        if "IS_BOPOMOFO" in flags:
            bopomofo_index.append((bopomofo, flags))
        if "IS_SECOND_BOPOMOFO" in flags:
            second_bopomofo_index.append((second, flags))


def sort_all():
    global content_table, hanyu_pinyin_index, luoma_pinyin_index
    global bopomofo_index, second_bopomofo_index

    #remove duplicates
    content_table = list(set(content_table))
    hanyu_pinyin_index = list(set(hanyu_pinyin_index))
    luoma_pinyin_index = list(set(luoma_pinyin_index))
    bopomofo_index = list(set(bopomofo_index))
    second_bopomofo_index = list(set(second_bopomofo_index))

    #define sort function
    sortfunc = operator.itemgetter(0)
    #begin sort
    content_table = sorted(content_table, key=sortfunc)
    #prepend zero item to reserve the invalid item
    content_table.insert(0, ("", "", "", "", "ChewingKey()"))
    #sort index
    hanyu_pinyin_index = sorted(hanyu_pinyin_index, key=sortfunc)
    luoma_pinyin_index = sorted(luoma_pinyin_index, key=sortfunc)
    bopomofo_index = sorted(bopomofo_index, key=sortfunc)
    second_bopomofo_index = sorted(second_bopomofo_index, key=sortfunc)

'''
def get_sheng_yun(pinyin):
    if pinyin == None:
        return None, None
    if pinyin == "":
        return "", ""
    if pinyin == "ng":
        return "", "ng"
    for i in range(2, 0, -1):
        s = pinyin[:i]
        if s in shengmu_list:
            return s, pinyin[i:]
    return "", pinyin
'''

def gen_content_table():
    entries = []
    for ((pinyin, bopomofo, luoma, second, chewing)) in content_table:
        entry = '{{"{0}", "{1}", "{2}", "{3}" ,{4}}}'.format(pinyin, bopomofo, luoma, second, chewing)
        entries.append(entry)
    return ',\n'.join(entries)


def gen_hanyu_pinyin_index():
    entries = []
    for (pinyin, flags) in hanyu_pinyin_index:
        index = [x[0] for x in content_table].index(pinyin)
        entry = '{{"{0}", {1}, {2}}}'.format(pinyin, flags, index)
        entries.append(entry)
    return ',\n'.join(entries)

def gen_luoma_pinyin_index():
    entries = []
    for (pinyin, flags) in luoma_pinyin_index:
        index = [x[2] for x in content_table].index(pinyin)
        entry = '{{"{0}", {1}, {2}}}'.format(pinyin, flags, index)
        entries.append(entry)
    return ',\n'.join(entries)

def gen_bopomofo_index():
    entries = []
    for (bopomofo, flags) in bopomofo_index:
        pinyin = BOPOMOFO_HANYU_PINYIN_MAP[bopomofo]
        index = [x[0] for x in content_table].index(pinyin)
        entry = '{{"{0}", {1}, {2}}}'.format(bopomofo, flags, index)
        entries.append(entry)
    return ',\n'.join(entries)

def gen_second_bopomofo_index():
    entries = []
    for (bopomofo, flags) in second_bopomofo_index:
        index = [x[3] for x in content_table].index(bopomofo)
        entry = '{{"{0}", {1}, {2}}}'.format(bopomofo, flags, index)
        entries.append(entry)
    return ',\n'.join(entries)

def gen_chewing_key_table():
    return gen_table_index(content_table)


#init code
filter_pinyin_list()
sort_all()


### main function ###
if __name__ == "__main__":
    #s = gen_content_table() + gen_hanyu_pinyin_index() + gen_bopomofo_index()
    s = gen_content_table() + gen_luoma_pinyin_index() + gen_second_bopomofo_index()
    #s = gen_chewing_key_table()
    print(s)