Skip to content

Commit e56274c

Browse files
author
xuming06
committed
add chat bot.
1 parent b27fe45 commit e56274c

83 files changed

Lines changed: 8550 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

09qa/chatterbot_demo/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+

09qa/chatterbot_demo/base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
from chatterbot import ChatBot
7+
from chatterbot.trainers import ListTrainer
8+
9+
# Create a new chat bot named Charlie
10+
chatbot = ChatBot('Charlie')
11+
12+
trainer = ListTrainer(chatbot)
13+
14+
trainer.train([
15+
"Hi, can I help you?",
16+
"Sure, I'd like to book a flight to Iceland.",
17+
"Your flight has been booked."
18+
])
19+
20+
# Get a response to the input text 'I would like to book a flight.'
21+
response = chatbot.get_response('I would like to book a flight.')
22+
23+
print(response)

09qa/chatterbot_demo/export.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
from chatterbot import ChatBot
8+
from chatterbot.trainers import ChatterBotCorpusTrainer
9+
10+
'''
11+
This is an example showing how to create an export file from
12+
an existing chat bot that can then be used to train other bots.
13+
'''
14+
15+
chatbot = ChatBot('Export Example Bot')
16+
17+
# First, lets train our bot with some data
18+
trainer = ChatterBotCorpusTrainer(chatbot)
19+
20+
trainer.train('chatterbot.corpus.english')
21+
22+
# Now we can export the data to a file
23+
trainer.export_for_training('./my_export.json')
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
from chatterbot import ChatBot
8+
from chatterbot.conversation import Statement
9+
from chatterbot.trainers import ListTrainer
10+
import logging
11+
logging.basicConfig(level=logging.INFO)
12+
"""
13+
This example shows how to create a chat bot that
14+
will learn responses based on an additional feedback
15+
element from the user.
16+
"""
17+
18+
# Uncomment the following line to enable verbose logging
19+
# import logging
20+
# logging.basicConfig(level=logging.INFO)
21+
22+
# Create a new instance of a ChatBot
23+
bot = ChatBot(
24+
'Feedback Learning Bot',
25+
logic_adapters=[
26+
{
27+
'import_path': 'chatterbot.logic.BestMatch',
28+
'default_response': 'I am sorry, but I do not understand.',
29+
'maximum_similarity_threshold': 0.90
30+
}
31+
],
32+
storage_adapter='chatterbot.storage.SQLStorageAdapter'
33+
)
34+
35+
trainer = ListTrainer(bot)
36+
37+
trainer.train([
38+
"Hi, can I help you?",
39+
"Sure, I'd like to book a flight to Iceland.",
40+
"Your flight has been booked.",
41+
'hi',
42+
'hello',
43+
'what is your sex?',
44+
'female',
45+
'bye',
46+
'byebye'
47+
])
48+
49+
50+
def get_feedback():
51+
text = input()
52+
53+
if 'yes' in text.lower() or 'y' in text.lower():
54+
return True
55+
elif 'no' in text.lower() or 'n' in text.lower():
56+
return False
57+
else:
58+
print('Please type either "yes" or "no"')
59+
return get_feedback()
60+
61+
62+
print('Type something to begin...')
63+
64+
# The following loop will execute each time the user enters input
65+
while True:
66+
try:
67+
input_statement = Statement(text=input())
68+
response = bot.get_response(input_statement)
69+
print(response)
70+
print('\n Is "{}" a right response to "{}"? \n'.format(
71+
response.text,
72+
input_statement.text
73+
))
74+
if not get_feedback():
75+
print('please input the correct one')
76+
correct_response = Statement(text=input())
77+
bot.learn_response(correct_response, input_statement)
78+
print('Responses added to bot!')
79+
80+
# Press ctrl-c or ctrl-d on the keyboard to exit
81+
except (KeyboardInterrupt, EOFError, SystemExit):
82+
break

09qa/chatterbot_demo/memory.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
from chatterbot import ChatBot
8+
9+
# Uncomment the following lines to enable verbose logging
10+
# import logging
11+
# logging.basicConfig(level=logging.INFO)
12+
13+
# Create a new instance of a ChatBot
14+
bot = ChatBot(
15+
'SQLMemoryTerminal',
16+
storage_adapter='chatterbot.storage.SQLStorageAdapter',
17+
database_uri=None,
18+
logic_adapters=[
19+
'chatterbot.logic.MathematicalEvaluation',
20+
'chatterbot.logic.TimeLogicAdapter',
21+
'chatterbot.logic.BestMatch'
22+
]
23+
)
24+
25+
# Get a few responses from the bot
26+
27+
28+
print(bot.get_response('What time is it?'))
29+
print(bot.get_response('What is 7 plus 7?'))

