使用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实现telnet服务器的方法
Jul 10 Python
python利用lxml读写xml格式的文件
Aug 10 Python
Python爬虫实现爬取京东手机页面的图片(实例代码)
Nov 30 Python
pandas.DataFrame选取/排除特定行的方法
Jul 03 Python
详解Django的CSRF认证实现
Oct 09 Python
python爬虫解决验证码的思路及示例
Aug 01 Python
python智联招聘爬虫并导入到excel代码实例
Sep 09 Python
Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space
Feb 23 Python
深入浅析Python 命令行模块 Click
Mar 11 Python
plt.figure()参数使用详解及运行演示
Jan 08 Python
python opencv检测直线 cv2.HoughLinesP的实现
Jun 18 Python
Python数据处理的三个实用技巧分享
Apr 01 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
星际争霸秘籍
2020/03/04 星际争霸
php中批量删除Mysql中相同前缀的数据表的代码
2011/07/01 PHP
ThinkPHP中I(),U(),$this-&gt;post()等函数用法
2014/11/22 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
YII框架页面缓存操作示例
2019/04/29 PHP
jquery 经典动画菜单效果代码
2010/01/26 Javascript
仿百度联盟对联广告实现代码
2014/08/30 Javascript
PHP使用方法重载实现动态创建属性的get和set方法
2014/11/17 Javascript
jQuery实现表格行上移下移和置顶的方法
2015/05/22 Javascript
JavaScript性能优化之小知识总结
2015/11/20 Javascript
AngularJS入门(用ng-repeat指令实现循环输出
2016/05/05 Javascript
JS Array创建及concat()split()slice()的使用方法
2016/06/03 Javascript
Bootstrap树形菜单插件TreeView.js使用方法详解
2016/11/01 Javascript
纯js实现悬浮按钮组件
2016/12/17 Javascript
JS编写兼容IE6,7,8浏览器无缝自动轮播
2018/10/12 Javascript
微信小程序之onLaunch与onload异步问题详解
2019/03/28 Javascript
使用webpack搭建pixi.js开发环境
2020/02/12 Javascript
Vuex中的Mutations的具体使用方法
2020/06/01 Javascript
[53:03]Optic vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
[54:26]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第一场 12.10
2020/12/12 DOTA
python让图片按照exif信息里的创建时间进行排序的方法
2015/03/16 Python
Python判断文件和文件夹是否存在的方法
2015/05/21 Python
python基础教程之五种数据类型详解
2017/01/12 Python
python数据封装json格式数据
2018/03/04 Python
python遍历文件夹,指定遍历深度与忽略目录的方法
2018/07/11 Python
django多对多表的创建,级联删除及手动创建第三张表
2019/07/25 Python
Django REST Framework序列化外键获取外键的值方法
2019/07/26 Python
Django框架教程之中间件MiddleWare浅析
2019/12/29 Python
python爬虫线程池案例详解(梨视频短视频爬取)
2021/02/20 Python
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
党章培训心得体会
2014/09/04 职场文书
2015年世界无车日活动总结
2015/03/23 职场文书
幼儿园见习总结
2015/06/23 职场文书
高二英语教学反思
2016/03/03 职场文书
nginx如何将http访问的网站改成https访问
2021/03/31 Servers
2021年pycharm的最新安装教程及基本使用图文详解
2021/04/03 Python