-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
270 lines (238 loc) · 6.62 KB
/
main.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
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
// Developed by Joan Rios i Pla.
// It's not possible to make this without human imput, we will persent the human with possible options.
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <conio.h>
// #include "functions.hpp"
using namespace std;
typedef struct
{
string text = "[]";
string type = "[]";
int length = 0;
} word;
vector<word> options;
vector<word> sentence;
const int numberOfWordFiles = 10;
string typesOfWords[numberOfWordFiles] = {"noun", "pronoun", "verb", "adjective", "adverb", "preposition",
"determiner", "conjunction", "interjection", "pi"};
vector<word> words[numberOfWordFiles];
string pi;
int current = 0;
void updateOptions();
string endOfSentence();
void flushOptions();
void flushSentence();
void addToOptions(word addition);
void addToSentence(word input);
vector<word> getWordsOfTypeAndLen(int type, int len);
string strPi();
string strUsage();
int main(void)
{
// Start rand()
srand((unsigned) time(NULL));
flushOptions();
flushSentence();
// Testing stuff
// Load files into vectors.
{
int time_before_load = time(NULL);
for (int i = 0; i < numberOfWordFiles; i++)
{
fstream tmp;
tmp.open("words/" + typesOfWords[i] + ".csv", ios::in);
if (!tmp)
{
cout << "File not found: " << typesOfWords[i] << endl
<< "Attempting creation of file: " << typesOfWords[i] << endl;
tmp.open("words/" + typesOfWords[i] + ".csv", ios::out | ios::in | ios::app);
if (tmp)
{
cout << "Creation succesful";
}
else
{
cout << "Failed.";
return 1;
}
}
else
{
cout << "Opened " << typesOfWords[i];
}
cout << endl;
// Load into vector;
cout << "Loading file into vector ... ";
if (typesOfWords[i] != "pi")
{
while (!tmp.eof())
{
string str;
getline(tmp, str);
word idk = {str, typesOfWords[i], (int) str.length()};
words[i].push_back(idk);
}
}
else
{
getline(tmp, pi);
}
cout << "Should be loaded" << endl;
// Close file
tmp.close();
}
// TOFIX
int time_after_load = time(NULL);
int time_diff = time_after_load - time_before_load;
cout << "Loaded in " << time_diff << " seconds." << endl;
}
// Usage
cout << strUsage();
cout << endl << "Press any key to continue.";
getch();
putchar('\n');
string input;
while (true)
{
cout << endl
<< "End of current text: " << endOfSentence() << endl
<< "Pi: " << strPi() << endl
<< "Chose one of the following or write a word:" << endl;
updateOptions();
for (int i = 0; i < options.size(); i++)
{
cout << i << ": " << options[i].text << endl;
}
cout << endl;
cin >> input;
if (isdigit(input.at(0)))
{
if(input.length() == 1) addToSentence(options[input.at(0)- '0']);
if(input.length() == 2) addToSentence(options[(input.at(0) - '0') * 10 + (input.at(1) - '0')]);
}
else if (isascii(input.at(0)))
{
if (input.at(0) == '/')
{
// Commands
if (input == "/sentence")
{
if (sentence.size() == 0) cout << "Empty sentence.";
for (int i = 0; i < sentence.size(); i++)
{
cout << sentence[i].text << " ";
}
putchar('\n');
}
else if (input == "/help" || input == "/usage")
{
cout << strUsage();
}
else
{
cout << "\033[31m" << "Unknown command " << "\033[m";
}
cout << endl << "Press any key to continue.";
getch();
putchar('\n');
}
else
{
addToSentence(*(new word {input, "[userInputed]", (int) input.length()}));
}
}
}
}
void updateOptions()
{
flushOptions();
int chars = pi.at(current) - '0';
for (int i = 0; i < numberOfWordFiles - 1; i++)
{
vector<word> all = getWordsOfTypeAndLen(i, chars);
if (all.size() >= 3)
{
for(int i = 0; i < 3; i++) addToOptions(all[rand() % all.size()]);
}
else if (all.size() == 2)
{
for(int i = 0; i < 2; i++) addToOptions(all[i]);
}
else if (all.size() == 1)
{
addToOptions(all[0]);
}
}
}
string endOfSentence()
{
string out = "";
if (sentence.size() >= 5)
out += "... ";
if (sentence.size() >= 4)
out += sentence[sentence.size() - 4].text + " ";
if (sentence.size() >= 3)
out += sentence[sentence.size() - 3].text + " ";
if (sentence.size() >= 2)
out += sentence[sentence.size() - 2].text + " ";
if (sentence.size() >= 1)
out += sentence[sentence.size() - 1].text;
return out;
}
void flushOptions()
{
options.clear();
}
void flushSentence()
{
sentence.clear();
}
void addToOptions(word addition)
{
options.push_back(addition);
}
void addToSentence(word addition)
{
sentence.push_back(addition);
current++;
}
vector<word> getWordsOfTypeAndLen(int type, int len)
{
vector<word> out;
for (auto i = words[type].begin(); i != words[type].end(); ++i)
{
if ((*i).length == len)
{
out.push_back(*i);
}
}
return out;
}
string strPi()
{
string out = "";
for (int i = 1; i <= 2; i++)
{
if(current - i >= 0) out += pi.at(current - i);
}
out += "\033[32m";
out += pi.at(current);
out += "\033[m";
out += pi.at(current + 1);
out += pi.at(current + 2);
return out;
}
string strUsage()
{
string out;
out += "\n";
out += "\033[32mUsage:\033[m\n";
out += "Chose from the list of matching words by typing the corresponding number.\n";
out += "If you want to see the whole sentence, type \033[34m/sentence\033[m.\n";
return out;
}