使用Python進行中文繁簡轉換的實現程式碼
中文繁體、簡體的差異,在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",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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。