-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord.cpp
70 lines (59 loc) · 1.29 KB
/
Word.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
//
// Created by Matthew Jones on 11/14/16.
//
#include <cstdlib>
#include <ctime>
#include "Word.h"
using json = nlohmann::json;
Word::Word(std::string w) {
if(w.length() > 1) {
switch (w.at(0)) {
case '.':
case ',':
case '?':
case '!':
case ':':
case ';':
case '\n':
w.erase(w.begin());
break;
default:
break;
}
}
word = w;
srand(time(NULL));
}
Word* Word::getRandWordFollowing() const{
int randomIndex = rand() % wordsFollowing.size();
return wordsFollowing[randomIndex];
}
std::vector<Word*> Word::getWordsFollowing() const{
return wordsFollowing;
}
void Word::addWordFollowing(Word *wordToAdd) {
wordsFollowing.push_back(wordToAdd);
}
std::string Word::toString() const {
// std::string rval = "{\"Word\": \"" + word + "\"\n\t\"wordsFollowing\": [";
// for(int i = 0; i < wordsFollowing.size(); i++) {
// rval.append("\n\t\t\"").append(wordsFollowing[i]->getWord()).append("\",");
// }
// rval.pop_back();
// rval.append("\n\t]\n}");
// return rval;
return toJSON().dump();
}
std::string Word::getWord() const {
return word;
}
json Word::toJSON() const {
json rval;
rval["String"] = word;
json arr;
for(int i = 0; i < wordsFollowing.size(); i++) {
arr.push_back(wordsFollowing[i]->getWord());
}
rval["Words Following"] = arr;
return rval;
}