-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21002169_Algorithm.cpp
More file actions
287 lines (221 loc) · 7.47 KB
/
Copy path21002169_Algorithm.cpp
File metadata and controls
287 lines (221 loc) · 7.47 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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
string input_Pattern = "pattern5.txt" ;
string input_Text = "text5.txt";
string output_file = "patternmatch5.txt" ;
//prefixtable
vector<int> computeLPS(const string& pattern) {
int m = pattern.size();
vector<int> lps(m, 0); //store the length of the longest proper prefix which is also a suffix for every prefix of the pattern
int j = 0;
for (int i = 1; i < m;) {
if (pattern[i] == pattern[j]) { //if a match occures
j++;
lps[i] = j;
i++;
} else {
if (j != 0) {
j = lps[j - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
//normal kmp
vector<int> kmp(const string& text, const string& pattern) {
int n = text.size();
int m = pattern.size();
vector<int> lps = computeLPS(pattern);
int i = 0; // index for text
int j = 0; // index for pattern
vector<int> occurrences; // to store starting indices of found patterns
while (i < n) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (j == m) {
occurrences.push_back(i - j); // pattern found, record the starting index
j = lps[j - 1]; // reset j to continue the search
} else if (i < n && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return occurrences;
}
// kmp for dot symbol
vector<int> dotkmp(const string& text, const string& pattern) {
int n = text.size();
int m = pattern.size();
vector<int> lps = computeLPS(pattern);
int i = 0;
int j = 0;
vector<int> occurrences;
while (i < n) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (pattern[j] == '.'){ //skips the dot
i++;
j++;
}
if (j == m) {
occurrences.push_back(i - j);
j = lps[j - 1];
} else if (i < n && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return occurrences;
}
//kmp for range [ ]
vector<int> rangekmp(const string& text, const string& pattern){
string test = pattern ;
int start = test.find('['); //find index of [
int end = test.find(']'); //find index of ]
string listing = test.substr(start+1 , end-start-1); //save the substring ie [substring] into the string listing
//cout << listing ;
vector<int> occurrences; //array to store indexs where kmp finds a match
for (int i = 0; i < listing.size(); ++i) { //iterates over all the charcters in the listing
string character(1,listing[i]);
string temp = test;
temp.replace(start, end-start+1 , character); //change example_[ABC]_example to example_A_example , example_B_example etc ..
vector<int> kmpresult = kmp(text,temp); //run kmp normally on changed string
occurrences.insert(occurrences.end(),kmpresult.begin(),kmpresult.end()); //append all indexed to occurrences
}//this loop will run from A1 to An , where text[A1...An]text <--- pattern
return occurrences ;
}
// kmp for strings at the end $
vector<int> endswithkmp(const string& text, const string& pattern){
string ptemp = pattern ;
string ttemp = text ;
ttemp.push_back('$'); //appends $ to end of pattern
ptemp.push_back('$') ; // appends $ to end of text(line of the text passed into the function) to make that match uniqe
return kmp(ttemp , ptemp); //now runs kmp normally
}
// kmp for strings at the start ^
vector<int> startswithkmp(const string& text, const string& pattern){
string ptemp = pattern ;
string ttemp = text ;
ttemp.insert(ttemp.begin(), '^'); //same as $ but done to front to make front uniqe
ptemp.insert(ptemp.begin() , '^');
//cout << temp << endl ;
return kmp(ttemp , ptemp);
}
// return the pattern in file format as a string
string file2pattern(string filename){ //opens the file which has the pattern and returns it as a string
ifstream file(filename);
if (!file.is_open()) {
return "error";
}
string line;
string content;
while (getline(file, line)) {
content += line;
}
file.close();
return content ;
}
// what is the symbol type used in the pattern
int findsymboltype(string pattern){
int dot=0 , star=0 , range=0 , endswith=0 , startswith=0;
if (pattern.find('.') != std::string::npos)
dot = 1;
if (pattern.find('[') != std::string::npos)
range = 1;
if (pattern.find('$') != std::string::npos)
endswith = 1;
if (pattern.find('^') != std::string::npos)
startswith = 1;
if(dot+star+range+endswith+startswith == 0)
return 0 ; //0 for no special pattern matching
else if(dot+star+range+endswith+startswith > 1)
return -1 ; //if more that one special symbol is used return -1
else if(dot == 1)
return 1 ; //.
else if(range == 1)
return 2 ; //[]
else if(endswith == 1)
return 3 ; //$
else if(startswith == 1)
return 4 ; //^
else
return 5 ;
}
// main func
int main() {
string pattern_fromfile = file2pattern(input_Pattern); //turns the pattern from the file into string form
int symboltype = findsymboltype(pattern_fromfile); // what symbol is in the pattern?
ifstream text_file(input_Text); //opens the text file
ofstream out_file(output_file);
if (!text_file.is_open()) {
cout << "Unable to open file.\n";
return 1;
}
string line;
int linenumber = 0 ;
while (getline(text_file, line)) {//reads the file line by line and matches any instances in said line
string pattern = pattern_fromfile;
linenumber++;
vector<int> indices;
if(symboltype == 0){
//no regx
indices = kmp(line , pattern); //normal pattern matching no special symbol used
}
else if(symboltype == 1){
// .
indices = dotkmp(line , pattern);
}
else if(symboltype == 2){
// []
indices = rangekmp(line , pattern);
}
else if(symboltype == 3){
// $
int s_index= pattern.find('$');
pattern.erase(s_index,1); //removes the $ symbol from the pattern
indices = endswithkmp(line,pattern);
}
else if(symboltype == 4){
// ^
int s_index= pattern.find('^');
pattern.erase(s_index,1); //removes the ^ symbol from the pattern
indices = startswithkmp(line,pattern);
}
else {
cout << "error";
}
if (!indices.empty()) {
out_file << "Pattern found at Line number :" << linenumber << " Indices: ";
cout << "Pattern found at Line number :" << linenumber << " Indices: ";
for (int index : indices) {
out_file << index << " ";
cout << index << " "; //prints the locations where matches were found
}
out_file << endl;
cout << endl;
}
else {
//cout << "empty" ;
}
}
text_file.close();
out_file.close();
return 0;
}