Repository navigation
Expand file tree
/
Copy pathutility.cpp
More file actions
376 lines (335 loc) · 12.6 KB
/
Copy pathutility.cpp
File metadata and controls
376 lines (335 loc) · 12.6 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
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
#include "utility.hpp"
unsigned long long hash_value_calculator(std::string page_title, std::string id) {
//generating hashvalue for entry
unsigned long long hashvalue = 5381;
int c;
const char* site_c = page_title.c_str();
const char* id_c = id.c_str();
while ( (c = *site_c++) )
hashvalue = ((hashvalue << 5) + hashvalue) + c; /* hash * 33 + c */
while ( (c = *id_c++) )
hashvalue = ((hashvalue << 5) + hashvalue) + c; /* hash * 33 + c */
return hashvalue;
}
// fills up bow anf tf_idf tables
void create_bow_and_tf(int** bow, float** tf_idf, Clique* list_of_entries, Dict* dictionary) {
Cliquenode* temp = list_of_entries->head;
Entry* e;
std::string word;
// int bow[list_of_entries->get_size()][dictionary_size];
// int tf_idf[list_of_entries->get_size()][dictionary_size];
int word_loc;
int entry_counter = 0;
int word_counter = 0; // keeps total number of description's words
// for each entry
while(temp != NULL) {
e = temp->data;
e->loc = entry_counter; // save its row number of arrays
std::istringstream iss(e->specs_words);
// for each word in its description
while(iss) {
word_counter++;
iss >> word;
word_loc = dictionary->find_loc(dictionary->root, word);
if( word_loc != -1 ) { // if word exists in dictionary
bow[entry_counter][word_loc] = bow[entry_counter][word_loc] + 1; // increase its value in bow table
}
}
// after finishing bow table
// run through it and create tf values
for(int i=0 ; i < DICTIONARY_SIZE ; i++) {
tf_idf[entry_counter][i] = (float)bow[entry_counter][i]/(float)word_counter;
// if(tf_idf[entry_counter][i] != 0) {
// std::cout << tf_idf[entry_counter][i] << "\n";
// }
}
temp = temp->next;
entry_counter++;
}
}
// -----------------SORT-------------------------
void merge(float* tfidf, int* words, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
float L[n1], R[n2];
int Ls[n1], Rs[n2];
for (int i = 0; i < n1; i++) {
L[i] = tfidf[l + i];
Ls[i] = words[l+i];
}
for (int j = 0; j < n2; j++) {
R[j] = tfidf[m + 1 + j];
Rs[j] = words[m + 1 + j];
}
int i = 0;
int j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
tfidf[k] = L[i];
words[k] = Ls[i];
i++;
}
else {
tfidf[k] = R[j];
words[k] = Rs[j];
j++;
}
k++;
}
while (i < n1) {
tfidf[k] = L[i];
words[k] = Ls[i];
i++;
k++;
}
while (j < n2) {
tfidf[k] = R[j];
words[k] = Rs[j];
j++;
k++;
}
}
void mergeSort(float* tfidf, int* words, int l, int r) {
if(l>=r) {
return;//returns recursively
}
int m = (l+r-1)/2;
mergeSort(tfidf, words, l, m);
mergeSort(tfidf, words, m+1, r);
merge(tfidf, words, l, m, r);
}
void prediction_merge(DoubleLinkedNode** pairs, int l, int m, int r){
unsigned int n1 = m - l + 1;
unsigned int n2 = r - m;
DoubleLinkedNode **L = new DoubleLinkedNode*[n1];
DoubleLinkedNode **R = new DoubleLinkedNode*[n2];
for (unsigned int i = 0; i < n1; i++) {
L[i] = pairs[l + i];
}
for (unsigned int j = 0; j < n2; j++) {
R[j] = pairs[m + 1 + j];
}
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = l;
while (i < n1 && j < n2) {
if (L[i]->pred <= R[j]->pred) {
pairs[k] = L[i];
i++;
}
else if(L[i]->pred > R[j]->pred) {
pairs[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
pairs[k] = L[i];
i++;
k++;
}
while (j < n2) {
pairs[k] = R[j];
j++;
k++;
}
delete[] L;
delete[] R;
}
void prediction_mergeSort(DoubleLinkedNode** pairs, int l, int r){
if(l>=r) {
return;//returns recursively
}
int m = (l+r-1)/2;
prediction_mergeSort(pairs, l, m);
prediction_mergeSort(pairs , m+1, r);
prediction_merge(pairs, l, m, r);
}
// -----------------SHUFFLE-------------------------
void swap(std::string* a, std::string* b) {
std::string temp;
temp = *a;
*a = *b;
*b = temp;
}
void shuffle(std::string* array, unsigned int size) {
srand(19);
// int s1,s2;
for(unsigned int i=0 ; i < size ; ++i) {
// s1 = rand()%size;
// s2 = rand()%size;
swap(array[i], array[rand()%size]);
}
}
// -----------------PRINTING-------------------------
void print_output(Clique* list_of_entries, unsigned int *output_lines_counter) {
*output_lines_counter = 0;
std::ofstream output;
output.open("output.csv");
output << "left_spec_id,right_spec_id,label\n";
Cliquenode* c_n = list_of_entries->head;
Clique* c = NULL;
AntiClique* d = NULL;
while( c_n != NULL ) { //parse list of entries
c = c_n->data->clique; //get entry's clique
int size = c->size;
// if( c->size == 1) { //if not similar to anyone keep it for validation
// single_entries.push(c_n->data);
// }
if( c->printed == false ) { //if not NULL
// NOTE: replace till "c->printed = true;" with print_clique_Job add to queue
Cliquenode* table[size]; //create table of clique's members
Cliquenode* temp_entry = c->head;
for(int i=0 ; i < size ; i++) {
table[i] = temp_entry;
temp_entry = temp_entry->next;
}
// print every possible pair from that clique
for(int i=0 ; i<size ; i++) {
std::string url1 = table[i]->data->get_page_title() + "//" + table[i]->data->get_id();
for(int j=i+1 ; j<size ; j++) {
std::string url2 = table[j]->data->get_page_title() + "//" + table[j]->data->get_id();
output << url1 << "," << url2 << ",1" << "\n";
++(*output_lines_counter);
}
// table[i]->data->clique = NULL; //make clique pointer NULL for all those entries so we don't print any pair more than once
}
c->printed = true;
// print every possible negative similarity pair of entries
d = c->different; // list of different cliques
if ( !d->is_empty() ) {
// std::cout << d->size << std::endl;
int d_size = d->size;
if(c->anti_printed == NULL){
c->anti_printed = new Clique*[d_size];
for(int i=0 ; i < d_size ; i++) c->anti_printed[i] = NULL;
}
// adding all different cliques to d_table
AntiCliquenode* d_table[d_size];
AntiCliquenode* temp_d = d->head;
for(int i=0 ; i<d_size ; i++) {
d_table[i] = temp_d;
temp_d = temp_d->next;
}
for(int i=0 ; i<d_size ; i++) { // for every different clique
Clique* d_c = d_table[i]->data;
if(d_c->anti_printed == NULL){
d_c->anti_printed = new Clique*[d_c->different->size];
for(int i=0 ; i < d_c->different->size ; i++) d_c->anti_printed[i] = NULL;
}
bool continue_flag = false;
for(int j = 0 ; j < d_size ; j++){
if(c->anti_printed[j] == d_c){
continue_flag = true;
break;
}
}
if(continue_flag == true) {
continue;
} else {
for(int j = 0 ; j < d_size ; j++){
if(c->anti_printed[j] == NULL){
c->anti_printed[j] = d_c;
break;
}
}
for(int j = 0 ; j < d_c->different->size ; j++) {
if(d_c->anti_printed[j] == NULL){
d_c->anti_printed[j] = c;
break;
}
}
}
/*pthread_mutex_t* mutex;
pthread_mutex_init(mutex, NULL);
print_anticlique_Job paj(c, d_c, &output, &output_lines_counter, mutex);
paj.run();*/
// NOTE: to replace with add to job scheduler (args as above)
for(int j=0 ; j<size ; j++) { // for every entry in c clique
Cliquenode* d_e = d_c->head;
std::string url1 = table[j]->data->get_page_title() + "//" + table[j]->data->get_id();
while( d_e != NULL ) { // for every entry in different clique
std::string url2 = d_e->data->get_page_title() + "//" + d_e->data->get_id();
output << url1 << "," << url2 << ",0" << "\n";
++(*output_lines_counter);
d_e = d_e->next;
}
}
}
}
}
c_n = c_n->next;
}
// NOTE: run printing tasks
output.close();
}
void clear_print(Clique* list_of_entries) {
Cliquenode* temp = list_of_entries->head;
while(temp != NULL) {
temp->data->clique->printed = false;
if(temp->data->clique->anti_printed) {
delete[] temp->data->clique->anti_printed;
temp->data->clique->anti_printed = NULL;
}
temp = temp->next;
}
}
void create_train_set(std::string* train_set, const unsigned int output_lines_counter) {
std::ifstream input;
input.open("output.csv");
if ( input.is_open() ) {
std::string empty;
getline(input, empty); // discard header line
// fill up array with info lines
for(unsigned int i=0 ; i < output_lines_counter ; ++i) {
// NOTE: add read_output_Job to queue
getline(input, train_set[i]);
// pthread_mutex_t* mutex;
// pthread_mutex_init(mutex, NULL);
// read_output_Job roj(train_set, &input, i, mutex);
// roj.run();
}
// NOTE: run reading output jobs
} else {
perror("no output file");
}
input.close();
}
void print_cliques(Clique* list_of_entries, unsigned int *output_lines_counter) {
*output_lines_counter = 0;
std::ofstream output;
output.open("cliques.csv");
output << "left_spec_id,right_spec_id,label\n";
Cliquenode* c_n = list_of_entries->head;
Clique* c = NULL;
while( c_n != NULL ) { //parse list of entries
c = c_n->data->clique; //get entry's clique
int size = c->size;
// if( c->size == 1) { //if not similar to anyone keep it for validation
// single_entries.push(c_n->data);
// }
if( c->printed == false ) { //if not NULL
// NOTE: replace till "c->printed = true;" with print_clique_Job add to queue
Cliquenode* table[size]; //create table of clique's members
Cliquenode* temp_entry = c->head;
for(int i=0 ; i < size ; i++) {
table[i] = temp_entry;
temp_entry = temp_entry->next;
}
// print every possible pair from that clique
for(int i=0 ; i<size ; i++) {
std::string url1 = table[i]->data->get_page_title() + "//" + table[i]->data->get_id();
for(int j=i+1 ; j<size ; j++) {
std::string url2 = table[j]->data->get_page_title() + "//" + table[j]->data->get_id();
output << url1 << "," << url2 << ",1" << "\n";
++(*output_lines_counter);
}
// table[i]->data->clique = NULL; //make clique pointer NULL for all those entries so we don't print any pair more than once
}
c->printed = true;
}
c_n = c_n->next;
}
output.close();
}