5

I have been working on my own chatbot using the chatterbot module in python. Here is my code so far:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer

my_bot = ChatBot(
    name="PyBot",
    read_only=True,
    logic_adapters=["chatterbot.logic.MathematicalEvaluation", "chatterbot.logic.BestMatch"]
)

small_talk = [
    'hi!',
    'how do you do?',
    'how are you?',
    'i\'m cool.',
    'fine, you?',
    'always cool.',
    'i\'m ok',
    'glad to hear that.',
    'i\'m fine',
    'glad to hear that.',
    'i feel awesome',
    'excellent, glad to hear that.',
    'not so good',
    'sorry to hear that.',
    'what\'s your name?',
    'i\'m pybot. ask me a math question, please.'
]

math_talk_1 = [
    'pythagorean theorem',
    'a squared plus b squared equals c squared'
]

math_talk_2 = [
    'law of cosines',
    'c**2 = a**2 + b**2 - 2 * a * b * cos(gamma)'
]

list_trainer = ListTrainer(my_bot)

for item in (small_talk, math_talk_1, math_talk_2):
    list_trainer.train(item)

corpus_trainer = ChatterBotCorpusTrainer(my_bot)
corpus_trainer.train('chatterbot.corpus.english')

print(my_bot.get_response("hi"));

print(my_bot.get_response("i feel awesome today"))

print(my_bot.get_response("what's your name?"))

print(my_bot.get_response("show me the pythagorean theorem"))

print(my_bot.get_response("do you know the law of cosines?"))

while True:
    try:
        bot_input = input("You: ")
        bot_response = my_bot.get_response(bot_input)
        print(f"{my_bot.name}: {bot_response}")

    except(KeyboardInterrupt, EOFError, SystemExit):
        break;

When I run it, I get this error:

Traceback (most recent call last):
  File "C:\Users\robin\Desktop\ChatBot\chatbot.py", line 46, in <module>
    corpus_trainer.train('chatterbot.corpus.english')
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\trainers.py", line 138, in train
    for corpus, categories, file_path in load_corpus(*data_file_paths):
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\corpus.py", line 64, in load_corpus
    corpus_data = read_corpus(file_path)
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\chatterbot\corpus.py", line 38, in read_corpus
    return yaml.load(data_file)
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\__init__.py", line 72, in load
    return loader.get_single_data()
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 37, in get_single_data
    return self.construct_document(node)
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 46, in construct_document
    for dummy in generator:
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 398, in construct_yaml_map
    value = self.construct_mapping(node)
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 204, in construct_mapping
    return super().construct_mapping(node, deep=deep)
  File "C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py", line 126, in construct_mapping
    if not isinstance(key, collections.Hashable):
AttributeError: module 'collections' has no attribute 'Hashable'

I honestly have no idea where I (or the module) went wrong. I have tried looking online for hours but I couldn't find anything. I have tried upgrading the modules and updating python, but that didn't seem to work. I am on Windows 11 using VS Code as my text editor if that helps at all.

Jakob Long
  • 51
  • 1
  • 1
  • 4
  • 7
    `Hashable` was moved to the `collections.abc` module in Python 3.3 (although I'm not sure when it was actually removed from the `collections` module itself). Your copy of `yaml` seems to be way out of date. – jasonharper Jun 17 '22 at 13:32
  • 3
    It's possible chatterbot has pinned a version of PyYAML that is too old and does not support newer Python versions. – Iguananaut Jun 17 '22 at 13:34

4 Answers4

4

According to this PyYaml bug report, you just need a newer version of PyYaml installed. The ticket doesn't specify exactly what version fixed it, but I was able to reproduce your error with PyYaml-3.10 with this:

import yaml
with open('sample.yaml', 'r') as file:
    yaml.safe_load(file)

To fix, I just did:

pip install --upgrade PyYaml

which upgraded me to PyYaml-6.0. After that, my python code runs without error.

CDahn
  • 1,795
  • 12
  • 23
0

After updating PyYAML to 6.0 it returns this error.

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. chatterbot-corpus 1.2.0 requires PyYAML<4.0,>=3.12, but you have pyyaml 6.0 which is incompatible.

0

uninstall the current version of PyYAML:

pip uninstall PyYaml

and install the required version 3.12:

pip install -U PyYaml==3.12

Since Python3.3, Hashable was moved to the collections.abc module, so you can force compatibility, as a workaround, by adding the following lines at top:

import collections.abc
collections.Hashable = collections.abc.Hashable
-1

Go to line 126 in C:\Users\robin\AppData\Local\Programs\Python\Python310\lib\site-packages\yaml\constructor.py and change: collections.Hashable to collections.abc.Hashable

https://github.com/ablab/spades/issues/873#issuecomment-1011073085

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 06:15
  • Editing the module isn't a good long term solution, since the next update to the yaml module will just break it again. – CDahn Oct 05 '22 at 13:04