ChatterBot icon indicating copy to clipboard operation
ChatterBot copied to clipboard

IndexError and KeyError

Open dylib opened this issue 7 years ago • 3 comments

I noticed there seems to be a couple of issues when inputting mixed words with numbers in the terminal_example.py:

KeyError

$ python terminal_example.py
Type something to begin...
ninety seven - 7
Traceback (most recent call last):
  File "terminal_example.py", line 30, in <module>
    bot_input = bot.get_response(None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 113, in get_response
    statement, response = self.generate_response(input_statement, conversation_id)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 132, in generate_response
    response = self.logic.process(input_statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/multi_adapter.py", line 49, in process
    if adapter.can_process(statement):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 33, in can_process
    response = self.process(statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 59, in process
    mathparse.parse(expression, language=self.language)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 269, in parse
    postfix = to_postfix(tokens)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 176, in to_postfix
    while (opstack != []) and (precedence[opstack[-1]] >= precedence[token]):
KeyError: '9ty'

IndexError

$ python terminal_example.py
sType something to begin...
seven - 7
Traceback (most recent call last):
  File "terminal_example.py", line 30, in <module>
    bot_input = bot.get_response(None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 113, in get_response
    statement, response = self.generate_response(input_statement, conversation_id)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 132, in generate_response
    response = self.logic.process(input_statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/multi_adapter.py", line 49, in process
    if adapter.can_process(statement):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 33, in can_process
    response = self.process(statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 59, in process
    mathparse.parse(expression, language=self.language)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 271, in parse
    return evaluate_postfix(postfix)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 203, in evaluate_postfix
    a = stack.pop()
IndexError: pop from empty list

...and...

$ python terminal_example.py
Type something to begin...
ninetyseven - seven
Traceback (most recent call last):
  File "terminal_example.py", line 30, in <module>
    bot_input = bot.get_response(None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 113, in get_response
    statement, response = self.generate_response(input_statement, conversation_id)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/chatterbot.py", line 132, in generate_response
    response = self.logic.process(input_statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/multi_adapter.py", line 49, in process
    if adapter.can_process(statement):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 33, in can_process
    response = self.process(statement)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chatterbot/logic/mathematical_evaluation.py", line 59, in process
    mathparse.parse(expression, language=self.language)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 271, in parse
    return evaluate_postfix(postfix)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mathparse/mathparse.py", line 203, in evaluate_postfix
    a = stack.pop()
IndexError: pop from empty list

Some type of exception handling here would probably be better than just crashing perhaps.

dylib avatar Apr 30 '18 02:04 dylib

@dylib This due to lack of statements present SQLStorageAdapter

https://github.com/gunthercox/ChatterBot/blob/abbd96934b0af0197d012d8cb0305d401cd3210a/examples/terminal_example.py#L12

may you could try to train your bot with some statements using list trainer http://chatterbot.readthedocs.io/en/stable/training.html#training-via-list-data

vkosuri avatar Apr 30 '18 06:04 vkosuri

Looks like this is related to the mathparse library. @dylib thank you for the test data, it'll make fixing this issue much easier.

gunthercox avatar May 08 '18 22:05 gunthercox

for the key error here is the solution i found: https://github.com/gunthercox/ChatterBot/issues/2256

Hope it helps!

AskellAytch avatar Jun 30 '22 14:06 AskellAytch