Module speechDictHandler
[hide private]
[frames] | no frames]

Source Code for Module speechDictHandler

  1  #speechDictHandler.py 
  2  #A part of NonVisual Desktop Access (NVDA) 
  3  #Copyright (C) 2006-2007 NVDA Contributors <http://www.nvda-project.org/> 
  4  #This file is covered by the GNU General Public License. 
  5  #See the file COPYING for more details. 
  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") # ordered by their priority E.G. voice specific speech dictionary is processed before the default 
 18  speechDictsPath=os.path.join(globalVars.appArgs.configPath, "speechDicts") 
 19   
20 -class SpeechDictEntry:
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
37 -class SpeechDict(list):
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
95 -def initialize():
96 for type in dictTypes: 97 dictionaries[type]=SpeechDict() 98 dictionaries["default"].load(os.path.join(speechDictsPath, "default.dic")) 99 dictionaries["builtin"].load("builtin.dic")
100
101 -def loadVoiceDict(synth):
102 """Loads appropriate dictionary for the given synthesizer. 103 It handles case when the synthesizer doesn't support voice setting. 104 """ 105 if synth.isSupported("voice"): 106 voiceName = synth.availableVoices[synth.voice].name 107 fileName=r"%s\%s-%s.dic"%(speechDictsPath,synth.name,api.filterFileName(voiceName)) 108 else: 109 fileName=r"%s\%s.dic"%(speechDictsPath,synth.name) 110 dictionaries["voice"].load(fileName)
111