使用Python進(jìn)行中文繁簡轉(zhuǎn)換的實(shí)現(xiàn)代碼
中文繁體、簡體的差異,在NPL中類似英文中的大小寫,但又比大小寫更為復(fù)雜,比如同樣為繁體字,大陸、香港和臺灣又不一樣。先前寫過一篇中文繁簡轉(zhuǎn)換的文章,感覺寫的不太詳細(xì),今天就針對Python下如何使用做進(jìn)一步的記錄。
OpenCC(Open Chinese Convert)
OpenCC是一個開源的中文繁簡轉(zhuǎn)化項(xiàng)目,支持詞匯級別的轉(zhuǎn)換、異體字轉(zhuǎn)換和地區(qū)習(xí)慣用詞轉(zhuǎn)換(中國大陸、臺灣、香港)。主要特點(diǎn)為:
- 嚴(yán)格區(qū)分「一簡對多繁」和「一簡對多異」。
- 完全兼容異體字,可以實(shí)現(xiàn)動態(tài)替換。
- 嚴(yán)格審校一簡對多繁詞條,原則為「能分則不合」。
- 支持中國大陸、臺灣、香港異體字和地區(qū)習(xí)慣用詞轉(zhuǎn)換,如「裏」「裡」、「鼠標(biāo)」「滑鼠」。
- 詞庫和函數(shù)庫完全分離,可以自由修改、導(dǎo)入、擴(kuò)展。
- 支持C、C++、Python、PHP、Java、Ruby、js and Android。
- 兼容Windows、Linux、Mac平臺。
opencc-python是用純Python所寫的OpenCC實(shí)現(xiàn)。需要注意的是使用pip安裝時(shí)正確的命令是pip install opencc-python-reimplemented,如果使用pip install opencc-python會出現(xiàn)如下錯誤:
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 = '開放中文轉(zhuǎn)換(Pure Python)' converted = cc.convert(to_convert) print(converted)
opencc-python命令行調(diào)用:
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.
總結(jié):OpenCC精度非常的高,另外也包含了習(xí)慣用詞轉(zhuǎn)換,比較適合放置在網(wǎng)站上進(jìn)行自動的語言翻譯。
參考鏈接:
https://github.com/BYVoid/OpenCC
https://github.com/yichen0831/opencc-python
zhconv
zhconv 提供基于 MediaWiki 和 OpenCC 詞匯表的最大正向匹配簡繁轉(zhuǎn)換,支持地區(qū)詞轉(zhuǎn)換: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'我?guī)质颤N不干你事。', 'zh-cn')) print(convert(u'人體內(nèi)存在很多微生物', '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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
運(yùn)行tensorflow python程序,限制對GPU和CPU的占用操作
今天小編就為大家分享一篇運(yùn)行tensorflow python程序,限制對GPU和CPU的占用操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python open讀寫文件實(shí)現(xiàn)腳本
Python中文件操作可以通過open函數(shù),這的確很像C語言中的fopen。通過open函數(shù)獲取一個file object,然后調(diào)用read(),write()等方法對文件進(jìn)行讀寫操作。2008-09-09python設(shè)計(jì)tcp數(shù)據(jù)包協(xié)議類的例子
今天小編就為大家分享一篇python設(shè)計(jì)tcp數(shù)據(jù)包協(xié)議類的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07利用Python的Flask框架來構(gòu)建一個簡單的數(shù)字商品支付解決方案
這篇文章主要介紹了利用Python的Flask框架來構(gòu)建一個簡單的數(shù)字商品支付解決方案,文中用極簡的代碼展示了一個flask框架下的支付模版,需要的朋友可以參考下2015-03-03Python銀行系統(tǒng)實(shí)戰(zhàn)源碼
這篇文章主要為大家詳細(xì)介紹了Python銀行系統(tǒng)實(shí)戰(zhàn)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Python之根據(jù)輸入?yún)?shù)計(jì)算結(jié)果案例講解
這篇文章主要介紹了Python之根據(jù)輸入?yún)?shù)計(jì)算結(jié)果案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Python面向?qū)ο笾畠?nèi)置函數(shù)相關(guān)知識總結(jié)
本次要總結(jié)的的內(nèi)置函數(shù)共8個,他們都跟面向?qū)ο蟮闹R相關(guān),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python進(jìn)行Socket接口測試的實(shí)現(xiàn)
Python 提供了強(qiáng)大且易于使用的 socket 模塊,使開發(fā)者能夠輕松地創(chuàng)建客戶端和服務(wù)器應(yīng)用,實(shí)現(xiàn)數(shù)據(jù)傳輸和交互,本文主要介紹了Python進(jìn)行Socket接口測試的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06