Python实现汉字转拼音:基于Delphi源码的移植与解析

栗子
栗子
发布时间:2026-10-13| 6人浏览过
原创

本文介绍如何使用Python实现汉字转拼音的功能,并对比了原始的Delphi实现代码。通过加载外部资源文件进行汉字编码匹配,Python版本简化了资源加载逻辑,提供了完整的转换函数及主程序示例,适用于需要批量处理中文文本拼音转换的场景。

Delphi源码解析与Python移植

此前曾注意到Delphi中有一段获取汉字拼音的代码,经查找并研究后,其核心逻辑如下所示。


复制代码

具体实现代码参考如下:


function get_hz_pywb(hzstr: string; pytype: integer): string;


var


I: Integer;


allstr: string;


hh: THandle;


pp: pointer;


ss: TStringList;


function retturn_wbpy(tempstr: 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 = trim(str[1] + str[2])) or (tempstr = trim(str[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 length(str) do


if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];


end;


Result := trim(outstr);


end;


begin


//加载资源文件,将内容赋值给 s


ss := TStringList.Create;


hh := FindResource(hInstance, 'mywb', 'TXT');


hh := LoadResource(hInstance, hh);


pp := LockResource(hh);


ss.Text := pchar(pp);


UnLockResource(hh);


FreeResource(hh);


allstr := '';


i := 0;


while i <= length(hzstr) do //提取汉字字符


begin


if (Ord(hzstr[I]) > 127) then


begin


if allstr = '' then


allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)


else


allstr := allstr + retturn_wbpy(hzstr[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 := trim(allstr);


en


function get_hz_pywb(hzstr: string; pytype: integer): string;


var


I: Integer;


allstr: string;


hh: THandle;


pp: pointer;


ss: TStringList;


function retturn_wbpy(tempstr: 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 = trim(str[1] + str[2])) or (tempstr = trim(str[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 length(str) do


if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];


end;


Result := trim(outstr);


end;


begin


//加载资源文件,将内容赋值给 s


ss := TStringList.Create;


hh := FindResource(hInstance, 'mywb', 'TXT');


hh := LoadResource(hInstance, hh);


pp := LockResource(hh);


ss.Text := pchar(pp);


UnLockResource(hh);


FreeResource(hh);


allstr := '';


i := 0;


while i <= length(hzstr) do //提取汉字字符


begin


if (Ord(hzstr[I]) > 127) then


begin


if allstr = '' then


allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)


else


allstr := allstr + retturn_wbpy(hzstr[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 := trim(allstr);


en



该实现依赖一个外部资源文件,后续将提供该文件的下载地址。


随后,作者将其转换为Python代码以供参考研究。


复制代码

Python实现代码如下:


# -*-coding:utf-8-*-


# 返回汉字的拼音


def Return_pinyin(word):


global reslist


for line in reslist:


if (word==line[0]+line[1]) or (word==line[2]+line[3]):


str = line


break


# 取①和②之间的内容


s = str.find(u'①')+4


e = str.find(u'②')+3


return str[s:e]



def GetPy(word):


#首先装载资源文件


i=0


allstr = ''


while i<len(word):


if ord(word[i])>127:


if allstr:


allstr += Return_pinyin(word[i]+word[i+1])


else:


allstr = Return_pinyin(word[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_input(u'请输入汉字: ')


print GetPy(word).lower()



欢迎就相关问题进行交流讨论。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。