pinyin_utils.py 394 B

123456789101112131415161718
  1. import re
  2. from pypinyin import lazy_pinyin
  3. # 替换同音字
  4. def replace_word(text, target_word):
  5. words = re.findall(r'\b\w+\b', text)
  6. for word in words:
  7. if is_same_pinyin(word, target_word):
  8. text = text.replace(word, target_word)
  9. return text
  10. # 判断拼音是否相同
  11. def is_same_pinyin(word1, word2):
  12. return lazy_pinyin(word1) == lazy_pinyin(word2)