#!/usr/bin/env python# encoding: utf-8"""Created by Eric Lo on 2010-05-20.Copyright (c) 2010 __lxneng@gmail.com__. http://lxneng.com All rights reserved."""classPinyin():def__init__(self,data_path='./Mandarin.dat'):self.dict={}forlineinopen(data_path):k,v=line.split('\t')self.dict[k]=vself.splitter=''defget_pinyin(self,chars=u"你好吗"):result=[]forcharinchars:key="%X"%ord(char)try:result.append(self.dict[key].split(" ")[0].strip()[:-1].lower())except:result.append(char)returnself.splitter.join(result)defget_initials(self,char=u'你'):try:returnself.dict["%X"%ord(char)].split(" ")[0][0]except:returnchar