09qa/chatterbot_demo/response.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
8+
from chatterbot import ChatBot
9+
from chatterbot.trainers import ListTrainer
10+
11+
12+
# Create a new instance of a ChatBot
13+
bot = ChatBot(
14+
'Example Bot',
15+
storage_adapter='chatterbot.storage.SQLStorageAdapter',
16+
logic_adapters=[
17+
{
18+
'import_path': 'chatterbot.logic.BestMatch',
19+
'default_response': 'I am sorry, but I do not understand.',
20+
'maximum_similarity_threshold': 0.90
21+
}
22+
]
23+
)
24+
25+
trainer = ListTrainer(bot)
26+
27+
# Train the chat bot with a few responses
28+
trainer.train([
29+
'How can I help you?',
30+
'I want to create a chat bot',
31+
'Have you read the documentation?',
32+
'No, I have not',
33+
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
34+
])
35+
36+
# Get a response for some unexpected input
37+
response = bot.get_response('How do I make an omelette?')
38+
print(response)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
from chatterbot import ChatBot
8+
9+
10+
# Uncomment the following lines to enable verbose logging
11+
# import logging
12+
# logging.basicConfig(level=logging.INFO)
13+
14+
# Create a new instance of a ChatBot
15+
bot = ChatBot(
16+
'Terminal',
17+
storage_adapter='chatterbot.storage.SQLStorageAdapter',
18+
logic_adapters=[
19+
'chatterbot.logic.MathematicalEvaluation',
20+
'chatterbot.logic.TimeLogicAdapter',
21+
'chatterbot.logic.BestMatch'
22+
],
23+
database_uri='sqlite:///database1.sqlite3'
24+
)
25+
26+
print('Type something to begin...')
27+
28+
# The following loop will execute each time the user enters input
29+
while True:
30+
try:
31+
user_input = input()
32+
33+
bot_response = bot.get_response(user_input)
34+
35+
print(bot_response)
36+
37+
# Press ctrl-c or ctrl-d on the keyboard to exit
38+
except (KeyboardInterrupt, EOFError, SystemExit):
39+
break
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
8+
from chatterbot import ChatBot
9+
from chatterbot.trainers import ChatterBotCorpusTrainer
10+
import logging
11+
12+
13+
'''
14+
This is an example showing how to train a chat bot using the
15+
ChatterBot Corpus of conversation dialog.
16+
'''
17+
18+
# Enable info level logging
19+
logging.basicConfig(level=logging.INFO)
20+
21+
chatbot = ChatBot('ai Example Bot')
22+
23+
# Start by training our bot with the ChatterBot corpus data
24+
trainer = ChatterBotCorpusTrainer(chatbot)
25+
26+
trainer.train(
27+
'chatterbot.corpus.chinese'
28+
)
29+
30+
# Now let's get a response to a greeting
31+
response = chatbot.get_response('什么是ai?')
32+
print(response)

09qa/chinese_rasa/endpoints.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
action_endpoint:
2+
url: 'http://localhost:5055/webhook'

09qa/deeppavlov_demo.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
# -*- coding: utf-8 -*-
8+
"""hello_bot.ipynb
9+
10+
Automatically generated by Colaboratory.
11+
12+
Original file is located at
13+
https://colab.research.google.com/github/deepmipt/DeepPavlov/blob/master/docs/intro/hello_bot.ipynb
14+
15+
# Hello bot!
16+
17+
Open in [Colaboratory](https://colab.research.google.com/github/deepmipt/DeepPavlov/blob/master/docs/intro/hello_bot.ipynb)
18+
"""
19+
20+
"""
21+
!pip install -q deeppavlov
22+
Import key components to build HelloBot.
23+
"""
24+
25+
from deeppavlov.agents.default_agent.default_agent import DefaultAgent
26+
from deeppavlov.agents.processors.highest_confidence_selector import HighestConfidenceSelector
27+
from deeppavlov.skills.pattern_matching_skill import PatternMatchingSkill
28+
29+
"""Create skills as pre-defined responses for a user's input containing specific keywords and regular expressions. Every skill returns response and confidence."""
30+
31+
hello = PatternMatchingSkill(responses=['Hello!'], patterns=["hi", "hello", "good day"])
32+
bye = PatternMatchingSkill(['Goodbye!', 'See you around'],
33+
patterns=["bye", "chao", "see you"])
34+
fallback = PatternMatchingSkill(["I don't understand, sorry", 'I can say "Hello world!"'])
35+
36+
"""Agent executes skills and then takes response from the skill with the highest confidence."""
37+
38+
agent = DefaultAgent([hello, bye, fallback], skills_selector=HighestConfidenceSelector())
39+
40+
"""Give the floor to the HelloBot!"""
41+
42+
print(agent(['Hello', 'Bye', 'Or not', 'HI', 'see you', 'your name ?']))

0 commit comments

Comments
 (0)