-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal version.cpp
438 lines (285 loc) · 12 KB
/
Final version.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
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
#include < iostream >
#include < fstream >
#include < vector >
#include < string >
#include < algorithm >
#include < map >
#include < stdio.h >
#include < ctype.h >
#include < cstring >
using namespace std;
std::map < string, int > mymap;
std::map < string, int > myresult;
bool inference_engine(string temp, string kb[10000], int kbn) {
if (temp.find_first_not_of(' ') == temp.npos)
return true;
kbn++;
kb[kbn] = temp;
static int tempcount = 0;
tempcount++;
std::pair < std::map < string, int > ::iterator, bool > ret;
ret = mymap.insert(std::pair < string, int > (temp, 1));
if (ret.second == false)
return false;
if (kbn > 20000)
return false;
char * temp_ptr = new char[temp.length() + 1];
std::strcpy(temp_ptr, temp.c_str());
char * ptr_to_temp = std::strtok(temp_ptr, " |");
string temp_tokens[50];
int k = 0;
while (ptr_to_temp != 0) {
temp_tokens[k] = ptr_to_temp;
k++;
ptr_to_temp = std::strtok(NULL, " |");
}
//till here the temp query has been tokenised and stored in temp_tokens
k--;
for (int l = 0; l <= k; l++) {
bool sol = true;
// here the alphabet of each of the temp_tokens is searched in kb
string copy_temp;
copy_temp = temp_tokens[l];
string cp, c;
std::size_t p = temp.find("(");
copy_temp = copy_temp.substr(0, p);
if (copy_temp[0] == '~') {
c.append(" ");
copy_temp.erase(copy_temp.begin());
c.append(copy_temp);
} else {
c.append("~");
c.append(copy_temp);
}
for (int i = 1; i <= kbn; i++) {
sol = true;
for (string::size_type t = 0;
(t = kb[i].find(c, t)) != string::npos;) //here the literal from the kb line is found matching with some literal in query
{
sol = true;
unsigned brackets_temp_open = temp_tokens[l].find('('); //extracting the contents of temp (i.e, one literal in query ) within the brackets
unsigned brackets_temp_close = temp_tokens[l].find(')');
string contents_inside_brackets = temp_tokens[l].substr(brackets_temp_open + 1, brackets_temp_close - brackets_temp_open - 1);
char * cstr_inside_contents = new char[contents_inside_brackets.length() + 1];
std::strcpy(cstr_inside_contents, contents_inside_brackets.c_str());
char * p_to_contents_inside_brackets = std::strtok(cstr_inside_contents, " ,");
string contains_parameter_variables[10];
int n = 0;
while (p_to_contents_inside_brackets != 0)
{
contains_parameter_variables[n] = p_to_contents_inside_brackets;
n++;
p_to_contents_inside_brackets = std::strtok(NULL, " ,");
}
//this kb has the matching term
//now tokenising this kb
std::string str3 = kb[i].substr(t);
char * cstrr = new char[str3.length() + 1];
std::strcpy(cstrr, str3.c_str());
char * kb_tokenr = std::strtok(cstrr, " |");
string temp_kb_token;
while (kb_tokenr != 0) {
temp_kb_token = kb_tokenr;
if (temp_kb_token[0] != ' ')
temp_kb_token.insert(0, " ");
std::size_t posf = temp_kb_token.find(c);
if (posf != std::string::npos)
break;
kb_tokenr = std::strtok(NULL, "|");
}
unsigned brackets_kb_open = temp_kb_token.find('(');
unsigned brackets_kb_close = temp_kb_token.find(')');
string contents_inside_brackets_kb = temp_kb_token.substr(brackets_kb_open + 1, brackets_kb_close - brackets_kb_open - 1);
char * cstr_inside_contents_kb = new char[contents_inside_brackets_kb.length() + 1];
std::strcpy(cstr_inside_contents_kb, contents_inside_brackets_kb.c_str());
char * p_to_contents_inside_brackets_kb = std::strtok(cstr_inside_contents_kb, " ,");
string contains_parameter_variables_kb[10];
int nkb = 0;
while (p_to_contents_inside_brackets_kb != 0) {
contains_parameter_variables_kb[nkb] = p_to_contents_inside_brackets_kb;
nkb++;
p_to_contents_inside_brackets_kb = std::strtok(NULL, " ,");
}
nkb--;
string unification_temp_kb;
unification_temp_kb.clear();
unification_temp_kb.append(temp);
unification_temp_kb.erase(unification_temp_kb.end() - 1);
unification_temp_kb.append(kb[i]);
int flag = 0;
for (int m = 0; m <= nkb; m++)
if (isupper(contains_parameter_variables_kb[m][0]) and isupper(contains_parameter_variables[m][0]) and contains_parameter_variables_kb[m].compare(contains_parameter_variables[m]) == 0)
flag++;
if (flag == nkb + 1) {
sol = true;
c = temp_kb_token;
}
for (int m = 0; m <= nkb; m++) {
if (islower(contains_parameter_variables_kb[m][0]) and isupper(contains_parameter_variables[m][0]))
{
for (string::size_type o = 0;
(o = unification_temp_kb.find(contains_parameter_variables_kb[m], o)) != string::npos;) {
if ((unification_temp_kb[o + 1] == ')'
or unification_temp_kb[o + 1] == ',') and(unification_temp_kb[o - 1] == '('
or unification_temp_kb[o - 1] == ',')) {
unification_temp_kb.replace(o, contains_parameter_variables_kb[m].length(), contains_parameter_variables[m]);
o += contains_parameter_variables[m].length();
int yes = 0;
for (int r = 0; r <= nkb; r++) {
if (contains_parameter_variables[r] == contains_parameter_variables_kb[m]) {
contains_parameter_variables[r] = contains_parameter_variables[m];
yes = 1;
}
}
if (yes == 1) {
m = -1;
break;
}
} else o++;
}
} else if (isupper(contains_parameter_variables_kb[m][0]) and islower(contains_parameter_variables[m][0])) //find and replace all variable occurances with paramater
{
// temp.erase (temp_tokens[l]); //cancel out literals
for (string::size_type o = 0;
(o = unification_temp_kb.find(contains_parameter_variables[m], o)) != string::npos;) {
if ((unification_temp_kb[o + 1] == ')'
or unification_temp_kb[o + 1] == ',') and(unification_temp_kb[o - 1] == '('
or unification_temp_kb[o - 1] == ',')) {
unification_temp_kb.replace(o, contains_parameter_variables[m].length(), contains_parameter_variables_kb[m]);
o += contains_parameter_variables_kb[m].length();
int yes = 0;
for (int r = 0; r <= nkb; r++) {
if (contains_parameter_variables_kb[r] == contains_parameter_variables[m]) {
contains_parameter_variables_kb[r] = contains_parameter_variables_kb[m];
yes = 1;
}
}
if (yes == 1) {
m = -1;
break;
}
} else o++;
}
} else if (isupper(contains_parameter_variables_kb[m][0]) and isupper(contains_parameter_variables[m][0]) and contains_parameter_variables_kb[m].compare(contains_parameter_variables[m]) != 0) {
sol = false;
break;
}
}
for (int m = 0; m <= nkb; m++) {
if (islower(contains_parameter_variables_kb[m][0]) and islower(contains_parameter_variables[m][0]) and contains_parameter_variables_kb[m].compare(contains_parameter_variables[m]) != 0) {
for (string::size_type o = 0;
(o = unification_temp_kb.find(contains_parameter_variables_kb[m], o)) != string::npos;) {
if ((unification_temp_kb[o + 1] == ')'
or unification_temp_kb[o + 1] == ',') and(unification_temp_kb[o - 1] == '('
or unification_temp_kb[o - 1] == ',')) {
unification_temp_kb.replace(o, contains_parameter_variables_kb[m].length(), contains_parameter_variables[m]);
o += contains_parameter_variables[m].length();
} else o++;
}
}
}
if (sol) {
std::size_t pos_kb_term = unification_temp_kb.find("|", unification_temp_kb.find(c) + 1, 1);
unification_temp_kb.erase(unification_temp_kb.find(c), (pos_kb_term - unification_temp_kb.find(c) + 1));
string cc = c;
if (cc[1] == '~')
cc.erase(cc.begin());
if (cc[0] == '~')
cc[0] = ' ';
else
cc[0] = '~';
pos_kb_term = unification_temp_kb.find("|", unification_temp_kb.find(cc) + 1, 1);
unification_temp_kb.erase(unification_temp_kb.find(cc), (pos_kb_term - unification_temp_kb.find(cc) + 2));
for (string::size_type o = 0;
(o = unification_temp_kb.find("|", o)) != string::npos;) {
if (unification_temp_kb[o - 1] != ' ')
unification_temp_kb.insert(o - 1, " ");
if (unification_temp_kb[o + 1] != ' ')
unification_temp_kb.insert(o + 1, " ");
if (unification_temp_kb[o + 1] == ' '
and unification_temp_kb[o + 2] == ' ')
unification_temp_kb.erase(o + 2, 1);
if (unification_temp_kb[o - 1] == ' '
and unification_temp_kb[o - 2] == ' ')
unification_temp_kb.erase(o - 2, 1);
o++;
}
if (unification_temp_kb[0] != ' ')
unification_temp_kb.insert(0, " ");
string copy_unification_temp_kb = unification_temp_kb;
myresult.clear();
char * cstr_result = new char[copy_unification_temp_kb.length() + 1];
std::strcpy(cstr_result, copy_unification_temp_kb.c_str());
char * p_to_result = std::strtok(cstr_result, " (");
string result_tok;
std::size_t pos_of_or = 0;
while (p_to_result != 0)
{
result_tok = p_to_result;
std::pair < std::map < string, int > ::iterator, bool > res;
res = myresult.insert(std::pair < string, int > (result_tok, 1));
if (res.second == false)
return false;
pos_of_or = copy_unification_temp_kb.find("|", pos_of_or + 1, 1);
char * p_cont = & copy_unification_temp_kb[pos_of_or + 1];
p_to_result = std::strtok(p_cont, " (");
}
if (inference_engine(unification_temp_kb, kb, kbn))
return true;
}
t++;
}
}
}
//control came here means there is no match found so terminate search
return false;
}
int main() {
int q, kbn;
string query[100];
string kb[1000];
char t[50];
ifstream myReadFile;
ofstream myWriteFile;
myReadFile.open("input.txt");
myWriteFile.open("output.txt");
myReadFile >> q;
for (int i = 0; i < q; i++)
{
myReadFile >> query[i];
cout << "\n";
}
myReadFile >> kbn;
for (int i = 0; i <= kbn; i++)
{
getline(myReadFile, kb[i], '\n');
kb[i].append(" | ");
cout << "\n";
}
for (int j = 0; j <= kbn; j++)
kb[j].insert(0, " ");
for (int i = 0; i < q; i++)
{
bool ans;
string temp;
string tempst;
tempst = query[i];
temp.insert(0, " ");
if (tempst[0] != '~') {
temp.append("~");
temp.append(tempst);
} else {
temp.append(tempst);
temp.erase(temp.begin() + 1);
}
temp.append(" | ");
ans = inference_engine(temp, kb, kbn);
if (ans)
myWriteFile << "TRUE\n";
else
myWriteFile << "FALSE\n";
}
myReadFile.close();
myWriteFile.close();
return 0;
}