程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes the conversion between simple and traditional Chinese, which is really fun

編輯:Python

Hello, everyone , I'm a panda

Directly !!!

1、opencc-python

First introduced opencc Medium Python Implementation library , It has simple installation , The translation is accurate , Easy to use and other advantages . We are fully qualified for our daily needs .

1.1 install opencc-python

First, in the terminal Install in opencc-python.

pip install opencc-python

1.2 The built-in opencc Translation configuration

There are four built-in opencc Translation configuration :

•t2s - Traditional to simplified (Traditional Chinese to Simplified Chinese)
•s2t - From simplified Chinese to traditional Chinese (Simplified Chinese to Traditional Chinese)
•mix2t - Mixed to traditional (Mixed to Traditional Chinese)
•mix2s - Mixed to simplified (Mixed to Simplified Chinese)

1.3 Simple traditional conversion

import opencc Python plug-in unit / material /. Source code Q Group :660193417 ####
cc = opencc.OpenCC('t2s')
print(cc.convert(u'Open Chinese Convert(OpenCC) Open Chinese conversion , It is a project dedicated to the conversion of simplified and complex Chinese , Provide high-quality thesaurus and function library (libopencc).'))

The output is as follows :

2、zhtools

2.1 install

utilize Python Some people have also developed commands to realize the conversion between simplified and traditional Chinese characters , And release to the github On , Address :https://github.com/skydark/nstools/tree/master/zhtools. Download... From this project zh_wiki.py and langconv.py Two documents , Put it in python Just under the code directory .

2.2 Simple traditional conversion

from langconv import Converter
def convert(text, flag=0): #text For the text to be converted ,flag=0 Represents simplification and complexity ,flag=1 Represents complexity and simplification
rule = 'zh-hans' if flag else 'zh-hant'
return Converter(rule).convert(text)
text1 = ' Quietly is the farewell music ; Summer insects are silent for me , Silence is Cambridge tonight 'print(convert(text1))
text2 = ' Silence is the farewell Sheng Xiao ; Summer insects are silent for me , Silence is Cambridge tonight 'print(convert(text2, 1))

The converted result is :

This method has the advantage of light weight , Easy to use , concise , But the translation may not be accurate .

3、zhconv

3.1zhconv install

zhconv The library is used directly pip install , The installation command is :

pip install zhconv

3.2 Usage method

zhconv Support the conversion of the following regional words :

  • zh-cn Continental simplified
  • zh-sg Maxine simplified ( Simplified Chinese characters used in Malaysia and Singapore )
  • zh-tw Taiwan orthodox ( Taiwan orthodox )
  • zh-hk Traditional Chinese in Hong Kong ( Traditional Chinese in Hong Kong )
  • zh-hans Simplified Chinese character
  • zh-hant Traditional ( traditional Chinese character )

Method 1: Direct import zhconv1

import zhconv
text = ' This go to years , Should be a good time . There are thousands of styles , More with who said ?'
text1 = zhconv.convert(text, 'zh-hant')
text2 = zhconv.convert(text, 'zh-tw')
text3 = zhconv.convert(text, 'zh-hk')
print(' Convert to traditional Chinese :', text1)
print(' Convert to Taiwanese orthodox :', text2)
print(' Convert to Hong Kong traditional :', text3)

The result of the conversion is :

Method 2: Import zhconv Of convert

from zhconv import convert
text = ' This go to years , Should be a good time . There are thousands of styles , More with who said ?'
text1 = convert(text, 'zh-hant')
print(' Convert to traditional Chinese :', text1)

The result of the conversion is :

4、 Simple and traditional conversion of documents

Leverage extension Libraries python-docx, Can be Word Convert the Chinese in the document , Convert simplified to traditional :

pip install python-docx

Here we use zhconv Library to put word file 《 hurriedly 》 Convert to 《 hurriedly 》 Traditional Chinese version :


from zhconv import convert
from docx import Document
word = Document('《 hurriedly 》.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('《 hurriedly 》 Traditional Chinese version .docx')

Before conversion :

After the transformation :

In this way, we will achieve 《 hurriedly 》 This document is converted to traditional Chinese version .

This is about Python This is the end of the article to realize the transformation between simple and traditional Chinese , More about Python Please continue to pay attention to the following related articles for the conversion between simplified and traditional Chinese and other contents !

I'm a panda , I'll see you in the next article


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved