def test_weather(): global newcommand, oldResult, output core.register('weather',devices.weather) expected_output_1 = { "arguments": {}, "device": "weather", "intent": 'will' } expected_output_2 = { "arguments": { 'name' : ' london' }, "device": "weather", "intent": 'set city' } expected_output_3 = { "arguments": {}, "device": "weather", "intent": 'maxTemperature' } expected_output_4 = { "arguments": {}, "device": "weather", "intent": 'weather' } actual_output, device, output = core.parse('forecast will it rain tomorrow', newcommand, oldResult, output) assert expected_output_1 == actual_output actual_output, device, output = core.parse('forecast set city london', newcommand, oldResult, output) print(actual_output) assert expected_output_2 == actual_output actual_output, device, output = core.parse('forecast max temperature', newcommand, oldResult, output) assert expected_output_3 == actual_output actual_output, device, output = core.parse('forecast what will be the weather tomorrow', newcommand, oldResult, output) assert expected_output_4 == actual_output print('finished testing')
def command(): ''' Recevies commands from the client, parses it and takes appropriate action. ''' # This object will be sent to the client output = { 'commands': [], 'error': False, # Used in the catch block 'final': True, # In the client we'll know if some more information is needed or if the command has been executed 'parsed': {}, # Whatever has been parsed so far 'message': '', # The message to be shown for interactive mode 'type': None, # Type of the next command is. Used if final is False. # Can be one of confirm, option, intent, argument, 'matched':False, # After selection of an option from a list matched is set to True } # "data" from $.ajax will be in request.form command = request.form['input'] # The input command newCommand = request.form['newCommand'] # Flag indicating whether the command is new or a continuation currentSession = request.form['currentSession'] # The device that is currently running oldResult = json.loads(request.form['oldResult']) # Any old results if the command is a continuation output['commands'].append(command) try: result, device, output = core.parse(command, newCommand, oldResult, currentSession, output) output['parsed'] = result if 'dont_execute' in output.keys(): return jsonify(output) if output['parsed']['intent'] == None: # no intent given, so ask user to give one output['final'] = False output['type'] = 'continue' # user needs to provide the correct command output['example'] = device['operations']['examples_intent']['arguments']['example'] # provide the required message and output['message'] = device['operations']['examples_intent']['arguments']['message'] # example in devices.py return jsonify(output) # If the operation requires user confirmation if device['operations'][result['intent']]['confirm'] == True: # If the user has told "no" if 'cancel' in output.keys(): return jsonify(output) output['final'] = False output['type'] = 'confirm' output['message'] = device['operations'][result['intent']]['message'] output['parsed'] = result return jsonify(output) else: output = execute.process(result, device, output) return jsonify(output) except: output = { 'error': True, 'final': True, 'message': 'Something went wrong, please try again' } return jsonify(output)
def test_totem(): global newcommand, oldResult, output core.register('totem', devices.totem) expected_output_1 = { "arguments": { "name": " devices" }, "device": "totem", "intent": "--play" } expected_output_2 = { "arguments": {}, "device": "totem", "intent": "--pause" } expected_output_3 = { "arguments": "", "device": "totem", "intent": None } expected_output_4 = { "arguments": {}, "device": "totem", "intent": "--quit" } expected_output_5 = { "arguments":{}, "device":"totem", "intent":"--next" } actual_output, device, output = core.parse('totem play devices', newcommand, oldResult, output) assert expected_output_1 == actual_output actual_output, device, output = core.parse('totem pause', newcommand, oldResult, output) assert expected_output_2 == actual_output actual_output, device, output = core.parse('totem',newcommand, oldResult, output) assert expected_output_3 == actual_output actual_output, device, output = core.parse('totem quit',newcommand, oldResult, output) assert expected_output_4 == actual_output actual_output, device, output = core.parse('totem next',newcommand, oldResult, output) assert expected_output_5 == actual_output print('finished testing')
def test_tweets(): global newcommand, oldResult, output core.register('tweet', devices.tweet) expected_output_1 = { "arguments": { "name": " google" }, "device": "tweet", "intent": "search/tweets" } expected_output_2 = { "arguments": { "name": " india" }, "device": "tweet", "intent": "trends/place" } expected_output_3 = { "arguments": { "name": " rcbtweets" }, "device": "tweet", "intent": "statuses/user_timeline" } expected_output_4 = { "arguments": "", "device": "tweet", "intent": None } actual_output, device, output = core.parse('tweets on google', newcommand, oldResult, output) assert expected_output_1 == actual_output actual_output, device, output = core.parse('tweets in india', newcommand, oldResult, output) assert expected_output_2 == actual_output actual_output, device, output = core.parse('tweets by rcbtweets', newcommand, oldResult, output) assert expected_output_3 == actual_output actual_output, device, output = core.parse('tweets', newcommand, oldResult, output) assert expected_output_4 == actual_output print('finished testing')
def command(): ''' Recevies commands from the client, parses it and takes appropriate action. ''' # This object will be sent to the client output = { 'commands': [], 'error': False, # Used in the catch block 'final': True, # In the client we'll know if some more information is needed or if the command has been executed 'parsed': {}, # Whatever has been parsed so far 'message': '', # The message to be shown for interactive mode 'type': None, # Type of the next command is. Used if final is False. # Can be one of confirm, option, intent, argument, 'matched': False, # After selection of an option from a list matched is set to True } # "data" from $.ajax will be in request.form command = request.form['input'] # The input command newCommand = request.form[ 'newCommand'] # Flag indicating whether the command is new or a continuation currentSession = request.form[ 'currentSession'] # The device that is currently running oldResult = json.loads( request.form['oldResult'] ) # Any old results if the command is a continuation output['commands'].append(command) try: result, device, output = core.parse(command, newCommand, oldResult, currentSession, output) output['parsed'] = result if 'dont_execute' in output.keys(): return jsonify(output) if output['parsed'][ 'intent'] == None: # no intent given, so ask user to give one output['final'] = False output[ 'type'] = 'continue' # user needs to provide the correct command output['example'] = device['operations']['examples_intent'][ 'arguments']['example'] # provide the required message and output['message'] = device['operations']['examples_intent'][ 'arguments']['message'] # example in devices.py return jsonify(output) # If the operation requires user confirmation if device['operations'][result['intent']]['confirm'] == True: # If the user has told "no" if 'cancel' in output.keys(): return jsonify(output) output['final'] = False output['type'] = 'confirm' output['message'] = device['operations'][ result['intent']]['message'] output['parsed'] = result return jsonify(output) else: output = execute.process(result, device, output) return jsonify(output) except: output = { 'error': True, 'final': True, 'message': 'Something went wrong, please try again' } return jsonify(output)