Comments (8)
Here is an alternative method you can try:
def remove_hyphens(statement):
# ...
bot = ChatBot('Test',
# ...
)
bot.preprocessors.append(
remove_hyphens
)
trainer = ListTrainer(bot)
# ...
By doing this, you can retroactively add your custom preprocessor to the chat bot.
from chatterbot-corpus.
ChatterBot's training process respects the preprocessors that are configured for the chatbot being trained. Because of this you could create a preprocessor that removes hyphens.
https://chatterbot.readthedocs.io/en/stable/preprocessors.html
def remove_hyphens(statement):
"""
Remove hypnens.
"""
statement.text = statement.text.replace('-', '')
return statement
chatbot = ChatBot(
'Example,
preprocessors=[
remove_hyphens
]
)
from chatterbot-corpus.
This is the error I'm getting after trying that:
AttributeError: NameError: name 'remove_hyphens' is not defined
Here is my code:
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
from chatterbot.conversation import Statement
import os
bot = ChatBot('Test',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.sqlite3',
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.90
}
],
preprocessors=[
remove_hyphens
],
trainer='chatterbot.trainers.ListTrainer')
trainer = ListTrainer(bot)
for _file in os.listdir('files'):
chats = open('files/' + _file, 'r').readlines()
trainer.train(chats)
def remove_hyphens(statement):
"""
Remove hypnens.
"""
statement.text = statement.text.replace('-', '')
return statement
while True:
try:
message = input('You:')
if message.strip() == 'Bye':
print('ChatBot: Bye')
break
else:
reply = bot.get_response(message)
if reply.confidence == 0:
print("I don't understand")
else:
print('ChatBot:', reply)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
from chatterbot-corpus.
You are seeing that error because you need to define the remove_hyphens
function before you reference it in the ChatBot
instance. (Just move the def remove_hyphens
before the bot =
line.)
from chatterbot-corpus.
Yes, you're right.
However, I am getting this error now: [AttributeError: 'function' object has no attribute 'split']
Traceback (most recent call last):
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\practise\newtrial\improved main\improvedMain.py", line 30, in
trainer='chatterbot.trainers.ListTrainer')
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\chatterbot.py", line 50, in init
self.preprocessors.append(utils.import_module(preprocessor))
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\utils.py", line 14, in import_module
module_parts = dotted_path.split('.')
AttributeError: 'function' object has no attribute 'split'
from chatterbot-corpus.
You might need to move the remove_hyphens function into it's own python file. Once you make that change, you should be able to modify the corresponding value in the preprocessors list to be a string representing a relative import.
from chatterbot-corpus.
I tried that and now I am getting a different kind of error. Hope you can help me.
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
import os
bot = ChatBot('Test',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.sqlite3',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.95
}
],
preprocessors= [{'import_path':'chatterbot.preprocessors.remove_hyphens'}],
trainer='chatterbot.trainers.ListTrainer')
trainer = ListTrainer(bot)
for _file in os.listdir('files'):
chats = open('files/' + _file, 'r').readlines()
trainer.train(chats)
while True:
try:
message = input('You:')
if message.strip() == 'Bye':
print('ChatBot: Bye')
break
else:
reply = bot.get_response(message)
if reply.confidence == 0:
print("Chatbot: I don't understand")
else:
print('ChatBot:', reply)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Error:
Traceback (most recent call last):
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\practise\newtrial\improved main\improvedMain.py", line 19, in <module>
trainer='chatterbot.trainers.ListTrainer')
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\chatterbot.py", line 50, in __init__
self.preprocessors.append(utils.import_module(preprocessor))
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\utils.py", line 14, in import_module
module_parts = dotted_path.split('.')
AttributeError: 'dict' object has no attribute 'split'
>>>
preprocessors.py
def remove_hyphens(statement):
"""
Remove hyphens.
"""
statement.text = statement.text.replace('-', '')
return statement
from chatterbot-corpus.
Thank you! It works now :)
from chatterbot-corpus.
Related Issues (20)
- Corpus data HOT 1
- other sources?
- Corpus.py needs updating for YAML>5 HOT 1
- [Italian] "Qual รจ". Not "Qual'รจ"
- Italian corpus sources
- Travis Build Failed..No module named 'spacy' HOT 1
- Adding more conversations for Odia Language
- Regarding running the downloaded file HOT 1
- sd HOT 1
- Pyyaml HOT 1
- Update conversations in Portuguese
- No module named 'spacy' in Job of Travis CI HOT 1
- line break, yaml file HOT 1
- chatterbot.corpus.english.health is very broken
- Huge datasets
- Unable to load certain language corpuses
- is training via ListTrainer still possible?
- Corpus _es chatterbot
- spanish dataset fail
- Odd non sequitur?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chatterbot-corpus.