summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2015-03-11 17:42:15 +0800
committerPeng Wu <alexepico@gmail.com>2015-03-11 17:42:15 +0800
commitb492c0027fc917d5e05027e7d5c1acbd44b92717 (patch)
tree4a79da718500e11af2c69abe96b0660a3ad7d716
parent3b2f55b3c34a6ae0a9a438fdb69d2bd0b15e5c6f (diff)
downloadlibzhuyin-b492c0027fc917d5e05027e7d5c1acbd44b92717.tar.gz
libzhuyin-b492c0027fc917d5e05027e7d5c1acbd44b92717.tar.xz
libzhuyin-b492c0027fc917d5e05027e7d5c1acbd44b92717.zip
update scripts for pinyin tables
-rw-r--r--scripts/genpinyintable.py24
-rw-r--r--scripts/utils.py28
2 files changed, 45 insertions, 7 deletions
diff --git a/scripts/genpinyintable.py b/scripts/genpinyintable.py
index 1594a91..63603d5 100644
--- a/scripts/genpinyintable.py
+++ b/scripts/genpinyintable.py
@@ -25,12 +25,14 @@ from bopomofo import BOPOMOFO_HANYU_PINYIN_MAP, BOPOMOFO_LUOMA_PINYIN_MAP, BOPOM
from pinyintable import *
from correct import *
from chewingkey import gen_table_index
+from utils import shuffle_all
content_table = []
hanyu_pinyin_index = []
luoma_pinyin_index = []
bopomofo_index = []
+shuffle_bopomofo_index = []
secondary_bopomofo_index = []
hsu_bopomofo_index = []
eten26_bopomofo_index = []
@@ -65,8 +67,8 @@ def filter_pinyin_list():
def populate_more_bopomofo_index():
for (bopomofo, flags) in bopomofo_index:
- # populate hsu bopomofo index
correct = bopomofo
+ # populate hsu bopomofo index
matches = itertools.chain(handle_rules(bopomofo, hsu_correct),
handle_special_rules(bopomofo, hsu_correct_special))
for wrong in matches:
@@ -89,9 +91,19 @@ def populate_more_bopomofo_index():
if bopomofo not in [x[0] for x in eten26_bopomofo_index]:
eten26_bopomofo_index.append((bopomofo, flags, correct))
+ # populate shuffled bopomofo index
+ for (bopomofo, flags) in bopomofo_index:
+ correct = bopomofo
+ shuffle_bopomofo_index.append((bopomofo, flags, correct))
+ newflags = '|'.join((flags, 'SHUFFLE_CORRECT'))
+ for shuffle in shuffle_all(bopomofo):
+ assert shuffle not in [x[0] for x in shuffle_bopomofo_index]
+ shuffle_bopomofo_index.append((shuffle, newflags, correct))
+
+
def sort_all():
global content_table, hanyu_pinyin_index, luoma_pinyin_index
- global bopomofo_index, secondary_bopomofo_index
+ global bopomofo_index, shuffle_bopomofo_index, secondary_bopomofo_index
global hsu_bopomofo_index, eten26_bopomofo_index
#remove duplicates
@@ -99,6 +111,7 @@ def sort_all():
hanyu_pinyin_index = list(set(hanyu_pinyin_index))
luoma_pinyin_index = list(set(luoma_pinyin_index))
bopomofo_index = list(set(bopomofo_index))
+ shuffle_bopomofo_index = list(set(shuffle_bopomofo_index))
secondary_bopomofo_index = list(set(secondary_bopomofo_index))
hsu_bopomofo_index = list(set(hsu_bopomofo_index))
eten26_bopomofo_index = list(set(eten26_bopomofo_index))
@@ -113,6 +126,7 @@ def sort_all():
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)
+ shuffle_bopomofo_index = sorted(shuffle_bopomofo_index, key=sortfunc)
secondary_bopomofo_index = sorted(secondary_bopomofo_index, key=sortfunc)
hsu_bopomofo_index = sorted(hsu_bopomofo_index, key=sortfunc)
eten26_bopomofo_index = sorted(eten26_bopomofo_index, key=sortfunc)
@@ -158,10 +172,10 @@ def gen_luoma_pinyin_index():
def gen_bopomofo_index():
entries = []
- for (bopomofo, flags) in bopomofo_index:
- pinyin = BOPOMOFO_HANYU_PINYIN_MAP[bopomofo]
+ for (shuffle, flags, correct) in shuffle_bopomofo_index:
+ pinyin = BOPOMOFO_HANYU_PINYIN_MAP[correct]
index = [x[0] for x in content_table].index(pinyin)
- entry = '{{"{0}", {1}, {2}}}'.format(bopomofo, flags, index)
+ entry = '{{"{0}", {1}, {2}}}'.format(shuffle, flags, index)
entries.append(entry)
return ',\n'.join(entries)
diff --git a/scripts/utils.py b/scripts/utils.py
index bd975a2..f3e46c5 100644
--- a/scripts/utils.py
+++ b/scripts/utils.py
@@ -17,7 +17,8 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
import os
@@ -26,12 +27,13 @@ header = '''/* This file is generated by python scripts. Don't edit this file di
*/
'''
+
def expand_file(filename, get_table_content):
infile = open(filename, "r")
print(header)
for line in infile.readlines():
line = line.rstrip(os.linesep)
- if len(line) < 3 :
+ if len(line) < 3:
print(line)
continue
if line[0] == '@' and line[-1] == '@':
@@ -39,3 +41,25 @@ def expand_file(filename, get_table_content):
print(get_table_content(tablename))
else:
print(line)
+
+
+def shuffle_all(instr):
+ for output in shuffle_recur(instr):
+ if output == instr:
+ continue
+ yield output
+
+
+def shuffle_recur(instr):
+ if len(instr) == 1:
+ yield instr
+ else:
+ for i, ch in enumerate(instr):
+ recur = instr[:i] + instr[i+1:]
+ for s in shuffle_recur(recur):
+ yield ch + s
+
+
+if __name__ == "__main__":
+ for s in shuffle_all("abc"):
+ print(s)