后来想到自己Delphi有一个获得拼音的代码。于是找了出来。研究了一下代码如下:
代码如下:
function get_hz_pywbhzstr: string; pytype: integer): string;
var
I: Integer;
allstr: string;
hh: THandle;
pp: pointer;
ss: TStringList;
function retturn_wbpytempstr: string; tqtype: integer): string;
var
outstr, str: string;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count – 1 do
begin
str := ss.Strings[i];
if tempstr = trimstr[1] + str[2])) or tempstr = trimstr[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//###################
outstr := ”; //提取编码
if tqtype = 1 then
begin
for i := pos’①’, str) + 2 to pos’②’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos’②’, str) + 2 to pos’③’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 3 then
begin
for i := pos’③’, str) + 2 to pos’④’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 4 then
begin
for i := pos’④’, str) + 2 to lengthstr) do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trimoutstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResourcehInstance, ‘mywb’, ‘TXT’);
hh := LoadResourcehInstance, hh);
pp := LockResourcehh);
ss.Text := pcharpp);
UnLockResourcehh);
FreeResourcehh);
allstr := ”;
i := 0;
while i <= lengthhzstr) do //提取汉字字符
begin
if Ordhzstr[I]) > 127) then
begin
if allstr = ” then
allstr := retturn_wbpyhzstr[I] + hzstr[I + 1], pytype)
else
allstr := allstr + retturn_wbpyhzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = ” then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
Result := trimallstr);
en
function get_hz_pywbhzstr: string; pytype: integer): string;
var
I: Integer;
allstr: string;
hh: THandle;
pp: pointer;
ss: TStringList;
function retturn_wbpytempstr: string; tqtype: integer): string;
var
outstr, str: string;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count – 1 do
begin
str := ss.Strings[i];
if tempstr = trimstr[1] + str[2])) or tempstr = trimstr[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//###################
outstr := ”; //提取编码
if tqtype = 1 then
begin
for i := pos’①’, str) + 2 to pos’②’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos’②’, str) + 2 to pos’③’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 3 then
begin
for i := pos’③’, str) + 2 to pos’④’, str) – 1 do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 4 then
begin
for i := pos’④’, str) + 2 to lengthstr) do
if str[i] ” then if outstr = ” then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trimoutstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResourcehInstance, ‘mywb’, ‘TXT’);
hh := LoadResourcehInstance, hh);
pp := LockResourcehh);
ss.Text := pcharpp);
UnLockResourcehh);
FreeResourcehh);
allstr := ”;
i := 0;
while i <= lengthhzstr) do //提取汉字字符
begin
if Ordhzstr[I]) > 127) then
begin
if allstr = ” then
allstr := retturn_wbpyhzstr[I] + hzstr[I + 1], pytype)
else
allstr := allstr + retturn_wbpyhzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = ” then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
Result := trimallstr);
en
这里需要用到一个资源文件。随后我会把资源文件地址发上来。
我把他改成Python代码供大家研究。。。。
代码如下:
# -*-coding:utf-8-*-
# 返回汉字的拼音
def Return_pinyinword):
global reslist
for line in reslist:
if word==line[0]+line[1]) or word==line[2]+line[3]):
str = line
break
# 取①和②之间的内容
s = str.findu’①’)+4
e = str.findu’②’)+3
return str[s:e]
def GetPyword):
#首先装载资源文件
i=0
allstr = ”
while i<lenword):
if ordword[i])>127:
if allstr:
allstr += Return_pinyinword[i]+word[i+1])
else:
allstr = Return_pinyinword[i]+word[i+1])
i +=2
else:
if allstr:
allstr += word[i]
else:
allstr = word[i]
i +=1
return allstr
if __name__==’__main__’:
f = open’wbtext1.txt’,’r’)
reslist = f.readlines)
f.close)
word = raw_inputu’请输入汉字: ‘)
print GetPyword).lower)
如果大家有什么问题欢迎讨论。。。。。。