我有一段代码,但是当我运行代码时,出现一个错误,我不明白显然擅长代码的姐姐……我的代码是:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)
for files in os.listdir(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english'):
data = open(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english' + files ,'r').readlines()
bot.train(data)
while True:
message = input('You:')
if message.strip() != 'Bye':
reply = bot.get_responce(message)
print('ChatBot :',reply)
if message.strip() == 'Bye':
print('ChatBot : Bye')
break
我在cmd
中的错误状态:
C:\Users\vanos\Desktop>ai.py
Traceback (most recent call last):
File "C:\Users\vanos\Desktop\ai.py", line 9, in <module>
data = open(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english' + files ,'r').readlines()
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Users\\vanos\\chatterbot-corpus-
1.2.0\\chatterbot_corpus\\data\\englishai.yml'
我在idle
中的错误状态:
Traceback (most recent call last):
File "C:/Users/vanos/Desktop/ai.py", line 9, in <module>
data = open(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english' + files ,'r').readlines()
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Users\\vanos\\chatterbot-corpus-
1.2.0\\chatterbot_corpus\\data\\englishai.yml'
我只是想制作一个AI聊天框。
我只想指出文件目录是正确的。我对它进行了三重检查,并已将目录复制并粘贴了5次。
我的预期结果是列表训练器加载并与AI进行对话。但是,我得到了这些错误,并且列表培训器将不会加载,而是每次都会出现该错误消息。
编辑/////
我当前的代码:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)
for files in os.listdir(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english'):
data = open(os.path.join(r'C:\Users\vanos\chatterbot-corpus-
1.2.0\chatterbot_corpus\data\english' , files) ,'r').readlines()
bot.train(data)
while True:
message = input('You:')
if message.strip() != 'Bye':
reply = bot.get_responce(message)
print('ChatBot :',reply)
if message.strip() == 'Bye':
print('ChatBot : Bye')
break
错误消息:
C:\Users\vanos\Desktop\AI>ai.py
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [########### ] 56%Traceback (most recent call last):
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 1193, in _execute_context
context)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\default.py", line 509, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: attempt to write a readonly database
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\vanos\Desktop\AI\ai.py", line 34, in <module>
bot.train(data)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\chatterbot\trainers.py", line 118, in train
self.storage.update(statement)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\chatterbot\storage\sql_storage.py", line 274, in update
self._session_finish(session)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\chatterbot\storage\sql_storage.py", line 396, in _session_finish
session.commit()
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\session.py", line 954, in commit
self.transaction.commit()
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\session.py", line 467, in commit
self._prepare_impl()
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\session.py", line 447, in _prepare_impl
self.session.flush()
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\session.py", line 2313, in flush
self._flush(objects)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site
packages\sqlalchemy\orm\session.py", line 2440, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\util\compat.py", line 249, in reraise
raise value
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\session.py", line 2404, in _flush
flush_context.execute()
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\unitofwork.py", line 395, in execute
rec.execute(self)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\unitofwork.py", line 560, in execute
uow
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\persistence.py", line 181, in save_obj
mapper, table, insert)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\orm\persistence.py", line 872, in
_emit_insert_statements
execute(statement, params)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 948, in execute
return meth(self, multiparams, params)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\sql\elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 1200, in _execute_context
context)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 1413, in _handle_dbapi_exception
exc_info
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\util\compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\util\compat.py", line 248, in reraise
raise value.with_traceback(tb)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\base.py", line 1193, in _execute_context
context)
File "C:\Users\vanos\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\sqlalchemy\engine\default.py", line 509, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) attempt to
write a readonly database [SQL: 'INSERT INTO statement (text, extra_data)
VALUES (?, ?)'] [parameters: ('- what do you get when you cross a serious
thief and a mad young man?', <memory at 0x035C5370>)] (Background on this
error at: http://sqlalche.me/e/e3q8)
IDK为什么会帮助
我再次运行代码进行仔细检查,结果给出了另一个错误...
当前错误:
C:\Users\vanos\Desktop\AI>ai.py
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
You:
Traceback (most recent call last):
File "C:\Users\vanos\Desktop\AI\ai.py", line 39, in <module>
reply = bot.get_responce(message)
AttributeError: 'ChatBot' object has no attribute 'get_responce'
帮助大声笑
我第三次尝试进行三重检查,并收到以下错误消息:
C:\Users\vanos\Desktop\AI>ai.py
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
List Trainer: [####################] 100%
You:Hi
Traceback (most recent call last):
File "C:\Users\vanos\Desktop\AI\ai.py", line 39, in <module>
reply = bot.get_responce(message)
AttributeError: 'ChatBot' object has no attribute 'get_responce'
至少这一次它给了我一个选择,让我笑起来。请帮助人们。
答案 0 :(得分:0)
使用。
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)
for files in os.listdir(r'C:\Users\vanos\chatterbot-corpus-1.2.0\chatterbot_corpus\data\english'):
data = open(os.path.join(r'C:\Users\vanos\chatterbot-corpus-1.2.0\chatterbot_corpus\data\english' , files) ,'r').readlines()
bot.train(data)
while True:
message = input('You:')
if message.strip() != 'Bye':
reply = bot.get_responce(message)
print('ChatBot :',reply)
if message.strip() == 'Bye':
print('ChatBot : Bye')
break
`
答案 1 :(得分:-1)
使用
os.path.join('C:/', 'Users', 'vanos', 'chatterbot', 'rest_of_the_path')
避免使用经过编码的路径,因为它们取决于平台。使用os.path.join()之类的函数可处理特定于平台的路径格式。