/
test_list_training.py
282 lines (219 loc) · 8.72 KB
/
test_list_training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -*- coding: utf-8 -*-
from tests.base_case import ChatBotTestCase
from chatterbot.trainers import ListTrainer
class ListTrainingTests(ChatBotTestCase):
def setUp(self):
super(ListTrainingTests, self).setUp()
self.trainer = ListTrainer(
self.chatbot,
show_training_progress=False
)
def test_training_adds_statements(self):
"""
Test that the training method adds statements
to the database.
"""
conversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm great.",
"That is good to hear",
"Thank you.",
"You are welcome.",
"Sure, any time.",
"Yeah",
"Can I help you with anything?"
]
self.trainer.train(conversation)
response = self.chatbot.get_response("Thank you.")
self.assertEqual(response.text, "You are welcome.")
def test_training_sets_in_response_to(self):
conversation = [
"Do you like my hat?",
"I do not like your hat."
]
self.trainer.train(conversation)
statements = self.chatbot.storage.filter(
in_response_to="Do you like my hat?"
)
self.assertIsLength(statements, 1)
self.assertEqual(statements[0].in_response_to, "Do you like my hat?")
def test_training_sets_search_text(self):
conversation = [
"Do you like my hat?",
"I do not like your hat."
]
self.trainer.train(conversation)
statements = self.chatbot.storage.filter(
in_response_to="Do you like my hat?"
)
self.assertIsLength(statements, 1)
self.assertEqual(statements[0].search_text, "ik")
def test_training_sets_search_in_response_to(self):
conversation = [
"Do you like my hat?",
"I do not like your hat."
]
self.trainer.train(conversation)
statements = self.chatbot.storage.filter(
in_response_to="Do you like my hat?"
)
self.assertIsLength(statements, 1)
self.assertEqual(statements[0].search_in_response_to, "ik")
def test_database_has_correct_format(self):
"""
Test that the database maintains a valid format
when data is added and updated. This means that
after the training process, the database should
contain nine objects and eight of these objects
should list the previous member of the list as
a response.
"""
conversation = [
"Hello sir!",
"Hi, can I help you?",
"Yes, I am looking for italian parsely.",
"Italian parsely is right over here in out produce department",
"Great, thank you for your help.",
"No problem, did you need help finding anything else?",
"Nope, that was it.",
"Alright, have a great day.",
"Thanks, you too."
]
self.trainer.train(conversation)
# There should be a total of 9 statements in the database after training
self.assertEqual(self.chatbot.storage.count(), 9)
# The first statement should be in response to another statement
self.assertIsNone(
self.chatbot.storage.filter(text=conversation[0])[0].in_response_to
)
# The second statement should be in response to the first statement
self.assertEqual(
self.chatbot.storage.filter(text=conversation[1])[0].in_response_to,
conversation[0]
)
def test_training_with_unicode_characters(self):
"""
Ensure that the training method adds unicode statements
to the database.
"""
conversation = [
u'¶ ∑ ∞ ∫ π ∈ ℝ² ∖ ⩆ ⩇ ⩈ ⩉ ⩊ ⩋ ⪽ ⪾ ⪿ ⫀ ⫁ ⫂ ⋒ ⋓',
u'⊂ ⊃ ⊆ ⊇ ⊈ ⊉ ⊊ ⊋ ⊄ ⊅ ⫅ ⫆ ⫋ ⫌ ⫃ ⫄ ⫇ ⫈ ⫉ ⫊ ⟃ ⟄',
u'∠ ∡ ⦛ ⦞ ⦟ ⦢ ⦣ ⦤ ⦥ ⦦ ⦧ ⦨ ⦩ ⦪ ⦫ ⦬ ⦭ ⦮ ⦯ ⦓ ⦔ ⦕ ⦖ ⟀',
u'∫ ∬ ∭ ∮ ∯ ∰ ∱ ∲ ∳ ⨋ ⨌ ⨍ ⨎ ⨏ ⨐ ⨑ ⨒ ⨓ ⨔ ⨕ ⨖ ⨗ ⨘ ⨙ ⨚ ⨛ ⨜',
u'≁ ≂ ≃ ≄ ⋍ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ⩯ ⩰ ⫏ ⫐ ⫑ ⫒ ⫓ ⫔ ⫕ ⫖',
u'¬ ⫬ ⫭ ⊨ ⊭ ∀ ∁ ∃ ∄ ∴ ∵ ⊦ ⊬ ⊧ ⊩ ⊮ ⊫ ⊯ ⊪ ⊰ ⊱ ⫗ ⫘',
u'∧ ∨ ⊻ ⊼ ⊽ ⋎ ⋏ ⟑ ⟇ ⩑ ⩒ ⩓ ⩔ ⩕ ⩖ ⩗ ⩘ ⩙ ⩚ ⩛ ⩜ ⩝ ⩞ ⩟ ⩠ ⩢',
]
self.trainer.train(conversation)
response = self.chatbot.get_response(conversation[1])
self.assertEqual(response, conversation[2])
def test_training_with_emoji_characters(self):
"""
Ensure that the training method adds statements containing emojis.
"""
conversation = [
u'Hi, how are you? 😃',
u'I am just dandy 👍',
u'Superb! 🎆'
]
self.trainer.train(conversation)
response = self.chatbot.get_response(conversation[1])
self.assertEqual(response, conversation[2])
def test_training_with_unicode_bytestring(self):
"""
Test training with an 8-bit bytestring.
"""
conversation = [
'Hi, how are you?',
'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x90\x97',
'Superb!'
]
self.trainer.train(conversation)
response = self.chatbot.get_response(conversation[1])
self.assertEqual(response, conversation[2])
def test_similar_sentence_gets_same_response_multiple_times(self):
"""
Tests if the bot returns the same response for the same
question (which is similar to the one present in the training set)
when asked repeatedly.
"""
training = [
'how do you login to gmail?',
'Goto gmail.com, enter your login information and hit enter!'
]
similar_question = 'how do I login to gmail?'
self.trainer.train(training)
response_to_trained_set = self.chatbot.get_response('how do you login to gmail?')
response1 = self.chatbot.get_response(similar_question)
response2 = self.chatbot.get_response(similar_question)
self.assertEqual(response_to_trained_set, response1)
self.assertEqual(response1.text, response2.text)
def test_consecutive_trainings_same_responses_different_inputs(self):
"""
Test consecutive trainings with the same responses to different inputs.
"""
self.trainer.train(["A", "B", "C"])
self.trainer.train(["B", "C", "D"])
response1 = self.chatbot.get_response("B")
response2 = self.chatbot.get_response("C")
self.assertEqual(response1.text, "C")
self.assertEqual(response2.text, "D")
class ChatterBotResponseTests(ChatBotTestCase):
def setUp(self):
super(ChatterBotResponseTests, self).setUp()
"""
Set up a database for testing.
"""
self.trainer = ListTrainer(
self.chatbot,
show_training_progress=False
)
data1 = [
"african or european?",
"Huh? I... I don't know that.",
"How do you know so much about swallows?"
]
data2 = [
"Siri is adorable",
"Who is Seri?",
"Siri is my cat"
]
data3 = [
"What... is your quest?",
"To seek the Holy Grail.",
"What... is your favourite colour?",
"Blue."
]
self.trainer.train(data1)
self.trainer.train(data2)
self.trainer.train(data3)
def test_answer_to_known_input(self):
"""
Test that a matching response is returned
when an exact match exists.
"""
input_text = "What... is your favourite colour?"
response = self.chatbot.get_response(input_text)
self.assertIn("Blue", response.text)
def test_answer_close_to_known_input(self):
input_text = "What is your favourite colour?"
response = self.chatbot.get_response(input_text)
self.assertIn("Blue", response.text)
def test_match_has_no_response(self):
"""
Make sure that the if the last line in a file
matches the input text then a index error does
not occur.
"""
input_text = "Siri is my cat"
response = self.chatbot.get_response(input_text)
self.assertGreater(len(response.text), 0)
def test_empty_input(self):
"""
If empty input is provided, anything may be returned.
"""
response = self.chatbot.get_response("")
self.assertTrue(len(response.text) >= 0)