Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions pinyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
__all__ = ["PinYin"]

import os.path
import re

INDEX =[0,0,0,0,2239,5543,9510,13471,17413,21406,25333]

class PinYin(object):
def __init__(self, dict_file='word.data'):
Expand All @@ -25,8 +27,11 @@ def load_word(self):
with file(self.dict_file) as f_obj:
for f_line in f_obj.readlines():
try:
line = f_line.split(' ')
self.word_dict[line[0]] = line[1]
match = re.compile('\s+')
line = match.split(f_line)
self.word_dict.setdefault(line[0],[])
for value in line[1:]:
self.word_dict[line[0]].append(value)
except:
line = f_line.split(' ')
self.word_dict[line[0]] = line[1]
Expand All @@ -39,8 +44,16 @@ def hanzi2pinyin(self, string=""):

for char in string:
key = '%X' % ord(char)
result.append(self.word_dict.get(key, char).split()[0][:-1].lower())

if len(key)==4:
index = INDEX[(int)(key[0])]
else:
index = INDEX[10]

for ks in sorted(self.word_dict)[index:]:
if key == ks:
result.append(self.word_dict[key][0][:-1].lower())
break

return result


Expand Down