2015-08-20 06:58:19 +00:00
|
|
|
import shlex
|
|
|
|
|
|
|
|
def add(x=None, y=None, *trash):
|
|
|
|
try:
|
|
|
|
return float(x) + float(y)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return
|
|
|
|
|
|
|
|
def multiply(x=None, y=None, *trash):
|
|
|
|
try:
|
|
|
|
return float(x) * float(y)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return
|
|
|
|
|
|
|
|
def exponent(x=None, y=None, *trash):
|
|
|
|
try:
|
|
|
|
return float(x) ** float(y)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return
|
|
|
|
|
|
|
|
def join(text=None, spacer=None, *trash):
|
|
|
|
if None in (text, spacer):
|
|
|
|
return
|
|
|
|
return spacer.join(text.split(' '))
|
|
|
|
|
2015-08-21 06:09:50 +00:00
|
|
|
def whatever(*trash):
|
2015-08-20 06:58:19 +00:00
|
|
|
return 'Look what I found: ' + str(trash)
|
|
|
|
|
|
|
|
|
|
|
|
FUNCTION_MAP = {
|
|
|
|
'add': add,
|
|
|
|
'multiply': multiply,
|
|
|
|
'exponent': exponent,
|
|
|
|
'join': join
|
|
|
|
}
|
2015-08-21 06:09:50 +00:00
|
|
|
DEFAULT_FUNCTION = whatever
|
|
|
|
FUNCTION_MAP = {c.lower():FUNCTION_MAP[c] for c in FUNCTION_MAP}
|
2015-08-20 06:58:19 +00:00
|
|
|
|
|
|
|
COMMAND_IDENTIFIERS = ['robot!']
|
|
|
|
|
|
|
|
SAMPLE_COMMENTS = [
|
|
|
|
'robot! add 1 2',
|
|
|
|
'I want robot! multiply 4 2 trash trash trash',
|
|
|
|
'robot! blank',
|
|
|
|
'robot!',
|
|
|
|
'robot! add 7 19',
|
|
|
|
'robot! exponent 7 2',
|
|
|
|
'robot! join "one two three" "-"',
|
|
|
|
'robot! add 10 10 robot! multiply 10 10 robot! exponent 10 10',
|
|
|
|
'robot! add 1 1\nrobot! multiply 1 1',
|
|
|
|
' do ROBOT! add boom box',
|
2015-08-20 07:02:04 +00:00
|
|
|
'robot! robot!',
|
2015-08-21 06:09:50 +00:00
|
|
|
'robot! add'
|
2015-08-20 06:58:19 +00:00
|
|
|
'']
|
|
|
|
|
|
|
|
COMMAND_IDENTIFIERS = [c.lower() for c in COMMAND_IDENTIFIERS]
|
|
|
|
|
2015-08-21 06:09:50 +00:00
|
|
|
def functionmap_line(text):
|
2015-08-20 06:58:19 +00:00
|
|
|
print('User said:', text)
|
|
|
|
elements = shlex.split(text)
|
|
|
|
print('Broken into:', elements)
|
2015-08-21 06:09:50 +00:00
|
|
|
results = []
|
2015-08-20 06:58:19 +00:00
|
|
|
for element_index, element in enumerate(elements):
|
|
|
|
if element.lower() not in COMMAND_IDENTIFIERS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
arguments = elements[element_index:]
|
|
|
|
assert arguments.pop(0).lower() in COMMAND_IDENTIFIERS
|
|
|
|
|
|
|
|
# If the user has multiple command calls on one line
|
|
|
|
# (Which is stupid but they might do it anyway)
|
|
|
|
# Let's only process one at a time please.
|
|
|
|
for argument_index, argument in enumerate(arguments):
|
|
|
|
if argument.lower() in COMMAND_IDENTIFIERS:
|
|
|
|
arguments = arguments[:argument_index]
|
|
|
|
break
|
|
|
|
|
|
|
|
print('Found command:', arguments)
|
|
|
|
if len(arguments) == 0:
|
|
|
|
print('Did nothing')
|
|
|
|
continue
|
|
|
|
|
2015-08-21 06:09:50 +00:00
|
|
|
command = arguments[0].lower()
|
|
|
|
actual_function = command in FUNCTION_MAP
|
|
|
|
function = FUNCTION_MAP.get(command, DEFAULT_FUNCTION)
|
2015-08-20 06:58:19 +00:00
|
|
|
print('Using function:', function.__name__)
|
|
|
|
|
2015-08-21 06:09:50 +00:00
|
|
|
if actual_function:
|
|
|
|
# Currently, the first argument is the name of the command
|
|
|
|
# If we found an actual function, we can remove that
|
|
|
|
# (because add() doesn't need "add" as the first arg)
|
|
|
|
# If we're using the default, let's keep that first arg
|
|
|
|
# because it might be important.
|
2015-08-20 06:58:19 +00:00
|
|
|
arguments = arguments[1:]
|
|
|
|
result = function(*arguments)
|
|
|
|
print('Output: %s' % result)
|
2015-08-21 06:09:50 +00:00
|
|
|
results.append(results)
|
|
|
|
return results
|
2015-08-20 06:58:19 +00:00
|
|
|
|
2015-08-21 06:09:50 +00:00
|
|
|
def functionmap_comment(comment):
|
2015-08-20 06:58:19 +00:00
|
|
|
lines = comment.split('\n')
|
2015-08-21 06:09:50 +00:00
|
|
|
results = []
|
2015-08-20 06:58:19 +00:00
|
|
|
for line in lines:
|
2015-08-21 06:09:50 +00:00
|
|
|
result = functionmap_line(line)
|
|
|
|
if result is not None:
|
|
|
|
results += result
|
|
|
|
return results
|
2015-08-20 06:58:19 +00:00
|
|
|
|
|
|
|
for comment in SAMPLE_COMMENTS:
|
|
|
|
print()
|
2015-08-21 06:09:50 +00:00
|
|
|
functionmap_comment(comment)
|
2015-08-20 06:58:19 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
User said: robot! add 1 2
|
|
|
|
Broken into: ['robot!', 'add', '1', '2']
|
|
|
|
Found command: ['add', '1', '2']
|
|
|
|
Using function: add
|
|
|
|
Output: 3.0
|
|
|
|
|
|
|
|
User said: I want robot! multiply 4 2 trash trash trash
|
|
|
|
Broken into: ['I', 'want', 'robot!', 'multiply', '4', '2', 'trash', 'trash', 'trash']
|
|
|
|
Found command: ['multiply', '4', '2', 'trash', 'trash', 'trash']
|
|
|
|
Using function: multiply
|
|
|
|
Output: 8.0
|
|
|
|
|
|
|
|
User said: robot! blank
|
|
|
|
Broken into: ['robot!', 'blank']
|
|
|
|
Found command: ['blank']
|
|
|
|
Using function: default_function
|
|
|
|
Output: Look what I found: ('blank',)
|
|
|
|
|
|
|
|
User said: robot!
|
|
|
|
Broken into: ['robot!']
|
|
|
|
Found command: []
|
|
|
|
Did nothing
|
|
|
|
|
|
|
|
User said: robot! add 7 19
|
|
|
|
Broken into: ['robot!', 'add', '7', '19']
|
|
|
|
Found command: ['add', '7', '19']
|
|
|
|
Using function: add
|
|
|
|
Output: 26.0
|
|
|
|
|
|
|
|
User said: robot! exponent 7 2
|
|
|
|
Broken into: ['robot!', 'exponent', '7', '2']
|
|
|
|
Found command: ['exponent', '7', '2']
|
|
|
|
Using function: exponent
|
|
|
|
Output: 49.0
|
|
|
|
|
|
|
|
User said: robot! join "one two three" "-"
|
|
|
|
Broken into: ['robot!', 'join', 'one two three', '-']
|
|
|
|
Found command: ['join', 'one two three', '-']
|
|
|
|
Using function: join
|
|
|
|
Output: one-two-three
|
|
|
|
|
|
|
|
User said: robot! add 10 10 robot! multiply 10 10 robot! exponent 10 10
|
|
|
|
Broken into: ['robot!', 'add', '10', '10', 'robot!', 'multiply', '10', '10', 'robot!', 'exponent', '10', '10']
|
|
|
|
Found command: ['add', '10', '10']
|
|
|
|
Using function: add
|
|
|
|
Output: 20.0
|
|
|
|
Found command: ['multiply', '10', '10']
|
|
|
|
Using function: multiply
|
|
|
|
Output: 100.0
|
|
|
|
Found command: ['exponent', '10', '10']
|
|
|
|
Using function: exponent
|
|
|
|
Output: 10000000000.0
|
|
|
|
|
|
|
|
User said: robot! add 1 1
|
|
|
|
Broken into: ['robot!', 'add', '1', '1']
|
|
|
|
Found command: ['add', '1', '1']
|
|
|
|
Using function: add
|
|
|
|
Output: 2.0
|
|
|
|
User said: robot! multiply 1 1
|
|
|
|
Broken into: ['robot!', 'multiply', '1', '1']
|
|
|
|
Found command: ['multiply', '1', '1']
|
|
|
|
Using function: multiply
|
|
|
|
Output: 1.0
|
|
|
|
|
2015-08-20 07:02:04 +00:00
|
|
|
User said: do ROBOT! add boom box
|
|
|
|
Broken into: ['do', 'ROBOT!', 'add', 'boom', 'box']
|
2015-08-20 06:58:19 +00:00
|
|
|
Found command: ['add', 'boom', 'box']
|
|
|
|
Using function: add
|
|
|
|
Output: None
|
|
|
|
|
2015-08-20 07:02:04 +00:00
|
|
|
User said: robot! robot!
|
|
|
|
Broken into: ['robot!', 'robot!']
|
|
|
|
Found command: []
|
|
|
|
Did nothing
|
|
|
|
Found command: []
|
|
|
|
Did nothing
|
|
|
|
|
2015-08-20 06:58:19 +00:00
|
|
|
User said:
|
|
|
|
Broken into: []
|
|
|
|
'''
|