Go to the documentation of this file.00001
00002
00003
00004
00005 import rospy
00006 from docomo_perception.srv import (SentenseUnderstanding, SentenseUnderstandingResponse)
00007
00008 import json
00009 import requests
00010 import yaml
00011 import threading
00012
00013 global APIHOST, APIKEY, APPKEY, CLIENTVER
00014 settings_path = "/var/lib/robot/docomo_sentense_understanding_settings.yaml"
00015
00016
00017 class DocomoSentenseUnderstandingNode(object):
00018 def __init__(self):
00019 self.srv = rospy.Service("sentense_understanding", SentenseUnderstanding, self.serviceCallback)
00020 self.lock = threading.Lock()
00021
00022 def serviceCallback(self, req):
00023 dic = {
00024 "projectKey": "OSU",
00025 "appInfo": {
00026 "appKey": APPKEY
00027 },
00028 "clientVer": CLIENTVER,
00029 "language": "ja",
00030 "userUtterance": {
00031 "utteranceText": req.text
00032 }
00033 }
00034 sendData = json.dumps(dic, sort_keys=False, ensure_ascii=False, indent=2)
00035 sendData = "json=" + sendData
00036 rospy.loginfo("sendData: %s", sendData)
00037 params = {
00038 "APIKEY": APIKEY
00039 }
00040 headers = {
00041 "Content-type": "application/x-www-form-urlencoded"
00042 }
00043
00044 self.lock.acquire()
00045 urlres = requests.post(APIHOST, params=params, headers=headers, data=sendData)
00046 self.lock.release()
00047
00048 rospy.loginfo("sent request %s -> %d", urlres.url, urlres.status_code)
00049 rospy.loginfo("content: %s", urlres.content)
00050 if urlres.status_code is not requests.codes.ok:
00051 rospy.logerr("bad status code: %d", urlres.status_code)
00052 return SentenseUnderstandingResponse()
00053 resdic = json.loads(urlres.content)
00054 res = SentenseUnderstandingResponse()
00055 res.command = resdic["dialogStatus"]["command"]["commandName"]
00056 res.revisedText = resdic["userUtterance"]["utteranceRevised"]
00057 res.words = resdic["userUtterance"]["utteranceWord"]
00058 if resdic["dialogStatus"].has_key("slotStatus"):
00059 res.args = [ s["slotValue"] for s in resdic["dialogStatus"]["slotStatus"] ]
00060 res.extractedWords = [ s['wordsValue'] for s in resdic['extractedWords'] ]
00061 return res
00062
00063 def load_sentense_understanding_settings():
00064 global APIHOST, APIKEY, APPKEY, APPKEY, CLIENTVER
00065
00066 try:
00067 with open(settings_path) as f:
00068 key = yaml.load(f)
00069 APIHOST = key['APIHOST']
00070 APIKEY = key['APIKEY']
00071 APPKEY = key['APPKEY']
00072 CLIENTVER = key['CLIENTVER']
00073 rospy.loginfo("loaded settings")
00074 except IOError as e:
00075 rospy.logerr('"%s" not found : %s' % (settings_path, e))
00076 exit(-1)
00077 except Exception as e:
00078 rospy.logerr("failed to load settings: %s", e)
00079 rospy.logerr("check if exists valid setting file in %s", settings_path)
00080 exit(-1)
00081
00082 if __name__ == '__main__':
00083 rospy.init_node("docomo_sentense_understanding")
00084 n = DocomoSentenseUnderstandingNode()
00085 load_sentense_understanding_settings()
00086 rospy.spin()