-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (138 loc) · 3.45 KB
/
Copy pathmain.cpp
File metadata and controls
174 lines (138 loc) · 3.45 KB
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cctype>
std::string normalize(std::string text)
{
std::string result;
bool previousSpace = false;
for (char c : text)
{
if (std::ispunct(static_cast<unsigned char>(c)))
continue;
c = std::tolower(static_cast<unsigned char>(c));
if (std::isspace(static_cast<unsigned char>(c)))
{
if (!previousSpace)
{
result += ' ';
previousSpace = true;
}
}
else
{
result += c;
previousSpace = false;
}
}
// Deleting space in start
if (!result.empty() && result.front() == ' ')
result.erase(result.begin());
// Deleting space in end
if (!result.empty() && result.back() == ' ')
result.pop_back();
return result;
}
std::string toLower(std::string text)
{
for (char& c : text)
{
c = std::tolower(static_cast<unsigned char>(c));
}
return text;
}
struct Knowledge
{
std::string question;
std::string answer;
};
std::vector<Knowledge> knowledge;
// Loading knowledge from a file
void loadKnowledge()
{
std::ifstream file("knowledge.txt");
if (!file.is_open())
return;
std::string line;
std::string question;
std::string answer;
while (std::getline(file, line))
{
if (line.rfind("Q: ", 0) == 0)
{
question = line.substr(3);
}
else if (line.rfind("A: ", 0) == 0)
{
answer = line.substr(3);
knowledge.push_back({ question, answer });
question.clear();
answer.clear();
}
}
file.close();
}
// Saving new knowledge
void saveKnowledge(const std::string& question, const std::string& answer)
{
std::ofstream file("knowledge.txt", std::ios::app);
if (!file.is_open())
{
std::cout << "Error: cant open file!\n";
return;
}
file << "Q: " << question << "\n";
file << "A: " << answer << "\n\n";
file.close();
}
// Searching for answer
std::string findAnswer(const std::string& question)
{
std::string normalizedQuestion = normalize(question);
for (const auto& item : knowledge)
{
if (normalize(item.question) == normalizedQuestion)
{
return item.answer;
}
}
return "";
}
int main()
{
// Loading old knowledge
loadKnowledge();
std::cout << "AI launched!\n";
std::cout << "Enter 'exit' to exit\n";
while (true)
{
std::string request;
std::cout << "You: ";
std::getline(std::cin, request);
if (request == "exit" || request == "Exit")
break;
// searching answer
std::string answer = findAnswer(request);
if (!answer.empty())
{
// if know
std::cout << "AI: " << answer << "\n\n";
}
else
{
// if unknow
std::cout << "AI: I dont know answer\n";
std::cout << "AI: Please, Teach me!\n";
std::string newAnswer;
std::cout << "You: ";
std::getline(std::cin, newAnswer);
// Adding new knowledge
knowledge.push_back({ request, newAnswer });
// Saving on disk
saveKnowledge(request, newAnswer);
std::cout << "AI: good, I remembered!\n\n";
}
}
return 0;
}