From 149eb1967a2b7da86e934c79fa6d62cb673bf885 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 6 Jun 2012 11:00:07 +0800 Subject: [PATCH] clean scripts --- scripts/id.py | 1 - scripts/pydict.py | 407 ------------------------------------------------------ scripts/pyutil.py | 148 -------------------- 3 files changed, 556 deletions(-) delete mode 100644 scripts/id.py delete mode 100644 scripts/pydict.py delete mode 100644 scripts/pyutil.py diff --git a/scripts/id.py b/scripts/id.py deleted file mode 100644 index 201a3cd..0000000 --- a/scripts/id.py +++ /dev/null @@ -1 +0,0 @@ -pinyin_id = {'': 0, 'ch': 3, 'zh': 23, 'ai': 25, 'uan': 50, 'iu': 43, 'ong': 45, 'ao': 28, 'an': 26, 'uai': 49, 'ang': 27, 'iong': 42, 'in': 40, 'ia': 35, 'ei': 30, 'ing': 41, 'ie': 39, 'er': 33, 'iao': 38, 'ian': 36, 'eng': 32, 'iang': 37, 'uo': 55, 'r': 15, 'en': 31, 'ui': 53, 'un': 54, 'ue': 52, 'uang': 51, 'a': 24, 'c': 2, 'b': 1, 'e': 29, 'd': 4, 'g': 6, 'f': 5, 'i': 34, 'h': 7, 'k': 9, 'j': 8, 'm': 11, 'l': 10, 'o': 44, 'n': 12, 'q': 14, 'p': 13, 's': 16, 'sh': 17, 'u': 47, 't': 18, 'w': 19, 'v': 56, 'y': 21, 'x': 20, 'ou': 46, 'z': 22, 'ua': 48} diff --git a/scripts/pydict.py b/scripts/pydict.py deleted file mode 100644 index 456140d..0000000 --- a/scripts/pydict.py +++ /dev/null @@ -1,407 +0,0 @@ -# -*- coding: utf-8 -*- -# vim:set et sts=4 sw=4: -# -# ibus-pinyin - The PinYin engine for IBus -# -# Copyright (c) 2007-2008 Peng Huang -# -# 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., 675 Mass Ave, Cambridge, MA 02139, USA. - -N_ = lambda x : x -PINYIN_DICT = { - "a" : 1, "ai" : 2, "an" : 3, "ang" : 4, "ao" : 5, - "ba" : 6, "bai" : 7, "ban" : 8, "bang" : 9, "bao" : 10, - "bei" : 11, "ben" : 12, "beng" : 13, "bi" : 14, "bian" : 15, - "biao" : 16, "bie" : 17, "bin" : 18, "bing" : 19, "bo" : 20, - "bu" : 21, "ca" : 22, "cai" : 23, "can" : 24, "cang" : 25, - "cao" : 26, "ce" : 27, "cen" : 28, "ceng" : 29, "ci" : 30, - "cong" : 31, "cou" : 32, "cu" : 33, "cuan" : 34, "cui" : 35, - "cun" : 36, "cuo" : 37, "cha" : 38, "chai" : 39, "chan" : 40, - "chang" : 41, "chao" : 42, "che" : 43, "chen" : 44, "cheng" : 45, - "chi" : 46, "chong" : 47, "chou" : 48, "chu" : 49, "chuai" : 50, - "chuan" : 51, "chuang" : 52, "chui" : 53, "chun" : 54, "chuo" : 55, - "da" : 56, "dai" : 57, "dan" : 58, "dang" : 59, "dao" : 60, - "de" : 61, "dei" : 62, - # "den" : 63, - "deng" : 64, "di" : 65, - "dia" : 66, "dian" : 67, "diao" : 68, "die" : 69, "ding" : 70, - "diu" : 71, "dong" : 72, "dou" : 73, "du" : 74, "duan" : 75, - "dui" : 76, "dun" : 77, "duo" : 78, "e" : 79, "ei" : 80, - "en" : 81, "er" : 82, "fa" : 83, "fan" : 84, "fang" : 85, - "fei" : 86, "fen" : 87, "feng" : 88, "fo" : 89, "fou" : 90, - "fu" : 91, "ga" : 92, "gai" : 93, "gan" : 94, "gang" : 95, - "gao" : 96, "ge" : 97, "gei" : 98, "gen" : 99, "geng" : 100, - "gong" : 101, "gou" : 102, "gu" : 103, "gua" : 104, "guai" : 105, - "guan" : 106, "guang" : 107, "gui" : 108, "gun" : 109, "guo" : 110, - "ha" : 111, "hai" : 112, "han" : 113, "hang" : 114, "hao" : 115, - "he" : 116, "hei" : 117, "hen" : 118, "heng" : 119, "hong" : 120, - "hou" : 121, "hu" : 122, "hua" : 123, "huai" : 124, "huan" : 125, - "huang" : 126, "hui" : 127, "hun" : 128, "huo" : 129, "ji" : 130, - "jia" : 131, "jian" : 132, "jiang" : 133, "jiao" : 134, "jie" : 135, - "jin" : 136, "jing" : 137, "jiong" : 138, "jiu" : 139, "ju" : 140, - "juan" : 141, "jue" : 142, "jun" : 143, "ka" : 144, "kai" : 145, - "kan" : 146, "kang" : 147, "kao" : 148, "ke" : 149, - # "kei" : 150, - "ken" : 151, "keng" : 152, "kong" : 153, "kou" : 154, "ku" : 155, - "kua" : 156, "kuai" : 157, "kuan" : 158, "kuang" : 159, "kui" : 160, - "kun" : 161, "kuo" : 162, "la" : 163, "lai" : 164, "lan" : 165, - "lang" : 166, "lao" : 167, "le" : 168, "lei" : 169, "leng" : 170, - "li" : 171, "lia" : 172, "lian" : 173, "liang" : 174, "liao" : 175, - "lie" : 176, "lin" : 177, "ling" : 178, "liu" : 179, - "lo" : 180, - "long" : 181, "lou" : 182, "lu" : 183, "luan" : 184, - # "lue" : 185, - "lun" : 186, "luo" : 187, "lv" : 188, "lve" : 189, - "ma" : 190, - "mai" : 191, "man" : 192, "mang" : 193, "mao" : 194, "me" : 195, - "mei" : 196, "men" : 197, "meng" : 198, "mi" : 199, "mian" : 200, - "miao" : 201, "mie" : 202, "min" : 203, "ming" : 204, "miu" : 205, - "mo" : 206, "mou" : 207, "mu" : 208, "na" : 209, "nai" : 210, - "nan" : 211, "nang" : 212, "nao" : 213, "ne" : 214, "nei" : 215, - "nen" : 216, "neng" : 217, "ni" : 218, "nian" : 219, "niang" : 220, - "niao" : 221, "nie" : 222, "nin" : 223, "ning" : 224, "niu" : 225, - # "ng" : 226, - "nong" : 227, "nou" : 228, "nu" : 229, "nuan" : 230, - # "nue" : 231, - "nuo" : 232, "nv" : 233, "nve" : 234, - "o" : 235, - "ou" : 236, "pa" : 237, "pai" : 238, "pan" : 239, "pang" : 240, - "pao" : 241, "pei" : 242, "pen" : 243, "peng" : 244, "pi" : 245, - "pian" : 246, "piao" : 247, "pie" : 248, "pin" : 249, "ping" : 250, - "po" : 251, "pou" : 252, "pu" : 253, "qi" : 254, "qia" : 255, - "qian" : 256, "qiang" : 257, "qiao" : 258, "qie" : 259, "qin" : 260, - "qing" : 261, "qiong" : 262, "qiu" : 263, "qu" : 264, "quan" : 265, - "que" : 266, "qun" : 267, "ran" : 268, "rang" : 269, "rao" : 270, - "re" : 271, "ren" : 272, "reng" : 273, "ri" : 274, "rong" : 275, - "rou" : 276, "ru" : 277, "ruan" : 278, "rui" : 279, "run" : 280, - "ruo" : 281, "sa" : 282, "sai" : 283, "san" : 284, "sang" : 285, - "sao" : 286, "se" : 287, "sen" : 288, "seng" : 289, "si" : 290, - "song" : 291, "sou" : 292, "su" : 293, "suan" : 294, "sui" : 295, - "sun" : 296, "suo" : 297, "sha" : 298, "shai" : 299, "shan" : 300, - "shang" : 301, "shao" : 302, "she" : 303, "shei" : 304, "shen" : 305, - "sheng" : 306, "shi" : 307, "shou" : 308, "shu" : 309, "shua" : 310, - "shuai" : 311, "shuan" : 312, "shuang" : 313, "shui" : 314, "shun" : 315, - "shuo" : 316, "ta" : 317, "tai" : 318, "tan" : 319, "tang" : 320, - "tao" : 321, "te" : 322, - # "tei" : 323, - "teng" : 324, "ti" : 325, - "tian" : 326, "tiao" : 327, "tie" : 328, "ting" : 329, "tong" : 330, - "tou" : 331, "tu" : 332, "tuan" : 333, "tui" : 334, "tun" : 335, - "tuo" : 336, "wa" : 337, "wai" : 338, "wan" : 339, "wang" : 340, - "wei" : 341, "wen" : 342, "weng" : 343, "wo" : 344, "wu" : 345, - "xi" : 346, "xia" : 347, "xian" : 348, "xiang" : 349, "xiao" : 350, - "xie" : 351, "xin" : 352, "xing" : 353, "xiong" : 354, "xiu" : 355, - "xu" : 356, "xuan" : 357, "xue" : 358, "xun" : 359, "ya" : 360, - "yan" : 361, "yang" : 362, "yao" : 363, "ye" : 364, "yi" : 365, - "yin" : 366, "ying" : 367, "yo" : 368, "yong" : 369, "you" : 370, - "yu" : 371, "yuan" : 372, "yue" : 373, "yun" : 374, "za" : 375, - "zai" : 376, "zan" : 377, "zang" : 378, "zao" : 379, "ze" : 380, - "zei" : 381, "zen" : 382, "zeng" : 383, "zi" : 384, "zong" : 385, - "zou" : 386, "zu" : 387, "zuan" : 388, "zui" : 389, "zun" : 390, - "zuo" : 391, "zha" : 392, "zhai" : 393, "zhan" : 394, "zhang" : 395, - "zhao" : 396, "zhe" : 397, "zhen" : 398, "zheng" : 399, "zhi" : 400, - "zhong" : 401, "zhou" : 402, "zhu" : 403, "zhua" : 404, "zhuai" : 405, - "zhuan" : 406, "zhuang" : 407, "zhui" : 408, "zhun" : 409, "zhuo" : 410, - # some weird pinyins - #~ "eng" : 411, "chua" : 412, "fe" : 413, "fiao" : 414, "liong" : 415 -} - -PINYIN_LIST = PINYIN_DICT.keys () - -ID_PINYIN_DICT = {} -for pinyin, id in PINYIN_DICT.items (): - ID_PINYIN_DICT[id] = pinyin - -SHENGMU_DICT = { - "" : 0, "b" : 1, "p" : 2, "m" : 3, "f" : 4, "d" : 5, - "t" : 6, "n" : 7, "l" : 8, "g" : 9, "k" : 10, "h" : 11, - "j" : 12, "q" : 13, "x" : 14, "zh" : 15, "ch" : 16, "sh" : 17, - "r" : 18, "z" : 19, "c" : 20, "s" : 21, "y" : 22, "w" : 23 -} -SHENGMU_LIST = SHENGMU_DICT.keys () -#~ PINYIN_PARTIAL_LIST = [] -#~ for p in PINYIN_LIST: - #~ for i in range (2, len (p)): - #~ if not (p[:i] in PINYIN_LIST or p[:i] in SHENGMU_LIST or p[:i] in PINYIN_PARTIAL_LIST): - #~ PINYIN_PARTIAL_LIST.append (p[:i]) -#~ print PINYIN_PARTIAL_LIST -PINYIN_PARTIAL_LIST = [ -'ro', 'zo', 'zon', 'so', 'son', 'tua', 'zua', 'no', -'non', 'be', 'ho', 'cua', 'jio', 'jion', 'xua', 'ko', -'kon', 'we', 'go', 'tia', 'fi', 'fia', 'fe', 'din', 'ra', -'yon', 'do', 'don', 'ron', 'to', 'nia', 'qua', 'cho', -'chon', 'mia', 'den', 'sho', 'nua', 'ten', 'co', 'ton', -'pe', 'tin', 'sua', 'xio', 'xion', 'pia', 'bia', 'dua', -'con', 'hon', 'jua', 'yua', 'zho', 'zhon', 'rua', 'lua', -'lio', 'lion', 'qio', 'qion', 'lon', 'gon', 'len'] - -ID_SHENGMU_DICT = {} - - -for shengmu, id in SHENGMU_DICT.items (): - ID_SHENGMU_DICT[id] = shengmu - -MOHU_SHENGMU = { - "z" : ("z", "zh"), - "zh" : ("z", "zh"), - "c" : ("c", "ch"), - "ch" : ("c", "ch"), - "s" : ("s", "sh"), - "sh" : ("s", "sh"), - "l" : ("l", "n"), - "n" : ("l", "n") -} - -MOHU_YUNMU = { - "an" : ("an", "ang"), - "ang" : ("an", "ang"), - "en" : ("en", "eng"), - "eng" : ("en", "eng"), - "in" : ("in", "ing"), - "ing" : ("in", "ing") -} - -MSPY_SHUANGPIN_SHENGMU_DICT = { - "b" : "b", "c" : "c", "d" : "d", "f" : "f", "g" : "g", - "h" : "h", "i" : "ch","j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "u" : "sh","v" : "zh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -MSPY_SHUANGPIN_YUNMU_DICT = { - "a" : ("a",), - "b" : ("ou",), - "c" : ("iao",), - "d" : ("uang", "iang"), - "e" : ("e",), - "f" : ("en",), - "g" : ("eng", "ng"), - "h" : ("ang",), - "i" : ("i",), - "j" : ("an",), - "k" : ("ao",), - "l" : ("ai",), - "m" : ("ian",), - "n" : ("in",), - "o" : ("uo", "o"), - "p" : ("un",), - "q" : ("iu",), - "r" : ("uan", "er"), - "s" : ("ong", "iong"), - "t" : ("ue",), - "u" : ("u",), - "v" : ("ui","ue"), - "w" : ("ia","ua"), - "x" : ("ie",), - "y" : ("uai", "v"), - "z" : ("ei",), - ";" : ("ing",) -} - -ZRM_SHUANGPIN_SHENGMU_DICT = { - "b" : "b", "c" : "c", "d" : "d", "f" : "f", "g" : "g", - "h" : "h", "i" : "ch","j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "u" : "sh","v" : "zh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -ZRM_SHUANGPIN_YUNMU_DICT = { - "a" : ("a",), - "b" : ("ou",), - "c" : ("iao",), - "d" : ("uang", "iang"), - "e" : ("e",), - "f" : ("en",), - "g" : ("eng", "ng"), - "h" : ("ang",), - "i" : ("i",), - "j" : ("an",), - "k" : ("ao",), - "l" : ("ai",), - "m" : ("ian",), - "n" : ("in",), - "o" : ("uo", "o"), - "p" : ("un",), - "q" : ("iu",), - "r" : ("uan", "er"), - "s" : ("ong", "iong"), - "t" : ("ue",), - "u" : ("u",), - "v" : ("ui","v"), - "w" : ("ia","ua"), - "x" : ("ie",), - "y" : ("uai", "ing"), - "z" : ("ei",), -} - -ABC_SHUANGPIN_SHENGMU_DICT = { - "a" : "zh", "b" : "b", "c" : "c", "d" : "d", "e":"ch", "f" : "f", "g" : "g", - "h" : "h", "j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "v" : "sh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -ABC_SHUANGPIN_YUNMU_DICT = { - "a" : ("a",), - "b" : ("ou",), - "c" : ("in","uai"), - "d" : ("ia", "ua"), - "e" : ("e",), - "f" : ("en",), - "g" : ("eng", "ng"), - "h" : ("ang",), - "i" : ("i",), - "j" : ("an",), - "k" : ("ao",), - "l" : ("ai",), - "m" : ("ue","ui"), - "n" : ("un",), - "o" : ("uo", "o"), - "p" : ("uan",), - "q" : ("ei",), - "r" : ("er", "iu"), - "s" : ("ong", "iong"), - "t" : ("iang","uang"), - "u" : ("u",), - "v" : ("v","ue"), - "w" : ("ian",), - "x" : ("ie",), - "y" : ("ing",), - "z" : ("iao",), -} - -ZGPY_SHUANGPIN_SHENGMU_DICT = { - "a" : "ch", "b" : "b", "c" : "c", "d" : "d", "f" : "f", "g" : "g", - "h" : "h", "i" : "sh","j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "u" : "zh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -ZGPY_SHUANGPIN_YUNMU_DICT = { - "a" : ("a", ), - "b" : ("iao", ), - "d" : ("ie", ), - "e" : ("e", ), - "f" : ("ian", ), - "g" : ("iang", "uang"), - "h" : ("ong", "iong"), - "i" : ("i", ), - "j" : ("er", "iu"), - "k" : ("ei", ), - "l" : ("uan", ), - "m" : ("un", ), - "n" : ("ue", "ui"), - "o" : ("uo", "o"), - "p" : ("ai", ), - "q" : ("ao", ), - "r" : ("an", ), - "s" : ("ang", ), - "t" : ("eng", "ng"), - "u" : ("u", ), - "v" : ("v", ), - "w" : ("en", ), - "x" : ("ia", "ua"), - "y" : ("in", "uai"), - "z" : ("ou" ,), - ";" : ("ing", ) -} - -PYJJ_SHUANGPIN_SHENGMU_DICT = { - "a" : "'", "b" : "b", "c" : "c", "d" : "d", "f" : "f", "g" : "g", - "h" : "h", "i" : "sh","j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "u" : "ch","v" : "zh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -PYJJ_SHUANGPIN_YUNMU_DICT = { - "a" : ("a",), - "b" : ("ia","ua"), - "c" : ("uan",), - "d" : ("ao", ), - "e" : ("e",), - "f" : ("an",), - "g" : ("ang",), - "h" : ("iang","uang"), - "i" : ("i",), - "j" : ("ian",), - "k" : ("iao",), - "l" : ("in",), - "m" : ("ie",), - "n" : ("iu",), - "o" : ("uo", "o"), - "p" : ("ou",), - "q" : ("er","ing"), - "r" : ("en", ), - "s" : ("ai", ), - "t" : ("eng", "ng"), - "u" : ("u",), - "v" : ("v","ui"), - "w" : ("ei",), - "x" : ("uai","ue"), - "y" : ("ong","iong"), - "z" : ("un",), -} - -XHE_SHUANGPIN_SHENGMU_DICT = { - "b" : "b", "c" : "c", "d" : "d", "f" : "f", "g" : "g", - "h" : "h", "i" : "ch", "j" : "j", "k" : "k", "l" : "l", - "m" : "m", "n" : "n", "o" : "'", "p" : "p", "q" : "q", - "r" : "r", "s" : "s", "t" : "t", "u" : "sh", "v" : "zh", - "w" : "w", "x" : "x", "y" : "y", "z" : "z" -} - -XHE_SHUANGPIN_YUNMU_DICT = { - "a" : ("a",), - "b" : ("in",), - "c" : ("ao",), - "d" : ("ai",), - "e" : ("e",), - "f" : ("en",), - "g" : ("eng", "ng"), - "h" : ("ang",), - "i" : ("i",), - "j" : ("an",), - "k" : ("uai", "ing"), - "l" : ("iang", "uang"), - "m" : ("ian",), - "n" : ("iao",), - "o" : ("uo", "o"), - "p" : ("ie",), - "q" : ("iu",), - "r" : ("uan", "er"), - "s" : ("ong", "iong"), - "t" : ("ue",), - "u" : ("u",), - "v" : ("v", "ui"), - "w" : ("ei",), - "x" : ("ia", "ua"), - "y" : ("un",), - "z" : ("ou",), -} - -SHUANGPIN_SCHEMAS = [ - (N_("MSPY"), (MSPY_SHUANGPIN_SHENGMU_DICT, MSPY_SHUANGPIN_YUNMU_DICT)), - (N_("ZRM"), (ZRM_SHUANGPIN_SHENGMU_DICT, ZRM_SHUANGPIN_YUNMU_DICT)), - (N_("ABC"), (ABC_SHUANGPIN_SHENGMU_DICT, ABC_SHUANGPIN_YUNMU_DICT)), - (N_("ZGPY"), (ZGPY_SHUANGPIN_SHENGMU_DICT, ZGPY_SHUANGPIN_YUNMU_DICT)), - (N_("PYJJ"), (PYJJ_SHUANGPIN_SHENGMU_DICT, PYJJ_SHUANGPIN_YUNMU_DICT)), - (N_("XHE"), (XHE_SHUANGPIN_SHENGMU_DICT, XHE_SHUANGPIN_YUNMU_DICT)), -] - diff --git a/scripts/pyutil.py b/scripts/pyutil.py deleted file mode 100644 index 48edde7..0000000 --- a/scripts/pyutil.py +++ /dev/null @@ -1,148 +0,0 @@ -# -*- coding: utf-8 -*- -# vim:set et sts=4 sw=4: -# -# ibus-pinyin - The PinYin engine for IBus -# -# Copyright (c) 2007-2008 Peng Huang -# -# 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., 675 Mass Ave, Cambridge, MA 02139, USA. - -from pydict import * - -class PinYinWord: - correct_dict = {"nve" : "nue", "lve" : "lue"} - def __init__ (self, pinyin): - if pinyin in self.correct_dict: - pinyin = self.correct_dict [pinyin] - - self._pinyin = pinyin - self._is_completed = self.is_valid_pinyin () - if self._is_completed: - sheng_mu, yun_mu = self.split () - self._pinyin_id = PINYIN_DICT [self._pinyin] - self._sheng_mu_id = SHENGMU_DICT [sheng_mu] - else: - self._sheng_mu_id = SHENGMU_DICT [self._pinyin] - - def is_valid_pinyin (self): - return PINYIN_DICT.has_key (self._pinyin) - - def get_sheng_mu_id (self): - return self._sheng_mu_id - - def get_shengmu (self): - return ID_SHENGMU_DICT[self._sheng_mu_id] - - def get_pinyin_id (self): - return self._pinyin_id - - def get_pinyin (self): - return self._pinyin - - def get_pattern (self, mohu = False): - if mohu == False: - if self.is_valid_pinyin (): - return self._pinyin - else: - return self._pinyin + "%" - else: - if not self.is_valid_pinyin (): - if self._pinyin in ("zh", "ch", "sh"): - return self._pinyin[0] + "%" - return self._pinyin + "%" - else: - shengmu = self.get_shengmu () - yunmu = self._pinyin [len (shengmu):] - if shengmu in ("zh", "ch", "sh", "z", "c", "s"): - shengmu = shengmu[0] + "%" - if yunmu in ("ing", "in", "en", "eng", "an", "ang"): - yunmu = yunmu[0:2] + "%" - return shengmu + yunmu - - def split (self): - if not self.is_valid_pinyin (): - raise Exception ("Pinyin '%s' is not a valid pinyin!" % py) - if self._pinyin[:2] in SHENGMU_DICT.keys (): - return self._pinyin[:2], self._pinyin[2:] - elif self._pinyin[:1] in SHENGMU_DICT.keys (): - return self._pinyin[:1], self._pinyin[1:] - else: - return "", self._pinyin[:] - - def __str__ (self): - return self._pinyin - -class PinYinString: - def __init__ (self, string): - pass - -def load_pinyin_table (_file): - - def pinyin_table_parser (f): - for l in f: - a = unicode (l, "utf-8").strip ().split () - hanzi, pinyin, freq = a - yield (hanzi, pinyin, int (freq)) - # db.add_phrases (pinyin_table_parser (bzf)) - - hanzi_dic = {} - for hanzi, pinyin, freq in pinyin_table_parser (_file): - if not hanzi_dic.has_key (hanzi): - hanzi_dic[hanzi] = {} - - if hanzi_dic[hanzi].has_key (pinyin): - hanzi_dic[hanzi][pinyin] += freq - else: - hanzi_dic[hanzi][pinyin] = freq - - return hanzi_dic - -def load_phrase_pinyin_freq (_file): - def phrase_pinyin_parser (f): - for l in f: - phrase, pinyin, freq = unicode (l, "utf-8").strip ().split () - pinyin = pinyin.replace (u"u:", u"v") - yield (phrase, pinyin, int (freq)) - phrases_dic = {} - for phrase, pinyin, freq in phrase_pinyin_parser (_file): - if not phrases_dic.has_key (phrase): - phrases_dic[phrase] = [] - phrases_dic[phrase].append ((phrase, pinyin, freq)) - - return phrases_dic - -def load_phrase_pinyin (_file): - def phrase_pinyin_parser (f): - for l in f: - phrase, pinyin = unicode (l, "utf-8").strip ().split () - pinyin = pinyin.replace (u"u:", u"v") - yield (phrase, pinyin, 0) - phrases_dic = {} - for phrase, pinyin, freq in phrase_pinyin_parser (_file): - if not phrases_dic.has_key (phrase): - phrases_dic[phrase] = [] - phrases_dic[phrase].append ((phrase, pinyin, freq)) - - return phrases_dic - -def load_sogou_phrases (_file): - import re - dic = {} - for l in _file: - w = unicode (l, "utf8") - w = re.split (ur"\t+", w) - dic [w[0]] = (w[0], int (w[1])) - return dic - -- 2.7.4