AttributeError: 'list' object has no attribute 'split' - Chatterbot - python

I have been encountering this error within my chatterbot script when attempting to use the train function from a file list:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\project\main.py", line 60, in <module>
trainer.train(file_list)
File "C:\Users\user\pythonProject\lib\site-packages\chatterbot\trainers.py", line 136, in train
data_file_paths.extend(list_corpus_files(corpus_path))
File "C:\Users\user\pythonProject\lib\site-packages\chatterbot\corpus.py", line 46, in list_corpus_files
corpus_path = get_file_path(dotted_path, extension=CORPUS_EXTENSION)
File "C:\Users\user\pythonProject\lib\site-packages\chatterbot\corpus.py", line 20, in get_file_path
parts = dotted_path.split('.')
AttributeError: 'list' object has no attribute 'split'
Here is the code for reference:
import os
import re
import yaml
import transformers
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from Cognify.cleaner import clean_corpus
I am using .yml files found in the chatterbot git as a reference piece in order to ensure it functions, but I cannot seem to get it to read the files. The trainer I am using is based off of the ChatterBotCorpusTrainer, as I have read that it is meant for reading .yml files. Additionally, it calls on the cleaner script, which is an external script that cleans and preprocesses data from the user so the bot can process the data after (although, I am not sure it is necessary).
Here is where I actually call the trainer:
chatbot = ChatBot("Cognify")
trainer = ChatterBotCorpusTrainer(chatbot)
file_list = [
"C:/Users/user/PycharmProjects/pythonProject/directory/ai.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/botprofile.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/computers.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/conversations.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/emotion.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/food.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/gossip.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/greetings.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/health.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/history.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/humor.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/literature.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/money.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/movies.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/politics.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/psychology.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/science.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/sports.yml",
"C:/Users/user/PycharmProjects/pythonProject/directory/trivia.yml"
]
cleaned_corpus = clean_corpus(file_list)
trainer.train(file_list)
I am not certain where or why it is giving me this error. I cannot seem to pinpoint it, however I sense it is in plain sight. Additionally, I am using the hugging-face BERT transformer. Any advice is welcome.

ChatterBotCorpusTrainer.train's signature doesn't accept a list, it takes *args. Relevant documentation example, and the source code of the signature.
Replace trainer.train(file_list) with trainer.train(*file_list).

Related

I pip installed the arcgis library, but the reverse geocoding function is not working

from geopy.geocoders import ArcGIS
from arcgis.geocoding import reverse_geocode
arcgis = ArcGIS()
results = reverse_geocode(location={"x": 103.876722, "y": 1.3330736})
print(results)
When I run this script in python, I get the following error.
Traceback (most recent call last):
File "C:\Users\18102\RentScraping\newMarketRequests.py", line 22, in <module>
results = reverse_geocode(location={"x": 103.876722, "y": 1.3330736})
File "C:\Users\18102\AppData\Roaming\Python\Python310\site-packages\arcgis\geocoding\_functions.py", line 1457, in reverse_geocode
geocoder = arcgis.env.active_gis._tools.geocoders[0]
AttributeError: 'NoneType' object has no attribute '_tools'
I pip installed the arcgis library, which I have used before for geocoding on a different project, but the reverse geocoding isn't working. I saw some documentation that had a few variations on how to provide the location parameter. I also tried reverse_geocode([103.876722, 1.3330736]) and got the same result. I think there might be something that got updated and the documentation I'm referencing is out of date. Any advice?

AttributeError: module 'collections' has no attribute 'Iterable'

