-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.cpp
More file actions
494 lines (420 loc) · 13.6 KB
/
Dictionary.cpp
File metadata and controls
494 lines (420 loc) · 13.6 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// "Dictionary.cpp" file
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <random>
#include <ctime>
#include <sstream>
#include <cctype>
#include "Dictionary.h"
using namespace std;
// Implementation of other supportive functions which do not belong to a class
// Function to make all the characters of a word lowercase and replace all the spaces in the word by hyphens
string format_string(string word)
{
string format_word;
// Make all the characters lowercase
for (int i = 0; i < word.length(); i++)
{
format_word += tolower(word[i]);
}
// Replace the spaces in the word by hyphens
size_t start = 0;
while ((format_word.find(" ", start)) != string::npos)
{
size_t x = format_word.find(" ", start);
format_word.replace(x, 1, "-");
start += x;
}
return format_word;
}
// Function to count the number of words in a string
int countWords(string str)
{
stringstream stream(str); // Create a stringstream from the input string
string word;
int wordCount = 0;
// Loop through each word in the stringstream
while (stream >> word)
{
wordCount++;
}
return wordCount;
}
// Function to generate a random number
int randomNumber(int lower_bound, int upper_bound)
{
mt19937 generator(static_cast<unsigned int>(time(0))); // Seed the random number generator with the current time
uniform_int_distribution<int> distribution(lower_bound, upper_bound); // Define the distribution
int randNum = distribution(generator); // Generate a random number between lower bound and upper bound
return randNum;
}
// Function to check whether there are only a-z characters and the hyphen in a string
bool checkWord(string word)
{
for (char c : word)
{
if ((!isalpha(c)) and (c != '-'))
{
return false; // Return false if invalid character is found
}
}
return true; // Return true if all the characters are letters a-z and hyphens
}
// Implementation of constructor and methods of "Dictionary" class
// Constructor
Dictionary::Dictionary() {}
// Getters
vector<Word> Dictionary::getWordlist()
{
return this->wordlist;
}
int Dictionary::getHighScore()
{
return this->highScore;
}
// Setters
void Dictionary::setWordlist(Word word)
{
this->wordlist.push_back(word);
}
void Dictionary::setHighScore(int newHighScore)
{
this->highScore = newHighScore;
}
// Method to load the dictionary
bool Dictionary::load(string fileName)
{
// Open the the dictionary file
ifstream file(fileName);
// Load the information for each word and add them into the dictionary
if (file.is_open())
{
string type;
string definition;
string name;
string blank_line;
while (getline(file, type))
{
type = type.erase(0, 6); // Remove the text "Type: " and load
getline(file, definition);
definition = definition.erase(0, 12); // Remove the text "Definition: " and load
getline(file, name);
name = name.erase(0, 6); // Remove the text "Word: " and load
getline(file, blank_line);
// Add the information of word to a Word object
Word word(name, type, definition);
// Add each word into the dictionary
setWordlist(word);
}
file.close();
cout << "Dictionary loaded and parsed successfully..." << "\n";
return true;
}
else
{
cout << "Error opening the dictionary file!" << "\n";
return false;
}
}
// Method to search for a word in the dictionary and output it's information
void Dictionary::search(string term)
{
string format_term = format_string(term); // Make the input lowercase and replace the spaces in the input with hyphens
// Searching
for (auto &wordObj : getWordlist())
{
// Word found
if (wordObj.getName() == format_term)
{
wordObj.printDefinition();
return;
}
}
// Throw an exception if the word not found
throw out_of_range("Word not found!");
}
// Method to list down all the palindromes in the dictionary
void Dictionary::findPalindromes()
{
cout << "Palindromes," << "\n";
for (auto &wordObj : getWordlist())
{
string word = wordObj.getName();
string reversed_word;
reverse_copy(word.begin(), word.end(), back_inserter(reversed_word));
// Check whether the word is a palindrome
if (word == reversed_word)
{
cout << " " << word << "\n";
}
}
}
// Method to list down all the rhyming words to a given word
void Dictionary::findRhymingWords(string word)
{
int count = 0;
if (word.length() >= 3)
{
string str_1 = word.substr(word.length() - 3); // Separate the last 3 letters of the given word
cout << "Rhyming words to \"" << word << "\"," << "\n";
for (auto &wordObj : getWordlist())
{
string curr_word = wordObj.getName();
if (curr_word.length() >= 3)
{
string str_2 = curr_word.substr(curr_word.length() - 3); // Separate the last 3 letters of the current word
// Check whether the word rhymes with the given word
if (str_1 == str_2)
{
count++;
cout << " " << curr_word << "\n";
}
}
}
if (count == 0)
{
cout << " ......" << "\n\n";
cout << "Sorry, no rhyming words to \"" << word << "\"," << "\n";
}
else
{
cout << "\n";
cout << count << " rhyming words found!" << "\n";
}
}
else
{
cout << "The word should contain 3 or more letters!" << "\n";
}
}
// Method for the game "Guess the fourth word"
void Dictionary::guessTheWord()
{
int score = 0;
int n = 0;
cout << "------ Welcome to \"Guess the fourth word\"...! ------" << "\n\n";
cout << "Current high score: " << getHighScore() << "\n\n";
while (true)
{
int randNum = randomNumber(0, getWordlist().size() - 1); // Generate a random number between 0 and the length of the wordlist
Word randWord = getWordlist()[randNum]; // Choose a random word from the dictionary
if (countWords(randWord.getDefinition()) >= 4)
{
stringstream stream(randWord.getDefinition()); // Create a stringstream from the definition of the word
vector<string> words;
string word;
string guess;
// Split the input string into words
while (stream >> word)
{
words.push_back(word);
}
string correct_word = words[3];
words[3] = string(words[3].size(), '_'); // Replace the 4th word with underscores
// Reset the stringstream and concatenate the modified words back into a string
stream.str("");
stream.clear();
for (const auto &word : words)
{
stream << word << " ";
}
string defWithBlank = stream.str().substr(0, stream.str().size() - 1); // Remove the trailing space
// Implementation of the game
cout << "----------------------------------------------------" << "\n";
cout << "Guess the missing word of the definition," << "\n\n";
cout << " Word: " << randWord.getName() << "\n";
cout << " Definition: " << defWithBlank << "\n\n";
cout << "Your guess: ";
getline(cin, guess);
cout << "\n";
if (guess == correct_word)
{
cout << "Congratulations! your guess is correct!" << "\n\n";
score += 10;
if (score > getHighScore())
{
n++;
setHighScore(score);
if (n == 1)
{
cout << "Congratulations! you have beaten the highest score!" << "\n\n";
}
}
}
else
{
cout << "Your guess is incorrect!" << "\n";
cout << "The correct answer is, \"" << correct_word << "\"" << "\n";
cout << "Your score: " << score << "\n";
break;
}
}
}
}
// Method to add a new word to the dictionary
void Dictionary::addWord()
{
string name, type, definition, fileName;
// Input name
cout << "Enter the word: ";
getline(cin, name);
cout << "\n";
name = format_string(name); // Make the name lowercase and replace the spaces in the name with hyphens
// Check whether the name only contains a-z characters and the hyphen
if (checkWord(name) == false)
{
cout << "Invalid word!" << "\n";
return;
}
// Check whether the word already exists in the dictionary
for (auto &wordObj : getWordlist())
{
// Word exists
if (wordObj.getName() == name)
{
cout << "error: word exists, elevated privileges required to edit existing words" << "\n";
return;
}
}
// Input type
cout << "Enter the type: ";
getline(cin, type);
cout << "\n";
// Check whether the type is valid
if (type == "v" or type == "verb")
{
type = "v";
}
else if (type == "n" or type == "noun")
{
type = "n";
}
else if (type == "adv" or type == "adverb")
{
type = "adv";
}
else if (type == "adj" or type == "adjective")
{
type = "adj";
}
else if (type == "prep" or type == "preposition")
{
type = "prep";
}
else if (type == "pn" or type == "proper noun")
{
type = "pn";
}
else if (type == "n_and_v" or type == "noun and a verb")
{
type = "n_and_v";
}
else if (type == "misc" or type == "other words")
{
type = "misc";
}
else
{
cout << "Invalid type!" << "\n";
return;
}
// Input definition
cout << "Enter the definition: ";
getline(cin, definition);
cout << "\n";
// Input file name
cout << "Enter a name for the file to store the updated dictionary (with the extension \".txt\"): ";
getline(cin, fileName);
cout << "\n";
// Create a new Word instance
Word newWord(name, type, definition);
// Add the new word into the dictionary
setWordlist(newWord);
// Save the updated dictionary to a new file
ofstream outputFile(fileName); // Create an ofstream object and open the file
// Check whether the file is opened successfully
if (outputFile.is_open())
{
// Write data to the file
for (auto &wordObj : getWordlist())
{
outputFile << "Type: " << wordObj.getType() << "\n";
outputFile << "Definition: " << wordObj.getDefinition() << "\n";
outputFile << "Word: " << wordObj.getName() << "\n";
outputFile << "\n";
}
outputFile.close();
cout << "File \"" << fileName << "\" has been created and saved the updated dictionary successfully...\n";
}
else
{
cout << "Error opening the file \"" << fileName << "\" for writing!\n";
}
}
// Method to execute the menu
void Dictionary::menu()
{
while (true)
{
string n;
string word;
// Menu
cout << "------------------------------------------------------------------------------------------------" << "\n";
cout << "Dictionary..." << "\n";
cout << " Enter '1' to search a word" << "\n";
cout << " Enter '2' to list all the palindromes in the dictionary" << "\n";
cout << " Enter '3' to find rhyming words to a word" << "\n";
cout << " Enter '4' to play the game \"Guess the fourth word\"" << "\n";
cout << " Enter '5' to add a new word to the dictionary" << "\n";
cout << " Enter '6' to exit" << "\n\n";
cout << "Enter a number to continue: ";
cin >> n;
cin.ignore();
cout << "\n";
// Execution of tasks
if (n == "1")
{ // Search for a word if '1' is entered
cout << "Enter a word to search: ";
getline(cin, word);
cout << "\n";
try
{
search(word); // Search for the entered word in the dictionary
}
catch (const std::out_of_range &exception)
{
cerr << exception.what() << "\n"; // Handle the exception
}
}
else if (n == "2")
{ // List down palindromes if '2' is entered
findPalindromes();
}
else if (n == "3")
{ // Find rhyming words if '3' is entered
cout << "Enter a word to find rhyming words: ";
getline(cin, word);
cout << "\n";
findRhymingWords(word); // Find rhyming words to the entered word
}
else if (n == "4")
{ // Execute the game "Guess the fourth word" if '4' is entered
guessTheWord();
}
else if (n == "5")
{ // Add a word to the dictionary if '5' is entered
addWord();
}
else if (n == "6")
{ // Exit the loop if '6' is entered
break;
}
else
{ // Output "Invalid number!" if anything else is inputted
cout << "Invalid number!" << "\n";
}
cout << "\n";
}
}