client_nict.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 import urllib2
00004 import json
00005 import base64
00006 import socket
00007 import logging
00008 import traceback
00009 import ssl
00010 
00011 # import library
00012 from rospeex_core.validators import accepts, check_wave_data, check_language
00013 from rospeex_core.exceptions import InvalidRequestException
00014 from rospeex_core.exceptions import InvalidResponseException
00015 from rospeex_core.exceptions import SpeechRecognitionException
00016 from rospeex_core.exceptions import RequestTimeoutException
00017 from rospeex_core.sr.client_base import SpeechRecognitionClient
00018 
00019 # create logger
00020 logging.basicConfig(level=logging.INFO)
00021 logger = logging.getLogger(__name__)
00022 
00023 
00024 class SpeechRecognitionClient_NICT(SpeechRecognitionClient):
00025     """ SpeechRecognitionClient_NICT class """
00026 
00027     AUDIO_LENGTH = 16000
00028     FRAMERATE = 16000
00029     CHANNELS = 1
00030     SAMPWIDTH = 2
00031     URL = 'http://rospeex.ucri.jgn-x.jp/nauth_json/jsServices/VoiceTraSR'
00032     LANGUAGES = ['ja', 'en', 'zh', 'ko']
00033 
00034 
00035     @accepts(data=str, language=str, timeout=int)
00036     def request(self, data, language='ja', timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
00037         """
00038         send speech recognition request to server
00039         @param data: speech binary data
00040         @type  data: str
00041         @param language: speech data language
00042         @type  language: str
00043         @param timeout: time out time[ms]
00044         @type  timeout: int
00045         """
00046         check_wave_data(data, self.FRAMERATE, self.CHANNELS, self.SAMPWIDTH, self.AUDIO_LENGTH)
00047         check_language(language, self.LANGUAGES)
00048 
00049         # load voice data and encord to base64
00050         voice_send = base64.b64encode(data)
00051 
00052         data = {
00053             'method':'recognize',
00054             'params':[
00055                 '1.1',
00056                 {'audio': voice_send,
00057                  'audioType':'audio/x-wav',
00058                  'voiceType': '*',
00059                  'language':language,
00060                  'applicationType':'rospeex'
00061                 }
00062             ]
00063         }
00064 
00065         # サーバにリクエストを送信する
00066         res_data = None
00067         try:
00068             req = urllib2.Request(self.URL)
00069             req.add_header('Content-Type', 'application/json')
00070             res = urllib2.urlopen(req, json.dumps(data), timeout=timeout)
00071             res_data = json.loads(res.read())
00072 
00073         except urllib2.URLError as err:
00074             if isinstance(err.reason, socket.timeout):
00075                 raise RequestTimeoutException('request time out. Exception: %s' % str(err))
00076             raise InvalidRequestException('request url error. Exception: %s' % str(err))
00077 
00078         except urllib2.HTTPError as err:
00079             raise InvalidResponseException('http error. %s Exception:%s' % (err.code, err.msg))
00080 
00081         except (ssl.SSLError, socket.timeout) as err:
00082             raise RequestTimeoutException(str(err))
00083 
00084         except:
00085             raise SpeechRecognitionException('unknown exception. Traceback: %s' % traceback.format_exc())
00086 
00087         # 結果の取得
00088         if res_data['error'] is not None:
00089             if res_data['error'].has_key('faultCode'):
00090                 if res_data['error']['faultCode'] == 'Server.userException':
00091                     raise InvalidResponseException('the format is not supported.')
00092             raise InvalidResponseException('server response error. msg:%s'%res_data['error'])
00093 
00094         return res_data['result']


rospeex_core
Author(s): Komei Sugiura
autogenerated on Wed Aug 26 2015 16:10:30