使用Python进行中文繁简转换的实现代码


Posted in Python onOctober 18, 2019

中文繁体、简体的差异,在NPL中类似英文中的大小写,但又比大小写更为复杂,比如同样为繁体字,大陆、香港和台湾又不一样。先前写过一篇中文繁简转换的文章,感觉写的不太详细,今天就针对Python下如何使用做进一步的记录。

OpenCC(Open Chinese Convert)

OpenCC是一个开源的中文繁简转化项目,支持词汇级别的转换、异体字转换和地区习惯用词转换(中国大陆、台湾、香港)。主要特点为:

  • 严格区分「一简对多繁」和「一简对多异」。
  • 完全兼容异体字,可以实现动态替换。
  • 严格审校一简对多繁词条,原则为「能分则不合」。
  • 支持中国大陆、台湾、香港异体字和地区习惯用词转换,如「?」「?」、「鼠?恕埂富?蟆埂?/li>
  • 词库和函数库完全分离,可以自由修改、导入、扩展。
  • 支持C、C++、Python、PHP、Java、Ruby、js and Android。
  • 兼容Windows、Linux、Mac平台。

opencc-python是用纯Python所写的OpenCC实现。需要注意的是使用pip安装时正确的命令是pip install opencc-python-reimplemented,如果使用pip install opencc-python会出现如下错误:

Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\qw\AppData\Local\Temp\pip-install-rvsnpo_d\opencc-python\setup.py", line 1, in <module>
        from distribute_setup import use_setuptools
    ModuleNotFoundError: No module named 'distribute_setup'

opencc-pytho的使用:

from opencc import OpenCC
 
cc = OpenCC('t2s')
# hk2s: Traditional Chinese (Hong Kong standard) to Simplified Chinese
# s2hk: Simplified Chinese to Traditional Chinese (Hong Kong standard)
# s2t: Simplified Chinese to Traditional Chinese
# s2tw: Simplified Chinese to Traditional Chinese (Taiwan standard)
# s2twp: Simplified Chinese to Traditional Chinese (Taiwan standard, with phrases)
# t2hk: Traditional Chinese to Traditional Chinese (Hong Kong standard)
# t2s: Traditional Chinese to Simplified Chinese
# t2tw: Traditional Chinese to Traditional Chinese (Taiwan standard)
# tw2s: Traditional Chinese (Taiwan standard) to Simplified Chinese
# tw2sp: Traditional Chinese (Taiwan standard) to Simplified Chinese (with phrases)
 
to_convert = '?放中文??Q(Pure Python)'
converted = cc.convert(to_convert)
print(converted)

opencc-python命令行调用:

usage: python -m opencc [-h] [-i <file>] [-o <file>] [-c <conversion>]
            [--in-enc <encoding>] [--out-enc <encoding>]
 
optional arguments:
 -h, --help      show this help message and exit
 -i <file>, --input <file>
            Read original text from <file>. (default: None = STDIN)
 -o <file>, --output <file>
            Write converted text to <file>. (default: None = STDOUT)
 -c <conversion>, --config <conversion>
            Conversion (default: None)
 --in-enc <encoding>  Encoding for input (default: UTF-8)
 --out-enc <encoding> Encoding for output (default: UTF-8)
 
example with UTF-8 encoded file:
 
 python -m opencc -c s2t -i my_simplified_input_file.txt -o my_traditional_output_file.txt
 
See https://docs.python.org/3/library/codecs.html#standard-encodings for list of encodings.

总结:OpenCC精度非常的高,另外也包含了习惯用词转换,比较适合放置在网站上进行自动的语言翻译。

参考链接:

https://github.com/BYVoid/OpenCC
https://github.com/yichen0831/opencc-python

zhconv

zhconv 提供基于 MediaWiki 和 OpenCC 词汇表的最大正向匹配简繁转换,支持地区词转换:zh-cn, zh-tw, zh-hk, zh-sg, zh-hans, zh-hant。Python 2、3通用。

安装方式:pip install zhconv

使用示例:

from zhconv import convert
 
print(convert(u'我?质颤N不干你事。', 'zh-cn'))
print(convert(u'人体内存在很多微生物', 'zh-tw'))

