withopen(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] except: line = f_line.split(' ') self.word_dict[line[0]] = line[1]
defhanzi2pinyin(self, string=""): result = [] ifnotisinstance(string, str): 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())
return result
defhanzi2pinyin_split(self, string="", split=""): result = self.hanzi2pinyin(string=string) if split == "": return result else: return split.join(result)