I am using the "pdftables" library to extract tables from a pdf.
This is my code:
import pdftables
pg = pdftables.get_pdf_page(open("filename.pdf","rb"),253)
print(pg)
table = pdftables.page_to_tables(pg)
print(table)
I am getting this error and I am not sure what's causing it.
Traceback (most recent call last):
File "c:\Users\gayak\OneDrive\Documents\PDF to Database\PDF_to_Tables_3.py", line 9, in <module>
table = pdftables.page_to_tables(pg)
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\pdftables.py", line 485, in page_to_tables
box_list = LeafList().populate(page, flt).purge_empty_text()
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 98, in populate
for obj in children(pdfpage):
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 75, in children
if isinstance(obj, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
The version of python I am using is python 3.10.4
I used pip install pdftables.six to get the library
If you don't want to change the source code, there is an easier way. Just use this in your script after importing.
import collections
collections.Iterable = collections.abc.Iterable
As the Error says, the attribute isn't valid. When using collection.Iterable then it not finds the Iterable attribute. This can have different reasons but in this case, after looking on the package files on Github, i noticed that you are missing the abc keyword. I not tried this solution, but I'm 95% sure that using collections.abc.Iterable will do the thing.
import collections
from _collections_abc import Iterable
collection.Iterable = Iterable
This should work 🙂
A simple fix that works for python3.10:
Under directory
/usr/lib/python3.10/collections/__ init__.py
Note: The path might change depending
Add this line of code:
from _collections_abc import Iterable

Python: AttributeError: 'module' object has no attribute, which is untrue

What I want to do:
I want to import a python module (pocketsphinx) and use the output from the Decoder attribute. However when I try to use it, I'm informed that module attribute 'Decoder' doesn't exist.
decoder = Decoder(configSwitches)
It does exist, though, which is what makes it so strange.
What I've done so far:
When I pull up a python console and input import pocketsphinx, it imports without any issue. Running pocketsphinx.file returns:
'/usr/local/lib/python2.7/dist-packages/pocketsphinx-0.0.8-py2.7-linux-armv7l.egg/pocketsphinx/__init__.pyc'
Looking in '/usr/local/lib/python2.7/dist-packages/pocketsphinx-0.0.8-py2.7-linux-armv7l.egg/pocketsphinx/__init__.py', I see: from pocketsphinx import * and that's it.
When I go back up to /usr/local/lib/python2.7/dist-packages/pocketsphinx/pocketsphinx.py and open it in a text editor, I see that pocketsphinx.py does indeed have a Decoder class with a healthy number of defined methods.
My Ask:
What other steps can I take to diagnose what's wrong with my use of the pocketsphinx module?
Here's the example code I was trying to run before really digging into the project:
import pocketsphinx
hmmd = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/en-us"
lmdir = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/en-us.lm.bin"
dictp = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/cmudict-en-us.dict"
fileName = r'/home/michael/Desktop/sphinxASR/voice_message.wav'
if __name__ == "__main__":
wavFile = open(fileName, "rb")
speechRec = pocketsphinx.Decoder(hmm=hmmd, lm=lmdir, dictionary=dictp)
wavFile.seek(44)
speechRec.decode_raw(wavFile)
result = speechRec.get_hyp()
print(result)
Stack trace:
Traceback (most recent call last):
File "/home/michael/PycharmProjects/27test/getHypTest.py", line 14, in <module>
speechRec = pocketsphinx.Decoder(lm=lmdir, dictionary=dictp)
AttributeError: 'module' object has no attribute 'Decoder'
By looking at the pocketsphinx example code, it seems that your import should be:
from pocketsphinx.pocketsphinx import *
My first step diagnosing this issue would be to type the following, so that I can see what is being imported:
import pocketsphinx
dir(pocketsphinx)
You should import Decoder from pocketsphinx.
Instead of
import pocketsphinx
try:
from pocketsphinx.pocketsphinx import Decoder

Any way to see where python is importing a module from?

So I installed the facebook module, realized it was the wrong one, used pip to uninstall and then installed facebook-sdk. Here is my code:
import facebook
token = '[token]'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
and get
Traceback (most recent call last):
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
AttributeError: 'module' object has no attribute 'GraphAPI'
When looking this up, EVERY result says to uninstall facebook and facebook-sdk and reinstall facebook-sdk. And I have, many many times. I searched /python27/ for facebook afterwards to make sure the files were gone.
Is there any way on a windows machine to trace back where I am importing "facebook" from?
Module objects have a __file__ attribute, and the object representation also includes the file:
print facebook
print facebook.__file__
In your case, you are importing your own script; you named it facebook as well and are masking the installed module:
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
Note the filename in the first line, then the fact that the same file is used for that import. Python stores the main script as __main__, so importing the script itself results in another module being created for the actual filename.

Import Error using cPickle in Python

I am using Pickle in Python2.7. I am getting error while using cPickle.load() method. The code and error is shown below. Can someone guide me through this?
Code:
#! usr/bin/python
import cPickle
fo = open('result','rb')
dict1 = cPickle.load(fo)
Error:
Traceback (most recent call last):
File "C:\Python27\test.py", line 7, in <module>
dicts = cPickle.load(fo)
ImportError: No module named options
It seems like you can not do
import options
but when you or someone else did
cpickle.dump(xxx, open('result', 'rb'))
there was an object with a class or function of a module options that existed at this point in time, in xxx.
Solution
You can open the file binarily and replace options with the module you replaced the old module options with.
You probably created the file in your package like in module package.main by executing the file main.py or something like it, having a module options in the same directory.
Now you do import package.main, try to read the file and options is now called package.options and the module options can not be found.
How did you create this file? How do you load it now? cPickle/pickle does not transfer source code - so if you use a function you need the module when you load it.

Categories

Resources