python 实现中文简繁体转化

安装

把这两个文件下载,保存到代码同级目录

https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py

 

翻译代码

from langconv import *
def traditional_to_simplified(sentence):
  '''
  将sentence中的繁体字转为简体字
  :param sentence: 待转换的句子
  :return: 将句子中繁体字转换为简体字之后的句子
  '''
  sentence = Converter('zh-hans').convert(sentence)
  return sentence
def simplified_to_traditional(sentence):
  '''
  将sentence中的简体字转为繁体字
  :param sentence: 待转换的句子
  :return: 将句子中简体字转换为繁体字之后的句子
  '''
  sentence = Converter('zh-hant').convert(sentence)
  return sentence
if __name__=="__main__":
  traditional_sentence = '我愛吃熱乾面'
  simplified_sentence = traditional_to_simplified(traditional_sentence)
  print(simplified_sentence)
  simplified_sentence = "我爱吃方便面"
  traditional_sentence = simplified_to_traditional(simplified_sentence)
  print(traditional_sentence)

  

posted @ 2021-03-26 13:55  RoyFans  阅读(1817)  评论(0编辑  收藏  举报