1、opencc-python
首先介紹opencc中的Python實(shí)現(xiàn)庫(kù),它具有安裝簡(jiǎn)單,翻譯準(zhǔn)確,使用方便等優(yōu)點(diǎn)。對(duì)于我們?nèi)粘5男枨笸耆軌騽偃巍?/p>
源碼/插件點(diǎn)這(https://jq.qq.com/?_wv=1027&k=vIjt1iXV)

1.1安裝opencc-python
首先在terminal中安裝opencc-python。
pip install opencc-python
1.2內(nèi)建的opencc翻譯配置
這里有四種內(nèi)建的opencc翻譯配置:
?t2s - 繁體轉(zhuǎn)簡(jiǎn)體(Traditional Chinese to Simplified Chinese) ?s2t - 簡(jiǎn)體轉(zhuǎn)繁體(Simplified Chinese to Traditional Chinese) ?mix2t - 混合轉(zhuǎn)繁體(Mixed to Traditional Chinese) ?mix2s - 混合轉(zhuǎn)簡(jiǎn)體(Mixed to Simplified Chinese)
1.3簡(jiǎn)繁體轉(zhuǎn)換
import opencc cc = opencc.OpenCC('t2s') print(cc.convert(u'Open Chinese Convert(OpenCC)開放中文轉(zhuǎn)換,是一個(gè)致力於中文簡(jiǎn)繁轉(zhuǎn)換的項(xiàng)目,提供高質(zhì)量詞庫(kù)和函數(shù)庫(kù)(libopencc)。'))
輸出結(jié)果如下:


2、zhtools
2.1安裝
利用Python實(shí)現(xiàn)漢字的簡(jiǎn)體和繁體相互轉(zhuǎn)換的命令也有人開發(fā)過,并發(fā)布到github上,地址:https://github.com/skydark/nstools/tree/master/zhtools。下載該項(xiàng)目中的 zh_wiki.py 和 langconv.py 兩個(gè)文件,放到python代碼目錄下就可以了。
2.2簡(jiǎn)繁體轉(zhuǎn)換
from langconv import Converter def convert(text, flag=0): #text為要轉(zhuǎn)換的文本,flag=0代表簡(jiǎn)化繁,flag=1代表繁化簡(jiǎn) rule = 'zh-hans' if flag else 'zh-hant' return Converter(rule).convert(text) text1 = '悄悄是別離的笙簫; 夏蟲也為我沉默, 沉默是今晚的康橋'print(convert(text1)) text2 = '悄悄是別離的笙簫; 夏蟲也為我沉默, 沉默是今晚的康橋'print(convert(text2, 1))
轉(zhuǎn)換后的結(jié)果為:

該方法的優(yōu)點(diǎn)是輕量,使用方便,簡(jiǎn)潔,但可能翻譯會(huì)不太準(zhǔn)確。
3、zhconv
3.1zhconv安裝
zhconv庫(kù)直接使用pip安裝,安裝命令為:
pip install zhconv
3.2使用方法
zhconv支持以下地區(qū)詞的轉(zhuǎn)換:
zh-cn 大陸簡(jiǎn)體
zh-sg 馬新簡(jiǎn)體(馬來西亞和新加坡使用的簡(jiǎn)體漢字)
zh-tw 臺(tái)灣正體(臺(tái)灣正體)
zh-hk 香港繁體(香港繁體)
zh-hans 簡(jiǎn)體
zh-hant 繁體(繁體)
方法1:直接導(dǎo)入zhconv1
import zhconv text = '此去經(jīng)年,應(yīng)是良辰好景虛設(shè)。便縱有千種風(fēng)情,更與何人說?' text1 = zhconv.convert(text, 'zh-hant') text2 = zhconv.convert(text, 'zh-tw') text3 = zhconv.convert(text, 'zh-hk') print('轉(zhuǎn)換為繁體:', text1) print('轉(zhuǎn)換為臺(tái)灣正體:', text2) print('轉(zhuǎn)換為香港繁體:', text3)
轉(zhuǎn)換結(jié)果為:


方法2:導(dǎo)入zhconv的convert
from zhconv import convert text = '此去經(jīng)年,應(yīng)是良辰好景虛設(shè)。便縱有千種風(fēng)情,更與何人說?' text1 = convert(text, 'zh-hant') print('轉(zhuǎn)換為繁體:', text1)
轉(zhuǎn)換結(jié)果為:

4、文檔的簡(jiǎn)繁體轉(zhuǎn)換
利用擴(kuò)展庫(kù)python-docx,可以將Word文檔中的中文進(jìn)行轉(zhuǎn)換,簡(jiǎn)體轉(zhuǎn)換為繁體:
pip install python-docx
這里我們使用zhconv庫(kù)的方法來將word文檔《匆匆》轉(zhuǎn)換為《匆匆》繁體版:
from zhconv import convert from docx import document word = document('《匆匆》.docx') for t in word.paragraphs: t.text = convert(t.text, 'zh-hant')for i in word.tables: for p in i.rows: for h in p.cells: h.text = convert(h.text, 'zh-hant') word.save('《匆匆》繁體版.docx')
轉(zhuǎn)換前:

轉(zhuǎn)換后:

這樣我們就實(shí)現(xiàn)了將《匆匆》這個(gè)文檔轉(zhuǎn)換為了繁體版。
到此這篇關(guān)于Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python 簡(jiǎn)繁體轉(zhuǎn)換以及其他內(nèi)容請(qǐng)繼續(xù)關(guān)注之后的相關(guān)文章!

最后
我們?yōu)榇蠹医颐匮┣蚓W(wǎng)(https://xueqiu.com/) 最新所展示的滬深證券和港股關(guān)注人數(shù)增長(zhǎng)Top10。

原文地址:https://mp.weixin.qq.com/s?src=http://nmtw.com.cn/skin/st05skin/image/nopic.gif>
原文作者: Stata and Python數(shù)據(jù)分析