-
Notifications
You must be signed in to change notification settings - Fork 0
/
say.py
51 lines (40 loc) · 1.22 KB
/
say.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
from os import system
VOICE = 'Alva'
SPEED = 200
PRONUNCIATION_FILE = 'pronunciation/sv.json'
do_fix_pronunciation = True
pronunciation_words = {}
def say(text):
global pronunciation_words
if not pronunciation_words:
if PRONUNCIATION_FILE:
set_pronunciation_data(PRONUNCIATION_FILE)
if do_fix_pronunciation:
text = pronunciation_fix(text)
system('say -v {0} {1} -r {2}'.format(VOICE, text, SPEED))
def pronunciation_fix(text):
global pronunciation_words
for i, j in pronunciation_words.iteritems():
text = text.replace(i, j)
return text
def set_pronunciation_data(filename):
global pronunciation_words
with open(filename) as data_file:
data = json.load(data_file)
data = byteify(data)
pronunciation_words = data
def byteify(data):
if isinstance(data, dict):
return {byteify(key): byteify(value) for key, value in data.iteritems()}
elif isinstance(data, list):
return [byteify(element) for element in data]
elif isinstance(data, unicode):
return data.encode('utf-8')
else:
return data
if __name__ == '__main__':
say(sys.argv[1])