黑松山资源网 Design By www.paidiu.com

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

OpenCC(Open Chinese Convert)

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

  • 严格区分「一简对多繁」和「一简对多异」。
  • 完全兼容异体字,可以实现动态替换。
  • 严格审校一简对多繁词条,原则为「能分则不合」。
  • 支持中国大陆、台湾、香港异体字和地区习惯用词转换,如「裏」「裡」、「鼠標」「滑鼠」。
  • 词库和函数库完全分离,可以自由修改、导入、扩展。
  • 支持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 = '開放中文轉換(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'我幹什麼不干你事。', '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

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

黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。