溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Python進(jìn)行中文繁簡轉(zhuǎn)換的實(shí)現(xiàn)代碼

發(fā)布時(shí)間:2020-09-19 06:14:49 來源:腳本之家 閱讀:314 作者:標(biāo)點(diǎn)符 欄目:開發(fā)技術(shù)

中文繁體、簡體的差異,在NPL中類似英文中的大小寫,但又比大小寫更為復(fù)雜,比如同樣為繁體字,大陸、香港和臺(tái)灣又不一樣。先前寫過一篇中文繁簡轉(zhuǎn)換的文章,感覺寫的不太詳細(xì),今天就針對(duì)Python下如何使用做進(jìn)一步的記錄。

OpenCC(Open Chinese Convert)

OpenCC是一個(gè)開源的中文繁簡轉(zhuǎn)化項(xiàng)目,支持詞匯級(jí)別的轉(zhuǎn)換、異體字轉(zhuǎn)換和地區(qū)習(xí)慣用詞轉(zhuǎn)換(中國大陸、臺(tái)灣、香港)。主要特點(diǎn)為:

  • 嚴(yán)格區(qū)分「一簡對(duì)多繁」和「一簡對(duì)多異」。
  • 完全兼容異體字,可以實(shí)現(xiàn)動(dòng)態(tài)替換。
  • 嚴(yán)格審校一簡對(duì)多繁詞條,原則為「能分則不合」。
  • 支持中國大陸、臺(tái)灣、香港異體字和地區(qū)習(xí)慣用詞轉(zhuǎn)換,如「裏」「裡」、「鼠標(biāo)」「滑鼠」。
  • 詞庫和函數(shù)庫完全分離,可以自由修改、導(dǎo)入、擴(kuò)展。
  • 支持C、C++、Python、PHP、Java、Ruby、js and Android。
  • 兼容Windows、Linux、Mac平臺(tái)。

opencc-python是用純Python所寫的OpenCC實(shí)現(xiàn)。需要注意的是使用pip安裝時(shí)正確的命令是pip install opencc-python-reimplemented,如果使用pip install opencc-python會(huì)出現(xiàn)如下錯(cuò)誤:

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)行自動(dòng)的語言翻譯。

參考鏈接:

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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI