1
2
3
4
5
6
7 import re
8 import globalVars
9 from logHandler import log
10 import os
11 import codecs
12 import synthDriverHandler
13 import api
14 import config
15
16 dictionaries = {}
17 dictTypes = ("temp", "voice", "default", "builtin")
18 speechDictsPath=os.path.join(globalVars.appArgs.configPath, "speechDicts")
19
21
22 - def __init__(self, pattern, replacement,comment,caseSensitive=True,regexp=False):
23 self.pattern = pattern
24 flags = re.U
25 if not caseSensitive: flags|=re.IGNORECASE
26 tempPattern=pattern if regexp else re.escape(pattern)
27 self.compiled = re.compile(tempPattern,flags)
28 self.replacement = replacement
29 self.comment=comment
30 self.caseSensitive=caseSensitive
31 self.regexp=regexp
32
33 - def sub(self, text):
34 replacement=self.replacement
35 return self.compiled.sub(replacement, text)
36
38
39 - def load(self, fileName):
40 self.fileName=fileName
41 comment=""
42 del self[:]
43 log.debug("Loading speech dictionary '%s'..." % fileName)
44 if not os.path.isfile(fileName):
45 log.debug("file '%s' not found." % fileName)
46 return
47 file = codecs.open(fileName,"r","utf_8_sig",errors="replace")
48 for line in file:
49 if line.isspace():
50 comment=""
51 continue
52 line=line.rstrip('\r\n')
53 if line.startswith('#'):
54 if comment:
55 comment+=" "
56 comment+=line[1:]
57 else:
58 temp=line.split("\t")
59 if len(temp) ==4:
60 self.append(SpeechDictEntry(temp[0],temp[1],comment,bool(int(temp[2])),bool(int(temp[3]))))
61 comment=""
62 else:
63 log.warning("can't parse line '%s'" % line)
64 log.debug("%d loaded records." % len(self))
65 file.close()
66 return
67
68 - def save(self,fileName=None):
69 if not fileName:
70 fileName=getattr(self,'fileName',None)
71 if not fileName:
72 return
73 dirName=os.path.dirname(fileName)
74 if not os.path.isdir(dirName):
75 os.makedirs(dirName)
76 file = codecs.open(fileName,"w","utf_8_sig",errors="replace")
77 for entry in self:
78 if entry.comment:
79 file.write("#%s\r\n"%entry.comment)
80 file.write("%s\t%s\t%s\t%s\r\n"%(entry.pattern,entry.replacement,int(entry.caseSensitive),int(entry.regexp)))
81 file.close()
82
83 - def sub(self, text):
84 for entry in self:
85 text = entry.sub(text)
86 return text
87
88 -def processText(text):
89 if not globalVars.speechDictionaryProcessing:
90 return text
91 for type in dictTypes:
92 text=dictionaries[type].sub(text)
93 return text
94
100
111