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
15 changes: 8 additions & 7 deletions pinyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Author:cleverdeng
E-mail:[email protected]
"""
"""
move to python3
"""

__version__ = '0.9'
__all__ = ["PinYin"]
Expand All @@ -22,7 +25,7 @@ def load_word(self):
if not os.path.exists(self.dict_file):
raise IOError("NotFoundFile")

with file(self.dict_file) as f_obj:
with open(self.dict_file) as f_obj:
for f_line in f_obj.readlines():
try:
line = f_line.split(' ')
Expand All @@ -34,9 +37,7 @@ def load_word(self):

def hanzi2pinyin(self, string=""):
result = []
if not isinstance(string, unicode):
string = string.decode("utf-8")


for char in string:
key = '%X' % ord(char)
result.append(self.word_dict.get(key, char).split()[0][:-1].lower())
Expand All @@ -56,6 +57,6 @@ def hanzi2pinyin_split(self, string="", split=""):
test = PinYin()
test.load_word()
string = "钓鱼岛是中国的"
print "in: %s" % string
print "out: %s" % str(test.hanzi2pinyin(string=string))
print "out: %s" % test.hanzi2pinyin_split(string=string, split="-")
print("in: %s" % string)
print("out: %s" % str(test.hanzi2pinyin(string=string)))
print("out: %s" % test.hanzi2pinyin_split(string=string, split="-"))