python's zhconv library provides the maximum forward matching simplified and traditional conversion based on the MediaWiki vocabulary. Both pyhton2 and python3 support it, which can meet simple conversion needs.
The following summarizes the method of zhconv from installation to use (python3).
First, zhconv installation
The zhconv library can be installed directly using pip, the installation command:
pip install zhconv
Second, how to use zhconv
zhconv supports the conversion of the following regional words:
zh-cn Mainland Simplified
zh-sg Malaysia-Singapore Simplified (Simplified Chinese characters used in Malaysia and Singapore)
zh-tw Taiwanese (Taiwanese)
zh-hk Hong Kong Traditional (Hong Kong Traditional)
zh-hans Simplified
zh-hant Traditional (Traditional)
Method 1: Import zhconv directly
import zhconv# Simplified and Traditional Conversions = 'In front of the Dou Tan, the mountains and the moon are secluded, and Shixiong has dreams and sorrows.When Zhanger looks at Nanxue, Meihua and I have two white heads.'s1 = zhconv.convert(s, 'zh-hant')s2 = zhconv.convert(s, 'zh-tw')s3 = zhconv.convert(s, 'zh-hk')print('Convert to traditional results:', s1)print('Convert to Taiwanese text:', s2)print('Convert to Hong Kong Traditional Results:', s3)
Transformed into traditional Chinese and the result: The moon is secluded in the mountains in front of the Dou Tan, and Shixiong has a dream and a clear sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.
Translated into Taiwanese orthodox results: the mountains and moons are secluded in front of the Chaodou Temple, and Shixiong's dreams are born of pure sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.
Translated into Hong Kong Traditional Result: The mountains and the moon are secluded in front of the Chaodou Altar, and Shixiong has a dream and a clear sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.
Using Method 2: Import zhconv's convert
from zhconv import converts = 'No one accompanies me Gu Xingchen, no one wakes me up and my tea is cold.'s1 = convert(s, 'zh-hant')print('Convert to traditional results:', s1)
Translated into traditional Chinese results: No one accompanies me Gu Xingchen, no one wakes me up and my tea is cold.
—end—