gunthercox / ChatterBot

ChatterBot is a machine learning, conversational dialog engine for creating chat bots
https://chatterbot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
14.1k stars 4.45k forks source link

from chatterbot.corpus import load_corpus, list_corpus_files ImportError: cannot import name 'load_corpus' #1616

Closed aspiringguru closed 5 years ago

aspiringguru commented 5 years ago

I'm sure I've had this basic example of chatterbot corpus working previously. Following examples from https://chatterbot.readthedocs.io/en/latest/training.html#training-with-corpus-data and https://chatterbot.readthedocs.io/en/stable/corpus.html

I'm experiencing this error.

from chatterbot.corpus import load_corpus, list_corpus_files ImportError: cannot import name 'load_corpus'

full code below. Have replicated the problem on Windows10 and Ubuntu 16.

I'm sure this is a simple and obvious problem, but really throwing me atm.


from flask import Flask, render_template, request from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer

app = Flask(name)

print("loading ChatBot") chatbot = ChatBot('corpus example') print("setting trainer") trainer = ChatterBotCorpusTrainer(chatbot) print("training") trainer.train( "chatterbot.corpus.english" ) print("training completed")

@app.route("/") def home(): return render_template("chatterbot.html")

@app.route("/get") def get_bot_response(): userText = request.args.get('msg') return str(chatbot.get_response(userText))

if name == "main": app.run(debug=True, port=80)


python chatterbot_corpus.py loading ChatBot [nltk_data] Downloading package averaged_perceptron_tagger to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package averaged_perceptron_tagger is already up-to- [nltk_data] date! [nltk_data] Downloading package punkt to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package punkt is already up-to-date! [nltk_data] Downloading package stopwords to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package stopwords is already up-to-date! setting trainer training loading ChatBot [nltk_data] Downloading package averaged_perceptron_tagger to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package averaged_perceptron_tagger is already up-to- [nltk_data] date! [nltk_data] Downloading package punkt to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package punkt is already up-to-date! [nltk_data] Downloading package stopwords to [nltk_data] C:\Users\Matthew\AppData\Roaming\nltk_data... [nltk_data] Package stopwords is already up-to-date! setting trainer training Traceback (most recent call last): File "chatterbot_corpus.py", line 13, in "/c/Users/blah/Anaconda3/Lib/site-packages/chatterbot_corpus/data/english/chatterbot.corpus.english" File "C:\Users\blah\Anaconda3\lib\site-packages\chatterbot\trainers.py", line 130, in train from chatterbot.corpus import load_corpus, list_corpus_files File "C:\Users\blah\Anaconda3\lib\site-packages\chatterbot\corpus.py", line 5, in from chatterbot_corpus.corpus import DATA_DIRECTORY File "D:\2018_working\coding\python_flask_chatterbot\chatterbot_corpus.py", line 13, in "/c/Users/blah/Anaconda3/Lib/site-packages/chatterbot_corpus/data/english/chatterbot.corpus.english" File "C:\Users\blah\Anaconda3\lib\site-packages\chatterbot\trainers.py", line 130, in train from chatterbot.corpus import load_corpus, list_corpus_files ImportError: cannot import name 'load_corpus'


environment: Windows 10 $ python --version Python 3.6.5 :: Anaconda, Inc.

$ pip freeze | grep chatterbot chatterbot-corpus==1.2.0

same problem on AWS ubuntu 16 (using python Python 3.6.7 & chatterbot chatterbot-corpus==1.2.0)

[minor edit to replace username in path with blah]

gunthercox commented 5 years ago

@aspiringguru, It is possible that you are using an older version of chatterbot. I see that the chatterbot-corpus version you are using is 1.2. You'll want to make sure you have chatterbot 1.0 downloaded or installed for this to work.

aspiringguru commented 5 years ago

This was a fresh install of chatterbot yesterday. also: my bad, just noticed I only listed the chatterbot-corpus version.

$ pip freeze | grep atter ChatterBot==1.0.2 chatterbot-corpus==1.2.0

also checked with pip install --upgrade ChatterBot pip install --upgrade chatterbot-corpus same version after update as above.

also, checking my python runtime is not somehow using a different environment. import chatterbot print(chatterbot.version)

1.0.2

I'm was not able to check version of chatterbot-corpus programmatically inside runtime.

I've seen other questions/answers about corpus paths - when creating custom corpus.

I can see chatterbot corpus files in C:\Users\blah\Anaconda3\Lib\site-packages\chatterbot_corpus\data

other directories I can see installed C:\Users\blah\Anaconda3\Lib\site-packages\chatterbot C:\Users\blah\Anaconda3\Lib\site-packages\chatterbot_corpus-1.2.0.dist-info C:\Users\blah\Anaconda3\Lib\site-packages\ChatterBot-1.0.2.dist-info

Looking at file "C:\Users\blah\Anaconda3\lib\site-packages\chatterbot\trainers.py", line 130 so function load_corpus exists.



def load_corpus(*data_file_paths):
    """
    Return the data contained within a specified corpus.
    """
    for file_path in data_file_paths:
        corpus = []
        corpus_data = read_corpus(file_path)

        conversations = corpus_data.get('conversations', [])
        corpus.extend(conversations)

        categories = corpus_data.get('categories', [])

        yield corpus, categories, file_path

Weird thing is I had a demo working using the chatterbot corpus months ago, straight from the demos/examples, odd I can't get it working on a fresh install.

gunthercox commented 5 years ago

Unfortunately this isn't something I've encountered before. It does seem strange that the function exists, but the ImportError is being triggered. Based on the version outputs you've posted, it looks like everything in your environment should be set up correctly.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.