-
Notifications
You must be signed in to change notification settings - Fork 0
/
PROJECT.cpp
202 lines (184 loc) · 5.66 KB
/
PROJECT.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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
struct FileHandler{
string toStore;
string myText;
vector<string> read(string fileName){
vector<string> words;
ifstream file;
file.open(fileName);
string word;
while (file >> word){ // this operator >> sepate words on spaces in file.
words.push_back(word); // these word are added in vector words.
}
return words;
}
void write(string fileName){
ifstream readFile(fileName);
vector<string> update;
while (!readFile.eof()){ // adding words from file to vector.
cin.ignore();
getline(readFile, myText);
update.push_back(myText);
}
string allString = "";
for (auto stringVec : update){ // separate words in vector through spaces.
allString = allString + " " + stringVec;
}
string fileWords = allString; // each word in file is separated.
cout << "Write to store: ";
getline(cin, toStore);
fileWords = fileWords + " " + toStore; // new word is store with old words but not yet in file.
ofstream writeFile(fileName);
writeFile << fileWords; // all words are again stored back in the file.
}
};
struct Find{
void find(vector<string> var, string paragraph){
istringstream iss(paragraph); // It divide the paragraph into different varible
string s; // istringstream to separate the paragraph into individual words.
while (getline(iss, s, ' ')){ // paragraph is now iss and each word is storing to s.
string word = s.c_str(); // c_str() divide the string in char but last char must be null
if (count(var.begin(), var.end(), word)){
cout << word << " ";
}
}
cout << endl;
}
};
class Verb{
public:
vector<string> read(){
FileHandler fileHandler;
vector<string> verbs = fileHandler.read("verb.txt"); // sending file name to FileHandler struct
return verbs;
}
void write(){
FileHandler fileHandler;
fileHandler.write("verb.txt");
}
void find(vector<string> verbs, string paragraph){
cout << "Verb: ";
Find find;
find.find(verbs, paragraph);
}
};
class Noun{
public:
vector<string> read(){
FileHandler fileHandler;
vector<string> noun = fileHandler.read("noun.txt");
return noun;
}
void write(){
FileHandler fileHandler;
fileHandler.write("noun.txt");
}
void find(vector<string> noun, string paragraph){
cout << "noun: ";
Find find;
find.find(noun, paragraph);
}
};
class Article{
public:
vector<string> read(){
FileHandler fileHandler;
vector<string> article = fileHandler.read("article.txt");
return article;
}
void write(){
FileHandler fileHandler;
fileHandler.write("article.txt");
}
void find(vector<string> article, string paragraph){
cout << "Article: ";
Find find;
find.find(article, paragraph);
}
};
class Pronoun{
public:
vector<string> read(){
FileHandler fileHandler;
vector<string> pronoun = fileHandler.read("pronoun.txt");
return pronoun;
}
void write(){
FileHandler fileHandler;
fileHandler.write("pronoun.txt");
}
void find(vector<string> pronoun, string paragraph){
cout << "Pronoun: ";
Find find;
find.find(pronoun, paragraph);
}
};
class Preposition{
public:
vector<string> read(){
FileHandler fileHandler;
vector<string> prep = fileHandler.read("preposition.txt");
return prep;
}
void write(){
FileHandler fileHandler;
fileHandler.write("preposition.txt");
}
void find(vector<string> prep, string paragraph){
cout << "Preposition: ";
Find find;
find.find(prep, paragraph);
}
};
int main(){
char cont;
do{
Verb verb;
Noun noun;
Article article;
Pronoun pronoun;
Preposition prep;
vector<string> verbs = verb.read();
vector<string> nouns = noun.read();
vector<string> articles = article.read();
vector<string> pronouns = pronoun.read();
vector<string> preps = prep.read();
char option;
cout << "For updating, enter 'u'. For finding enter 'f': ";
cin >> option;
if (option == 'u'){
char addOpt;
cout << "For Verb adding, Press 'v'. For noun adding, Press 'n': ";
cin >> addOpt;
if (addOpt == 'v'){
verb.write();
} else if (addOpt == 'n'){
noun.write();
} else{
cout << "invalid.";
}
} else if (option == 'f'){
string paragraph;
cout << "Enter a paragraph or word: ";
cin.ignore();
getline(cin, paragraph);
verb.find(verbs, paragraph);
noun.find(nouns, paragraph);
article.find(articles, paragraph);
pronoun.find(pronouns, paragraph);
prep.find(preps, paragraph);
} else{
cout << "Invalid option.";
}
cout << endl
<< "To continue, press 'c': ";
cin >> cont;
} while (cont == 'c');
return 0;
}