使用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 相关文章推荐
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
May 20 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 Python
python数据预处理之将类别数据转换为数值的方法
Jul 05 Python
Python爬虫实现简单的爬取有道翻译功能示例
Jul 13 Python
Python实现的建造者模式示例
Aug 06 Python
python TKinter获取文本框内容的方法
Oct 11 Python
Python实例方法、类方法、静态方法的区别与作用详解
Mar 25 Python
六行python代码的爱心曲线详解
May 17 Python
Pandas实现dataframe和np.array的相互转换
Nov 30 Python
Python如何使用字符打印照片
Jan 03 Python
python 异步async库的使用说明
May 04 Python
python3通过udp实现组播数据的发送和接收操作
May 05 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与XML联手进行网站编程代码实例
2008/07/10 PHP
Yii2实现上下联动下拉框功能的方法
2016/08/10 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
php多进程中的阻塞与非阻塞操作实例分析
2020/03/04 PHP
基于jquery的网页SELECT下拉框美化代码
2010/10/28 Javascript
基于jquery的鼠标拖动效果代码
2012/05/30 Javascript
js 判断checkbox是否选中的操作方法
2012/11/09 Javascript
鼠标划过实现延迟加载并隐藏层的js代码
2013/10/11 Javascript
js 定时器setTimeout无法调用局部变量的解决办法
2013/11/28 Javascript
Nodejs学习笔记之测试驱动
2015/04/16 NodeJs
关于微信jssdk实现多图片上传的一点心得分享
2016/12/13 Javascript
浅谈js script标签中的预解析
2016/12/30 Javascript
Vue+Vux项目实践完整代码
2017/11/30 Javascript
如何让你的JS代码更好看易读
2017/12/01 Javascript
Vue 实现拖动滑块验证功能(只有css+js没有后台验证步骤)
2018/08/24 Javascript
详解Angular中实现自定义组件的双向绑定的两种方法
2018/11/23 Javascript
使用jquery模拟a标签的click事件无法实现跳转的解决
2018/12/04 jQuery
微信小程序实现简单跑马灯效果
2020/05/26 Javascript
如何使用VuePress搭建一个类型element ui文档
2019/02/14 Javascript
vue-cli配置全局sass、less变量的方法
2019/06/06 Javascript
前端插件之Bootstrap Dual Listbox使用教程
2019/07/23 Javascript
如何换个角度使用VUE过滤器详解
2019/09/11 Javascript
基于vue和websocket的多人在线聊天室
2020/02/01 Javascript
基于vue的video播放器的实现示例
2021/02/19 Vue.js
django 2.2和mysql使用的常见问题
2019/07/18 Python
python elasticsearch从创建索引到写入数据的全过程
2019/08/04 Python
AmazeUI导航的示例代码
2020/08/14 HTML / CSS
美国电力供应商店/电气批发商:USESI
2018/10/12 全球购物
美国农场商店:Blain’s Farm & Fleet
2020/01/17 全球购物
EJB需直接实现它的业务接口或Home接口吗,请简述理由
2016/11/23 面试题
俄语专业职业生涯规划
2014/02/26 职场文书
超市店庆活动方案
2014/08/31 职场文书
2014年银行柜员工作总结
2014/11/12 职场文书
2015年节能减排工作总结
2015/05/14 职场文书
2015年三年级班主任工作总结
2015/05/21 职场文书
《将心比心》教学反思
2016/02/23 职场文书