def sendto(self, key): """ @function: Send the flac file to Google and start the parser """ # lecture du fichier audio filename = '/tmp/voix_' + self.PID + '.flac' f = open(filename) data = f.read() f.close() # suppression du fichier audio if os.path.exists('/tmp/voix_' + self.PID + '.flac'): os.system('rm /tmp/voix_' + self.PID + '.flac') # fichier de configuration config = expanduser('~') + '/.config/google2ubuntu/google2ubuntu.xml' default = self.p + 'config/' + self.lang + '/default.xml' if os.path.exists(config): config_file = config else: if os.path.exists(expanduser('~') + '/.config/google2ubuntu') == False: os.makedirs(expanduser('~') + '/.config/google2ubuntu') if os.path.exists( expanduser('~') + '/.config/google2ubuntu/modules') == False: os.system('cp -r ' + self.p + '/modules ' + expanduser('~') + '/.config/google2ubuntu') if os.path.exists(default) == False: default = self.p + 'config/en_EN/default.xml' config_file = default print 'config file:', config_file print key try: # envoie une requête à Google #req = urllib2.Request('https://www.google.com/speech-api/v2/recognize?output=json&lang='+self.lang+'&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=chromium', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) req = urllib2.Request( 'https://www.google.com/speech-api/v2/recognize?output=json&lang=' + self.lang + '&key=' + key + '&client=chromium', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) # retour de la requête ret = urllib2.urlopen(req) response = ret.read() response = response.split('\n', 1)[1] text = response.split('"transcript":"', 2)[1].split('"', 2)[0] # parsing du retour #text=json.load(response) os.system('echo "' + text.encode("utf-8") + '" > /tmp/g2u_result_' + self.PID) # parsing du résultat pour trouver l'action sp = stringParser(text, config_file, self.PID) except Exception: message = _('unable to translate') os.system('echo "' + message + '" > /tmp/g2u_error_' + self.PID) sys.exit(1)
def sendto(self): """ @function: Send the flac file to Google and start the parser """ # lecture du fichier audio filename = '/tmp/voix_' + self.PID + '.flac' f = open(filename) data = f.read() f.close() # suppression du fichier audio if os.path.exists('/tmp/voix_' + self.PID + '.flac'): os.system('rm /tmp/voix_' + self.PID + '.flac') # fichier de configuration config = expanduser('~') + '/.config/google2ubuntu/google2ubuntu.xml' default = self.p + 'config/' + self.lang + '/default.xml' if os.path.exists(config): config_file = config else: if os.path.exists(expanduser('~') + '/.config/google2ubuntu') == False: os.makedirs(expanduser('~') + '/.config/google2ubuntu') if os.path.exists( expanduser('~') + '/.config/google2ubuntu/modules') == False: os.system('cp -r ' + self.p + '/modules ' + expanduser('~') + '/.config/google2ubuntu') if os.path.exists(default) == False: default = self.p + 'config/en_EN/default.xml' config_file = default print 'config file:', config_file try: # envoie une requête à Google req = urllib2.Request( 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=' + self.lang, data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) # retour de la requête ret = urllib2.urlopen(req) # before parsing we need to eliminate eventually empty result when sox bug # ret= ((ret.read()).split('}'))[0]+'}]}' # parsing du retour text = json.loads(ret.read())['hypotheses'][0]['utterance'] os.system('echo "' + text.encode("utf-8") + '" > /tmp/g2u_result_' + self.PID) # parsing du résultat pour trouver l'action sp = stringParser(text, config_file, self.PID) except Exception: message = _('unable to translate') os.system('echo "' + message + '" > /tmp/g2u_error_' + self.PID) sys.exit(1)
def getParsedSentList(laws): parsedSentList = [] for law in laws: sents = law['parsed']['sentences'] subList = [] for sent in sents: parsedSent = stringParser(sent['parsetree']) subList.append([str(parsedSent), law["lawID"], law["sentenceID"]]) parsedSentList.append(subList) return parsedSentList
def sendto(self, key): """ @function: Send the flac file to Google and start the parser """ # lecture du fichier audio filename='/tmp/voix_'+self.PID+'.flac' f = open(filename) data = f.read() f.close() # suppression du fichier audio if os.path.exists('/tmp/voix_'+self.PID+'.flac'): os.system('rm /tmp/voix_'+self.PID+'.flac') # fichier de configuration config = expanduser('~') + '/.config/google2ubuntu/google2ubuntu.xml' default = self.p +'config/'+self.lang+'/default.xml' if os.path.exists(config): config_file = config else: if os.path.exists(expanduser('~') +'/.config/google2ubuntu') == False: os.makedirs(expanduser('~') +'/.config/google2ubuntu') if os.path.exists(expanduser('~') +'/.config/google2ubuntu/modules') == False: os.system('cp -r '+self.p+'/modules '+expanduser('~') +'/.config/google2ubuntu') if os.path.exists(default) == False: default = self.p+'config/en_EN/default.xml' config_file = default print 'config file:', config_file print key try: # envoie une requête à Google #req = urllib2.Request('https://www.google.com/speech-api/v2/recognize?output=json&lang='+self.lang+'&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=chromium', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) req = urllib2.Request('https://www.google.com/speech-api/v2/recognize?output=json&lang='+self.lang+'&key='+key+'&client=chromium', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) # retour de la requête ret = urllib2.urlopen(req) response = ret.read() response = response.split('\n', 1)[1] text = response.split('"transcript":"',2)[1].split('"',2)[0] # parsing du retour #text=json.load(response) os.system('echo "'+text.encode("utf-8")+'" > /tmp/g2u_result_'+self.PID) # parsing du résultat pour trouver l'action sp = stringParser(text,config_file,self.PID) except Exception: message = _('unable to translate') os.system('echo "'+message+'" > /tmp/g2u_error_'+self.PID) sys.exit(1)
def sendto(self): """ @function: Send the flac file to Google and start the parser """ # lecture du fichier audio filename='/tmp/voix_'+self.PID+'.flac' f = open(filename) data = f.read() f.close() # suppression du fichier audio if os.path.exists('/tmp/voix_'+self.PID+'.flac'): os.system('rm /tmp/voix_'+self.PID+'.flac') # fichier de configuration config = expanduser('~') + '/.config/google2ubuntu/google2ubuntu.xml' default = self.p +'config/'+self.lang+'/default.xml' if os.path.exists(config): config_file = config else: if os.path.exists(expanduser('~') +'/.config/google2ubuntu') == False: os.makedirs(expanduser('~') +'/.config/google2ubuntu') if os.path.exists(expanduser('~') +'/.config/google2ubuntu/modules') == False: os.system('cp -r '+self.p+'/modules '+expanduser('~') +'/.config/google2ubuntu') if os.path.exists(default) == False: default = self.p+'config/en_EN/default.xml' config_file = default print 'config file:', config_file try: # envoie une requête à Google req = urllib2.Request('https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang='+self.lang, data=data, headers={'Content-type': 'audio/x-flac; rate=16000'}) # retour de la requête ret = urllib2.urlopen(req) # before parsing we need to eliminate eventually empty result when sox bug # ret= ((ret.read()).split('}'))[0]+'}]}' # parsing du retour text=json.loads(ret.read())['hypotheses'][0]['utterance'] os.system('echo "'+text.encode("utf-8")+'" > /tmp/g2u_result_'+self.PID) # parsing du résultat pour trouver l'action sp = stringParser(text,config_file,self.PID) except Exception: message = _('unable to translate') os.system('echo "'+message+'" > /tmp/g2u_error_'+self.PID) sys.exit(1)