-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.cpp
203 lines (172 loc) · 5.72 KB
/
chatbot.cpp
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
#include <iostream>
#include <random>
#include <algorithm>
#include <ctime>
#include "chatlogic.h"
#include "graphnode.h"
#include "graphedge.h"
#include "chatbot.h"
// constructor WITHOUT memory allocation
ChatBot::ChatBot()
{
// invalidate data handles
_image = nullptr;
_chatLogic = nullptr;
_rootNode = nullptr;
}
// constructor WITH memory allocation
ChatBot::ChatBot(std::string filename)
{
std::cout << "ChatBot Constructor" << std::endl;
// invalidate data handles
_chatLogic = nullptr;
_rootNode = nullptr;
// load image into heap memory
_image = new wxBitmap(filename, wxBITMAP_TYPE_PNG);
}
ChatBot::~ChatBot()
{
std::cout << "ChatBot Destructor" << std::endl;
// deallocate heap memory
if(_image != NULL) // Attention: wxWidgets used NULL and not nullptr
{
delete _image;
_image = NULL;
}
}
//// STUDENT CODE
////
ChatBot::ChatBot(const ChatBot &source) // 2 : copy constructor
{
_image = new wxBitmap();
*_image = *source._image;
_chatLogic = source._chatLogic;
_rootNode = source._rootNode;
_currentNode = source._currentNode;
std::cout << "COPYING content of instance " << &source << " to instance " << this << std::endl;
}
ChatBot& ChatBot::operator=(const ChatBot &source) // 3 : copy assignment operator
{
std::cout << "ASSIGNING content of instance " << &source << " to instance " << this << std::endl;
if (this == &source)
return *this;
delete _image;
_image = new wxBitmap();
*_image = *source._image;
_chatLogic = source._chatLogic;
_rootNode = source._rootNode;
_currentNode = source._currentNode;
return *this;
}
ChatBot::ChatBot(ChatBot &&source) // 4 : move constructor
{
std::cout << "MOVING (c’tor) instance " << &source << " to instance " << this << std::endl;
_image = source._image;
_chatLogic = source._chatLogic;
_rootNode = source._rootNode;
_currentNode = source._currentNode;
source._image = nullptr;
source._chatLogic = nullptr;
source._rootNode = nullptr;
source._currentNode = nullptr;
}
ChatBot& ChatBot::operator=(ChatBot &&source) // 5 : move assignment operator
{
std::cout << "MOVING (assign) instance " << &source << " to instance " << this << std::endl;
if (this == &source)
return *this;
delete _image;
_image = source._image;
_chatLogic = source._chatLogic;
_rootNode = source._rootNode;
_currentNode = source._currentNode;
source._image = nullptr;
source._chatLogic = nullptr;
source._rootNode = nullptr;
source._currentNode = nullptr;
return *this;
}
////
//// EOF STUDENT CODE
void ChatBot::ReceiveMessageFromUser(std::string message)
{
// loop over all edges and keywords and compute Levenshtein distance to query
typedef std::pair<GraphEdge *, int> EdgeDist;
std::vector<EdgeDist> levDists; // format is <ptr,levDist>
for (size_t i = 0; i < _currentNode->GetNumberOfChildEdges(); ++i)
{
GraphEdge *edge = _currentNode->GetChildEdgeAtIndex(i);
for (auto keyword : edge->GetKeywords())
{
EdgeDist ed{edge, ComputeLevenshteinDistance(keyword, message)};
levDists.push_back(ed);
}
}
// select best fitting edge to proceed along
GraphNode *newNode;
if (levDists.size() > 0)
{
// sort in ascending order of Levenshtein distance (best fit is at the top)
std::sort(levDists.begin(), levDists.end(), [](const EdgeDist &a, const EdgeDist &b) { return a.second < b.second; });
newNode = levDists.at(0).first->GetChildNode(); // after sorting the best edge is at first position
}
else
{
// go back to root node
newNode = _rootNode;
}
// tell current node to move chatbot to new node
_currentNode->MoveChatbotToNewNode(newNode);
}
void ChatBot::SetCurrentNode(GraphNode *node)
{
// update pointer to current node
_currentNode = node;
// select a random node answer (if several answers should exist)
std::vector<std::string> answers = _currentNode->GetAnswers();
std::mt19937 generator(int(std::time(0)));
std::uniform_int_distribution<int> dis(0, answers.size() - 1);
std::string answer = answers.at(dis(generator));
// send selected node answer to user
_chatLogic->SendMessageToUser(answer);
}
int ChatBot::ComputeLevenshteinDistance(std::string s1, std::string s2)
{
// convert both strings to upper-case before comparing
std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
std::transform(s2.begin(), s2.end(), s2.begin(), ::toupper);
// compute Levenshtein distance measure between both strings
const size_t m(s1.size());
const size_t n(s2.size());
if (m == 0)
return n;
if (n == 0)
return m;
size_t *costs = new size_t[n + 1];
for (size_t k = 0; k <= n; k++)
costs[k] = k;
size_t i = 0;
for (std::string::const_iterator it1 = s1.begin(); it1 != s1.end(); ++it1, ++i)
{
costs[0] = i + 1;
size_t corner = i;
size_t j = 0;
for (std::string::const_iterator it2 = s2.begin(); it2 != s2.end(); ++it2, ++j)
{
size_t upper = costs[j + 1];
if (*it1 == *it2)
{
costs[j + 1] = corner;
}
else
{
size_t t(upper < corner ? upper : corner);
costs[j + 1] = (costs[j] < t ? costs[j] : t) + 1;
}
corner = upper;
}
}
int result = costs[n];
delete[] costs;
return result;
}