-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternSearching.cpp
248 lines (235 loc) · 7.46 KB
/
PatternSearching.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
#include "PatternSearching.h"
#include "RE.h"
#include "utils.h"
PatternSearching::PatternSearching(string b, string re) {
/*
constructor voor wanneer de user 1 regex mee geeft.
*/
REQUIRE(b != "", "de naam van de tesktbestand mag niet leeg zijn");
REQUIRE(re != "", "de eerste regex mag niet leeg zijn");
regex1 = std::move(re);
tekstBestand = std::move(b);
_initCheck = this;
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
PatternSearching::PatternSearching(string b, string re1, string re2, bool u) {
/*
constructor voor wanneer de user 2 regexen mee geeft.
*/
REQUIRE(b != "", "de naam van de tesktbestand mag niet leeg zijn");
REQUIRE(re1 != "", "de eerste regex mag niet leeg zijn");
REQUIRE(re2 != "", "de 2de regex mag niet leeg zijn");
regex1 = std::move(re1);
regex2 = std::move(re2);
tekstBestand = std::move(b);
UorD = u;
_initCheck = this;
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
bool PatternSearching::properlyInitialized() {
/*
checkt of de aangeroepen functie juist is geinitialiseerd.
*/
return _initCheck == this;
}
string PatternSearching::getRegex1() {
/*
returned de eerste regex
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling getRegex1");
return regex1;
}
string PatternSearching::getRegex2() {
/*
returned de de regex
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling getRegex2");
return regex2;
}
string PatternSearching::getTekstBestand() {
/*
returned de naam van het tekstbestand
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling getTekstBestand");
return tekstBestand;
}
bool PatternSearching::getUnieOrFalse(){
/*
returned false voor unie of true voor doorsnede
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling getUnieOrFalse");
return UorD;
}
void PatternSearching::search() {
/*
in deze functie zoek ik de pattern in de tekst dat de we krijgen en return ik de positie waar de patterns zijn.
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling search");
if (regex2.empty()){
RE re(regex1, 'e');
ENFA enfa = re.toENFA();
DFA dfa = enfa.toDFA();
DFA minDfa = dfa.minimize();
ifstream file;
file.open(tekstBestand);
if (!file.is_open()) return;
string line;
vector<string> posities;
int x = 0;
int y = 0;
while (getline(file, line)){
istringstream iss(line);
do {
string word;
iss >> word;
if (!word.empty()) {
if (minDfa.accepts(word)) {
string positie = "(" + to_string(x) + ", " + to_string(y) + ")";
posities.push_back(positie);
x += 1;
} else {
x += 1;
}
}
} while (iss);
y+= 1;
x = 0;
}
for (int i = 0; i < posities.size(); ++i) {
cout << posities[i];
if (i != posities.size() -1)
cout << ", ";
}
}
else{
RE re(regex1, 'e');
RE re2(regex2, 'e');
ENFA enfa = re.toENFA();
ENFA enfa2 = re2.toENFA();
DFA dfa = enfa.toDFA();
DFA dfa2 = enfa2.toDFA();
DFA mindfa = dfa.minimize();
DFA mindfa2 = dfa2.minimize();
DFA product(mindfa, mindfa2, UorD);
ifstream file;
file.open(tekstBestand);
if (!file.is_open()) return;
string line;
vector<string> posities;
int x = 0;
int y = 0;
while (getline(file, line)){
istringstream iss(line);
do {
string word;
iss >> word;
if (!word.empty()) {
if (product.productAutomaat().accepts(word)) {
string positie = "(" + to_string(x) + ", " + to_string(y) + ")";
posities.push_back(positie);
x += 1;
} else {
x += 1;
}
}
} while (iss);
y+= 1;
x = 0;
}
for (int i = 0; i < posities.size(); ++i) {
cout << posities[i];
if (i != posities.size() -1)
cout << ", ";
}
}
}
void PatternSearching::testSearch() {
/*
in deze functie zoek ik de pattern in de tekst dat de we krijgen en return ik de positie in een txt bestand.
dit gebruik ik bij de testen om de output file te vergelijken met de juiste output.
*/
REQUIRE(this->properlyInitialized(),"Kruispunt wasn't initialized when calling testSearch");
if (regex2.empty()){
RE re(regex1, 'e');
ENFA enfa = re.toENFA();
DFA dfa = enfa.toDFA();
DFA minDfa = dfa.minimize();
ifstream file;
file.open(tekstBestand);
if (!file.is_open()) return;
string line;
vector<string> posities;
int x = 0;
int y = 0;
while (getline(file, line)){
istringstream iss(line);
do {
string word;
iss >> word;
if (!word.empty()) {
if (minDfa.accepts(word)) {
string positie = "(" + to_string(x) + ", " + to_string(y) + ")";
posities.push_back(positie);
x += 1;
} else {
x += 1;
}
}
} while (iss);
y+= 1;
x = 0;
}
ofstream myfile;
myfile.open("output.txt");
for (int i = 0; i < posities.size(); ++i) {
myfile << posities[i];
if (i != posities.size() -1)
myfile << ", ";
}
myfile.close();
}
else{
RE re(regex1, 'e');
RE re2(regex2, 'e');
ENFA enfa = re.toENFA();
ENFA enfa2 = re2.toENFA();
DFA dfa = enfa.toDFA();
DFA dfa2 = enfa2.toDFA();
DFA mindfa = dfa.minimize();
DFA mindfa2 = dfa2.minimize();
DFA product(mindfa, mindfa2, UorD);
ifstream file;
file.open(tekstBestand);
if (!file.is_open()) return;
string line;
vector<string> posities;
int x = 0;
int y = 0;
while (getline(file, line)){
istringstream iss(line);
do {
string word;
iss >> word;
if (!word.empty()) {
if (product.productAutomaat().accepts(word)) {
string positie = "(" + to_string(x) + ", " + to_string(y) + ")";
posities.push_back(positie);
x += 1;
} else {
x += 1;
}
}
} while (iss);
y+= 1;
x = 0;
}
ofstream myfile;
myfile.open("output.txt");
for (int i = 0; i < posities.size(); ++i) {
myfile << posities[i];
if (i != posities.size() -1)
myfile << ", ";
}
myfile.close();
}
}