命令行工具:

python -mzhconv [-w] {zh-cn|zh-tw|zh-hk|zh-sg|zh-hans|zh-hant|zh} < input > output

参考链接:

https://github.com/gumblex/zhconv

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
CentOS安装pillow报错的解决方法
Jan 27 Python
Python实现将SQLite中的数据直接输出为CVS的方法示例
Jul 13 Python
基于python的图片修复程序(实现水印去除)
Jun 04 Python
python 定义给定初值或长度的list方法
Jun 23 Python
PyTorch的深度学习入门之PyTorch安装和配置
Jun 27 Python
对Python中小整数对象池和大整数对象池的使用详解
Jul 09 Python
Python ATM功能实现代码实例
Mar 19 Python
python怎么对数字进行过滤
Jul 05 Python
Python下载网易云歌单歌曲的示例代码
Aug 12 Python
详解pandas赋值失败问题解决
Nov 29 Python
Python读取pdf表格写入excel的方法
Jan 22 Python
Python经常使用的一些内置函数
Apr 11 Python
Python Django框架防御CSRF攻击的方法分析
Oct 18 #Python
python使用matplotlib绘制雷达图
Oct 18 #Python
Python 日志logging模块用法简单示例
Oct 18 #Python
python调用matplotlib模块绘制柱状图
Oct 18 #Python
Python Django模板之模板过滤器与自定义模板过滤器示例
Oct 18 #Python
树莓派4B+opencv4+python 打开摄像头的实现方法
Oct 18 #Python
python使用Matplotlib改变坐标轴的默认位置
Oct 18 #Python
You might like
PHP新手用的Insert和Update语句构造类
2012/03/31 PHP
深入探讨:PHP使用数据库永久连接方式操作MySQL的是与非
2013/06/05 PHP
php中通过curl检测页面是否被百度收录
2013/09/27 PHP
php发送get、post请求的6种方法简明总结
2014/07/08 PHP
PHP常见的几种攻击方式实例小结
2019/04/29 PHP
点击按钮或链接不跳转只刷新页面的脚本整理
2013/10/22 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
JavaScript 性能优化小结
2015/10/12 Javascript
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound的解决方法
2017/01/19 Javascript
jQuery插件zTree实现的多选树效果示例
2017/03/08 Javascript
js学习心得_一个简单的动画库封装tween.js
2017/07/14 Javascript
django中使用vue.js的要点总结
2019/07/07 Javascript
微信小程序点击列表跳转到对应详情页过程解析
2019/09/26 Javascript
JS插件amCharts实现绘制柱形图默认显示数值功能示例
2019/11/26 Javascript
[01:38]【DOTA2亚洲邀请赛】Sumail——梦开始的地方
2017/03/03 DOTA
python抽象基类用法实例分析
2015/06/04 Python
Python实现PS滤镜的旋转模糊功能示例
2018/01/20 Python
python 使用正则表达式按照多个空格分割字符的实例
2018/12/20 Python
django-rest-swagger的优化使用方法
2019/08/29 Python
使用Python实现画一个中国地图
2019/11/23 Python
简单了解为什么python函数后有多个括号
2019/12/19 Python
基于PyQT实现区分左键双击和单击
2020/05/19 Python
wordpress添加Html5的表单验证required方法小结
2020/08/18 HTML / CSS
一家专门做特卖的网站:唯品会
2016/10/09 全球购物
台湾7-ELEVEN线上购物中心:7-11
2021/01/21 全球购物
新闻学专业应届生求职信
2013/11/08 职场文书
毕业自荐书
2013/12/09 职场文书
运动会稿件50字
2014/02/17 职场文书
诚实守信演讲稿
2014/09/01 职场文书
校园广播站开场白
2015/06/01 职场文书
初中军训感言
2015/08/01 职场文书
陶瓷类经典广告语集锦
2019/10/25 职场文书
详解python字符串驻留技术
2021/05/21 Python
Nginx实现高可用集群构建(Keepalived+Haproxy+Nginx)
2021/05/27 Servers
漫画「你在春天醒来」第10卷封面公开
2022/03/21 日漫
Mybatis-plus配置分页插件返回统一结果集
2022/06/21 Java/Android