見出し画像

繁体字にしたい

(Python学習初心者の試行錯誤・備忘録です)
台湾で使われる「中国語(台湾華語)」の表記は「繁体字」の漢字です。「簡体字」表記の文書を「繁体字」にしたい場面は結構あります。で、Pythonでそういう「変換」の処理をできないか?
 例によってCopilotに聞くと

chinese-converter


OpenCC


hanziconv

TEST

3つとも、と思ったんですが、最新バージョンのリリース日を見たら、
hanziconv が2016年9月1日
chineseーconverter が2023年9月4日
OpenCCが 2023年10月16日
とあるため、古めのhanziconvには手を出さないことにしました。

pipでchineseーconverter、OpenCCの2つをインストール。
前回
のテストコードに、繁体字変換を二通り用意して

import chinese_converter
import opencc
occconverter = opencc.OpenCC('s2t.json')

def mytranslate(lang,word,tolang):
    from PyMultiDictionary import MultiDictionary
    dictionary = MultiDictionary()
    templist = dictionary.translate(lang, word)
    result = None
    for templang, temptrans in templist:
        if templang == tolang:
            result =temptrans
            break
    return(result)

words = ['people','checked','baggage','late','mainly','active']
for word in words:
    tempzh =mytranslate('en',word,'zh')
    print (word, mytranslate('en',word,'ja'),tempzh,\
           occconverter.convert(tempzh),\
           chinese_converter.to_traditional(tempzh))

結果は

people 人々 人们 人們 人們
checked チェックの 有格子花的 有格子花的 有格子花的
baggage 手荷物 行李 行李 行李
late 遅れた 迟的 遲的 遲的
mainly 主に 大体上 大體上 大體上
active 活動的な 活跃的 活躍的 活躍的

繁体字一つ目がOpenCC 二つ目がchinese_converter この例の限りではどちらも正常に動作しているようです。

この記事が気に入ったらサポートをしてみませんか?