1、opencc-python
首先介紹opencc中的Python實作庫,它具有安裝簡單,翻譯準確,使用方便等優點,對于我們日常的需求完全能夠勝任,

1.1安裝opencc-python
首先在terminal中安裝opencc-python,
pip install opencc-python
1.2內建的opencc翻譯配置
這里有四種內建的opencc翻譯配置:
?t2s - 繁體轉簡體(Traditional Chinese to Simplified Chinese) ?s2t - 簡體轉繁體(Simplified Chinese to Traditional Chinese) ?mix2t - 混合轉繁體(Mixed to Traditional Chinese) ?mix2s - 混合轉簡體(Mixed to Simplified Chinese)
1.3簡繁體轉換
import opencc Python插件/素材/.原始碼Q群:903971231#### cc = opencc.OpenCC('t2s') print(cc.convert(u'Open Chinese Convert(OpenCC)開放中文轉換,是一個致力於中文簡繁轉換的項目,提供高質量詞庫和函數庫(libopencc),'))
輸出結果如下:

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

該方法的優點是輕量,使用方便,簡潔,但可能翻譯會不太準確,
3、zhconv
3.1zhconv安裝
zhconv庫直接使用pip安裝,安裝命令為:
pip install zhconv
3.2使用方法
zhconv支持以下地區詞的轉換:
zh-cn 大陸簡體
zh-sg 馬新簡體(馬來西亞和新加坡使用的簡體漢字)
zh-tw 臺灣正體(臺灣正體)
zh-hk 香港繁體(香港繁體)
zh-hans 簡體
zh-hant 繁體(繁體)
方法1:直接匯入zhconv1
import zhconv text = '此去經年,應是良辰好景虛設,便縱有千種風情,更與何人說?' text1 = zhconv.convert(text, 'zh-hant') text2 = zhconv.convert(text, 'zh-tw') text3 = zhconv.convert(text, 'zh-hk') print('轉換為繁體:', text1) print('轉換為臺灣正體:', text2) print('轉換為香港繁體:', text3)
轉換結果為:


方法2:匯入zhconv的convert
from zhconv import convert text = '此去經年,應是良辰好景虛設,便縱有千種風情,更與何人說?' text1 = convert(text, 'zh-hant') print('轉換為繁體:', text1)
轉換結果為:
4、檔案的簡繁體轉換
利用擴展庫python-docx,可以將Word檔案中的中文進行轉換,簡體轉換為繁體:
pip install python-docx
這里我們使用zhconv庫的方法來將word檔案《匆匆》轉換為《匆匆》繁體版:
Python原始碼/素材/解答Q群:903971231### 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')
轉換前:

轉換后:

這樣我們就實作了將《匆匆》這個檔案轉換為了繁體版,
到此這篇關于Python實作簡繁體轉換的文章就介紹到這了,更多相關Python 簡繁體轉換以及其他內容請繼續關注之后的相關文章!

最后
我們為大家揭秘雪球網(https://xueqiu.com/) 最新所展示的滬深證券和港股關注人數增長Top10,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495460.html
標籤:Python
上一篇:Python實作秒殺某寶商品搶購
