-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot.cpp
More file actions
135 lines (117 loc) · 2.96 KB
/
Copy pathChatbot.cpp
File metadata and controls
135 lines (117 loc) · 2.96 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
#include <iostream>
#include <string>
#include <list>
#include <fstream>
class words
{
private :
std::list<std::string> m_sentenceList;
std::list<std::string> m_totalWords;
int m_sencence_counter;
static const int m_sentenceSize = 1000;
int m_wordsNumb;
std::string m_sentence[m_sentenceSize];
std::string m_storedWords[m_sentenceSize];
public :
words()
{
m_wordsNumb = 0;
loadWords();
}
void talk(std::string sentence)
{
sentenceSplitt(sentence);
storeWords();
}
void sentencePrint()
{
for(int i = 0;i < m_wordsNumb; i++)
{
std::cout <<"Word nr :" << i <<" is :" << m_storedWords[i] <<std::endl;
}
}
void storeWords()
{
for(int i = 0;i < m_sencence_counter;i++)
{
bool neword = true;
for(int e = 0; e < m_wordsNumb; e++)
{
if (m_sentence[i] == m_storedWords[e])
{
neword = false;
}
}
if (neword == true)
{
m_storedWords[m_wordsNumb] = m_sentence[i];
m_wordsNumb++;
std::cout <<"added :" << m_sentence[i] << " to known Words." << std::endl;
}
}
}
void saveWords()
{
std::ofstream filer;
filer.open("words.txt", std::ofstream::out);
for(int i = 0; i < m_wordsNumb; i++)
{
filer << m_storedWords[i] << std::endl;
}
std::cout << "sucessfully stored words to file !" << std::endl;
filer.close();
}
void loadWords()
{
std::ifstream filer;
filer.open("words.txt");
int i = 0;
while(getline(filer,m_storedWords[i]))
{
i++;
m_wordsNumb++;
}
filer.close();
std::cout << "sucessfully loaded words from file !" << std::endl;
}
void sentenceSplitt(std::string line)
{
m_sencence_counter = 0;
std::string cut_line_x = line;
std::string worder;
int x1 = 0;
bool done = false;
while(done == false)
{
x1 = cut_line_x.find_first_of(' ');
if(x1 != std::string::npos)
{
worder = cut_line_x.substr(0,x1);
cut_line_x = cut_line_x.substr(x1+1);
}
else
{
worder = cut_line_x;
done = true;
}
//std::cout << "worder :" << worder <<std::endl;
m_sentence[m_sencence_counter] = worder;
m_sencence_counter++;
}
}
};
int main()
{
words Asterix;
bool exit = false;
while(exit == false)
{
std::string say;
std::getline(std::cin, say);
Asterix.talk(say);
if(say == "exit")
exit = true;
}
Asterix.sentencePrint();
Asterix.saveWords();
}