使用Python進行中文繁簡轉換的實現(xiàn)代碼
中文繁體、簡體的差異,在NPL中類似英文中的大小寫,但又比大小寫更為復雜,比如同樣為繁體字,大陸、香港和臺灣又不一樣。先前寫過一篇中文繁簡轉換的文章,感覺寫的不太詳細,今天就針對Python下如何使用做進一步的記錄。
OpenCC(Open Chinese Convert)
OpenCC是一個開源的中文繁簡轉化項目,支持詞匯級別的轉換、異體字轉換和地區(qū)習慣用詞轉換(中國大陸、臺灣、香港)。主要特點為:
- 嚴格區(qū)分「一簡對多繁」和「一簡對多異」。
- 完全兼容異體字,可以實現(xiàn)動態(tài)替換。
- 嚴格審校一簡對多繁詞條,原則為「能分則不合」。
- 支持中國大陸、臺灣、香港異體字和地區(qū)習慣用詞轉換,如「裏」「裡」、「鼠標」「滑鼠」。
- 詞庫和函數(shù)庫完全分離,可以自由修改、導入、擴展。
- 支持C、C++、Python、PHP、Java、Ruby、js and Android。
- 兼容Windows、Linux、Mac平臺。
opencc-python是用純Python所寫的OpenCC實現(xiàn)。需要注意的是使用pip安裝時正確的命令是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 = '開放中文轉換(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 詞匯表的最大正向匹配簡繁轉換,支持地區(qū)詞轉換: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'人體內存在很多微生物', '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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
運行tensorflow python程序,限制對GPU和CPU的占用操作
今天小編就為大家分享一篇運行tensorflow python程序,限制對GPU和CPU的占用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python設計tcp數(shù)據包協(xié)議類的例子
今天小編就為大家分享一篇python設計tcp數(shù)據包協(xié)議類的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07利用Python的Flask框架來構建一個簡單的數(shù)字商品支付解決方案
這篇文章主要介紹了利用Python的Flask框架來構建一個簡單的數(shù)字商品支付解決方案,文中用極簡的代碼展示了一個flask框架下的支付模版,需要的朋友可以參考下2015-03-03