-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
228 lines (190 loc) · 6.53 KB
/
main.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
#include <string>
#include <fstream>
#include <iostream>
#include<vector>
#include<ctype.h>
#include<math.h>
#include <windows.h>
#include <mysql.h>
using namespace std;
const int AlPHABET_SIZE=26;
const int CASE='a';
int innerAB=0, innerAA=0, innerBB=0;
struct Node
{
Node* parent = NULL;
Node* children[26]= {};
int occurences=0;
};
void InsertNode(Node* trieTree, string word)
{
Node* currentNode= trieTree;
string::iterator it=word.begin();
while(it !=word.end() && ((*it>=97 && *it<=122)||(*it>=65 && *it<=90)))
{
*it=tolower(*it);
if(currentNode->children[*it - CASE]==NULL)
{
currentNode->children[*it - CASE]=new Node();
currentNode->children[*it - CASE]->parent= currentNode;
}
currentNode= currentNode->children[*it - CASE];
++it;
}
++(currentNode->occurences);
//cout<<"fuck";
}
int Search(Node* trieTree, vector<char> word)
{ int j;
vector<char>::iterator it=word.begin();
while(it != word.end())
{
if(trieTree->children[*it - CASE] != NULL)
{
trieTree=trieTree->children[*it - CASE];
++it;
}
else
return 0;
}
j=trieTree->occurences;
if(trieTree->occurences)
return j;
else
return 0;
}
void PreOrderPrint(Node* trieTree, Node* trieTree_2, vector<char> word)
{
if(trieTree->occurences && trieTree->parent)
{
for(vector<char>::iterator it= word.begin(); it != word.end(); ++it)
{
//cout<<*it;
}
//cout<<" "<<trieTree->occurences<<endl;
int TT2occ;
TT2occ=Search(trieTree_2, word);
innerAB=innerAB+(trieTree->occurences*TT2occ);
innerAA=innerAA+(trieTree->occurences*trieTree->occurences);
}
for(int i=0; i<AlPHABET_SIZE; i++)
{
if(trieTree->children[i] != NULL)
{
word.push_back(CASE + i);
PreOrderPrint(trieTree->children[i], trieTree_2, word);
word.pop_back();
}
}
}
void finish_with_error(MYSQL *con)
{
cout<<mysql_error(con)<<endl;
mysql_close(con);
exit(1);
}
int main()
{
MYSQL *con = mysql_init(NULL);
int k=0;
if (con == NULL)
{
cout<<"mysql_init() failed\n";
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "",
"research_papers", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
if (mysql_query(con, "SELECT * FROM tbl_uploads"))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL)
{
finish_with_error(con);
}
int num_fields = mysql_num_fields(result);
//cout<<num_fields;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
if(row[i])
{
if(i==1)
cout<<row[i]<<" ";
if(i==4)
{
vector<char> word;
char* vec;
vec=row[i];
//cout<<vec;
//vec.push_back(*row[i]);
//cout<<*chava;
Node* trieTree= new Node();
Node* trieTree_2= new Node();
const int SIZE = 1000;
string words[SIZE];
string str;
int j;
//cout <<endl<< "Start reading from a file 1-" <<endl;
ifstream fin("C:\\Users\\HP\\Documents\\faint.txt");
for (int l = 0; (fin >> str) && (l < SIZE); ++l) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
//cout << "This string is " <<l<<" "<<str<<endl;
words[l] = str;
j=l;
} // We now also need to stop before we hit the no define array space.
fin.close();
cout << endl;
for (int m = 0; m <= j; ++m)
{
InsertNode(trieTree,words[m]);
}
//cout << "Start reading from a file 2-" <<endl;
ifstream fin_2(row[i]);
for (int n = 0; (fin_2 >> str) && (n < SIZE); ++n)
{
//cout << "This string is " <<n<<" "<<str<<endl;
words[n] = str;
j=n;
}
fin_2.close();
cout << endl;
for (int p = 0; p <= j; ++p)
{
InsertNode(trieTree_2,words[p]);
}
PreOrderPrint(trieTree, trieTree_2, word);
//cout<<"Inner Product of document 1 with document 1 is-"<<innerAA<<"\n";
//cout<<"Inner Product of document 1 with document 2 is-"<<innerAB<<"\n";
innerBB=innerAA;
innerAA=0;
innerAB=0;
PreOrderPrint(trieTree_2, trieTree, word);
//cout<<"Inner Product of document 2 with document 2 is-"<<innerAA<<"\n";
//cout<<"Inner Product of document 1 with document 2 is-"<<innerAB<<"\n";
cout<<"Document Similarity measure is- "<<acos(innerAB/sqrt(innerAA*innerBB))<<"\n";
cout<<"\nDocument 1 and 2 are "<<100-(acos(innerAB/sqrt(innerAA*innerBB))*200)/3.141592654<<" % similar\n";
k++;
innerAB=0, innerAA=0, innerBB=0;
cout<<"---------------------------------------------------------------------------------------------";
}
}
else
cout<<"NULL";
}
cout<<"\n";
}
mysql_free_result(result);
mysql_close(con);
/*if (mysql_query(con, "INSERT INTO author_info VALUES(3,'Conan','Germany')"))
{
finish_with_error(con);
}*/
return 0;
}