后来想到自己Delphi有一个获得拼音的代码。于是找了出来。研究了一下代码如下:
function get_hz_pywb(hzstr: string; pytype: integer): string; <BR>var <BR>I: Integer; <BR>allstr: string; <BR>hh: THandle; <BR>pp: pointer; <BR>ss: TStringList; <BR>function retturn_wbpy(tempstr: string; tqtype: integer): string; <BR>var <BR>outstr, str: string; <BR>i: integer; <BR>begin <BR>//################### 汉字查询电位 <BR>i := 0; <BR>while i <= ss.Count - 1 do <BR>begin <BR>str := ss.Strings[i]; <BR>if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then <BR>begin <BR>str := ss.Strings[i]; <BR>Break; <BR>end; <BR>i := i + 1; <BR>end; <BR>//################### <BR>outstr := ''; //提取编码 <BR>if tqtype = 1 then <BR>begin <BR>for i := pos('①', str) + 2 to pos('②', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 2 then <BR>begin <BR>for i := pos('②', str) + 2 to pos('③', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 3 then <BR>begin <BR>for i := pos('③', str) + 2 to pos('④', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 4 then <BR>begin <BR>for i := pos('④', str) + 2 to length(str) do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>Result := trim(outstr); <BR>end; <BR>begin <BR>//加载资源文件,将内容赋值给 s <BR>ss := TStringList.Create; <BR>hh := FindResource(hInstance, 'mywb', 'TXT'); <BR>hh := LoadResource(hInstance, hh); <BR>pp :=<span>本文来源gaodai#ma#com搞*!代#%^码网5</span> LockResource(hh); <BR>ss.Text := pchar(pp); <BR>UnLockResource(hh); <BR>FreeResource(hh); <BR>allstr := ''; <BR>i := 0; <BR>while i <= length(hzstr) do //提取汉字字符 <BR>begin <BR>if (Ord(hzstr[I]) > 127) then <BR>begin <BR>if allstr = '' then <BR>allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) <BR>else <BR>allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype); <BR>i := i + 2; <BR>end <BR>else <BR>begin <BR>if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I]; <BR>i := i + 1; <BR>end; <BR>end; <BR>ss.Free; <BR>Result := trim(allstr); <BR>en <BR>function get_hz_pywb(hzstr: string; pytype: integer): string; <BR>var <BR>I: Integer; <BR>allstr: string; <BR>hh: THandle; <BR>pp: pointer; <BR>ss: TStringList; <BR>function retturn_wbpy(tempstr: string; tqtype: integer): string; <BR>var <BR>outstr, str: string; <BR>i: integer; <BR>begin <BR>//################### 汉字查询电位 <BR>i := 0; <BR>while i <= ss.Count - 1 do <BR>begin <BR>str := ss.Strings[i]; <BR>if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then <BR>begin <BR>str := ss.Strings[i]; <BR>Break; <BR>end; <BR>i := i + 1; <BR>end; <BR>//################### <BR>outstr := ''; //提取编码 <BR>if tqtype = 1 then <BR>begin <BR>for i := pos('①', str) + 2 to pos('②', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 2 then <BR>begin <BR>for i := pos('②', str) + 2 to pos('③', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 3 then <BR>begin <BR>for i := pos('③', str) + 2 to pos('④', str) - 1 do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>if tqtype = 4 then <BR>begin <BR>for i := pos('④', str) + 2 to length(str) do <BR>if str[i] '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; <BR>end; <BR>Result := trim(outstr); <BR>end; <BR>begin <BR>//加载资源文件,将内容赋值给 s <BR>ss := TStringList.Create; <BR>hh := FindResource(hInstance, 'mywb', 'TXT'); <BR>hh := LoadResource(hInstance, hh); <BR>pp := LockResource(hh); <BR>ss.Text := pchar(pp); <BR>UnLockResource(hh); <BR>FreeResource(hh); <BR>allstr := ''; <BR>i := 0; <BR>while i <= length(hzstr) do //提取汉字字符 <BR>begin <BR>if (Ord(hzstr[I]) > 127) then <BR>begin <BR>if allstr = '' then <BR>allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) <BR>else <BR>allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype); <BR>i := i + 2; <BR>end <BR>else <BR>begin <BR>if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I]; <BR>i := i + 1; <BR>end; <BR>end; <BR>ss.Free; <BR>Result := trim(allstr); <BR>en <BR>
这里需要用到一个资源文件。随后我会把资源文件地址发上来。
我把他改成Python代码供大家研究。。。。
# -*-coding:utf-8-*- <BR># 返回汉字的拼音 <BR>def Return_pinyin(word): <BR>global reslist <BR>for line in reslist: <BR>if (word==line[0]+line[1]) or (word==line[2]+line[3]): <BR>str = line <BR>break <BR># 取①和②之间的内容 <BR>s = str.find(u'①')+4 <BR>e = str.find(u'②')+3 <BR>return str[s:e] <br><br>def GetPy(word): <BR>#首先装载资源文件 <BR>i=0 <BR>allstr = '' <BR>while i<len(word): <BR>if ord(word[i])>127: <BR>if allstr: <BR>allstr += Return_pinyin(word[i]+word[i+1]) <BR>else: <BR>allstr = Return_pinyin(word[i]+word[i+1]) <BR>i +=2 <BR>else: <BR>if allstr: <BR>allstr += word[i] <BR>else: <BR>allstr = word[i] <BR>i +=1 <BR>return allstr <BR>if __name__=='__main__': <BR>f = open('wbtext1.txt','r') <BR>reslist = f.readlines() <BR>f.close() <BR>word = raw_input(u'请输入汉字: ') <BR>print GetPy(word).lower() <BR>
如果大家有什么问题欢迎讨论。。。。。。