Skip to content

Instantly share code, notes, and snippets.

@fkztw
Last active July 9, 2017 20:15
Show Gist options
  • Save fkztw/71cd6a5d815ad10b467adeb7e08f563c to your computer and use it in GitHub Desktop.
Save fkztw/71cd6a5d815ad10b467adeb7e08f563c to your computer and use it in GitHub Desktop.
在座的各位都是谷歌小姐
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# USAGE: `$ cat $text_file | python3 gtts-chinese.py`
import os
import sys
from gtts import gTTS
SPECIAL_MAPPING = {
'0': '零',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
'7': '七',
'8': '八',
'9': '九',
'+': '加',
'-': '減',
'*': '乘',
'/': '除',
'.': '點',
}
def main():
sentence = sys.stdin.read().replace(' ', '')
print(repr(sentence))
tmp_token = ''
last_char_is_chinese = False
for char in sentence:
print(tmp_token)
if char in SPECIAL_MAPPING:
if not last_char_is_chinese and tmp_token:
tts = gTTS(text=tmp_token, lang='en')
tts.save("tmp.mp3")
os.system("mplayer tmp.mp3")
tmp_token = ''
tmp_token += SPECIAL_MAPPING[char]
last_char_is_chinese = True
continue
elif ord(char) <= 0x7f:
# ASCII Table
if last_char_is_chinese and tmp_token:
tts = gTTS(text=tmp_token, lang='zh-tw')
tts.save("tmp.mp3")
os.system("mplayer tmp.mp3")
tmp_token = ''
if char == '\n':
tmp_token += ' '
last_char_is_chinese = False
continue
if ord('a') <= ord(char) <= ord('z'):
tmp_token += char
last_char_is_chinese = False
continue
if ord('A') <= ord(char) <= ord('Z'):
tmp_token += char
last_char_is_chinese = False
continue
continue
else:
# consider as Chinese
if not last_char_is_chinese and tmp_token:
tts = gTTS(text=tmp_token, lang='en')
tts.save("tmp.mp3")
os.system("mplayer tmp.mp3")
tmp_token = ''
tmp_token += char
last_char_is_chinese = True
else:
if tmp_token:
tts = gTTS(text=tmp_token, lang='em')
tts.save("tmp.mp3")
os.system("mplayer tmp.mp3")
tmp_token = ''
os.system("rm tmp.mp3")
if __name__ == '__main__':
main()
@fkztw
Copy link
Author

fkztw commented Jul 9, 2017

使用範例:echo "市政府嵣嵣嵣起敬護嵣嵣嵣司真輔嵣嵣嵣Taipei City Hall Station嵣嵣嵣左側開門嵣嵣嵣下車時請注意您的隨身行李" | python gtts-chinese.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment