-
Notifications
You must be signed in to change notification settings - Fork 0
/
recom_steps.c
369 lines (292 loc) · 13.6 KB
/
recom_steps.c
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
//
// recom_steps.c
//
// Christina-Theano Kylafi
// AM: 1115201200077
#include "recom_steps.h"
int recom_steps(struct tweets* tweets[], long long tweets_num, struct sentim_score_map* sentim_map[], long long sentim_map_size, struct crypto_coins* coins[], long long coins_size, struct users* users[], long long users_num, char* outputfile, int p_nearest)
{
int dimension_of_coords = coins_size;
//sentiment score
calculate_sentim_score(tweets, tweets_num, sentim_map, sentim_map_size);
//print_tweets(tweets, tweets_num, coins);
//create users
create_users(users, users_num, tweets, tweets_num, coins, coins_size );
//print_users(users, users_num,coins_size, coins);
//normalise_vector_user_crypto(users, users_num, coins_size);
calculate_average_rating(users, users_num, coins_size);
user_crypto_save_users(users, users_num, dimension_of_coords);
//print_users(users, users_num,coins_size, coins);
//print_tweets(tweets, tweets_num, coins);
//clear users with no proposals
long long users_with_coins_num = 0;
users_with_coins_num = count_users_with_coins(users, users_num, coins_size);
printf("\nUsers with coins: %lld\n", users_with_coins_num);
struct users* users_with_coins[1];
if(users_with_coins_num!=users_num) //if we have users with no coins
{
users_with_coins[0] = malloc( users_with_coins_num * get_size_users());
users_init(users_with_coins, users_with_coins_num, coins_size);
clear_users_with_no_coins(users, users_num, users_with_coins, users_with_coins_num, coins_size);
users[0] = delete_users(users, users_num, coins_size);
users[0] = users_with_coins[0];
//delete_users(users_with_coins, users_with_coins_num, coins_size);
users_num = users_with_coins_num;
}
user_crypto_save_users(users, users_num, dimension_of_coords);
//clear_best_arrays(users, users_num);
//print_users(users, users_with_coins_num, coins_size, coins);
//create_representative_users
if(print_msgs_recom_on == 1)
printf("\nCreating Virtual Users by clustering the tweets (from file) ...\n");
int clusters_num = 0;
struct cluster* clusters[1];
clusters_num = clusters_for_representatives;
clusters[0] = malloc( clusters_num * get_size_cluster());
clusters_init(clusters_num, clusters);
char tweetsfile[40] = "./twitter_dataset_small_v2.csv";
char configfile[40] = "./cluster.conf";
char outfile[40] = "./clusterintemp.txt";
clustering(clusters, tweetsfile, configfile, outfile, 2, 2, 1, 1, d );
printf("\nTotal Virtual Users: %d\n", clusters_num);
//printClusters(clusters, clusters_num);
struct users* users_representatives[1];
users_representatives[0] = malloc( clusters_num * get_size_users());
users_init(users_representatives, clusters_num, coins_size);
create_represent(clusters, clusters_num, users_representatives, tweets, tweets_num, coins, coins_size);
//delete cluster_structure
delete_clusters(clusters_num, clusters);
calculate_average_rating(users_representatives, clusters_num, coins_size);
user_crypto_save_users(users_representatives, clusters_num, dimension_of_coords);
//print_users(users_representatives, clusters_num, coins_size, coins);
//normalise_vector_user_crypto(users_representatives, clusters_num, coins_size);
//print_users(users_representatives, clusters_num, coins_size, coins);
//clear representative users with no proposals
long long users_representatives_with_coins_num = 0, users_representatives_num = clusters_num;
users_representatives_with_coins_num = count_users_with_coins(users_representatives, clusters_num, coins_size);
printf("\nVirtual Users with coins: %lld\n", users_representatives_with_coins_num);
struct users* users_representatives_with_coins[1];
if(users_representatives_with_coins_num!= users_representatives_num) //if we have users with no coins
{
users_representatives_with_coins[0] = malloc( users_representatives_with_coins_num * get_size_users());
users_init(users_representatives_with_coins, users_representatives_with_coins_num, coins_size);
clear_users_with_no_coins(users_representatives, clusters_num, users_representatives_with_coins, users_representatives_with_coins_num, coins_size);
users_representatives[0] = delete_users(users_representatives, clusters_num, coins_size);
users_representatives[0] = users_representatives_with_coins[0];
users_representatives_num = users_representatives_with_coins_num;
}
//print_users(users_representatives, users_representatives_num, coins_size, coins);
struct timespec recom_start, recom_end;
double recom_time = 0.0;
print_line();
print_msg_centre("1.1 Cosine LSH Recommendation (real users)");
print_line();
//Reccomendation_1.1 --------------------------------------------------------------------------------------------------------------------------------------
clock_gettime(CLOCK_MONOTONIC_RAW, &recom_start);
cosine_LSH_recom_1_1( users, users_num, outputfile, dimension_of_coords, p_nearest, coins, coins_size);
// //print_users(users, users_num, coins_size, coins);
retrieve_saved_user_crypto_ratings_users(users, users_num, coins_size);
//printf("\nSteps %d\n", __LINE__);
print_line();
print_msg_centre("1.2 Cosine_LSH_Recommendation (virtual users)");
print_line();
//Recommendation_1.2 --------------------------------------------------------------------------------------------------------------------------------------
cosine_LSH_recom_1_2(users_representatives, users_representatives_num, users, users_num, outputfile, dimension_of_coords, p_nearest, coins, coins_size);
clock_gettime(CLOCK_MONOTONIC_RAW, &recom_end);
recom_time = (double)((recom_end.tv_sec - recom_start.tv_sec) * 1000000 + (recom_end.tv_nsec - recom_start.tv_nsec) / 1000);
recom_time = (double)recom_time/1000;
print_all_results(users, users_num, recom_time, 1);
// //print_users(users, users_num, coins_size, coins);
write_to_outputfile_recom(outputfile, users, users_num, recom_time, 1);
//printf("\nSteps %d\n", __LINE__);
//clear the arrays where i save the best coins for recommendation
clear_users(users, users_num);
//printf("\nSteps %d\n", __LINE__);
retrieve_saved_user_crypto_ratings_users(users, users_num, coins_size);
//print_users(users, users_num, coins_size, coins);
//printf("\nSteps %d\n", __LINE__);
print_line();
print_msg_centre("2.1 Clustering_Recommendation (real users)");
print_line();
//Recommendation_2.1 --------------------------------------------------------------------------------------------------------------------------------------
clock_gettime(CLOCK_MONOTONIC_RAW, &recom_start);
//print_users(users, users_num, coins_size, coins);
clustering_recom_2_1(users, users_num, outputfile, dimension_of_coords, p_nearest, coins, coins_size, tweets_num);
//copy saved crypto-user ratings to create new vector at the next recommendation
//print_users(users, users_num, coins_size, coins);
retrieve_saved_user_crypto_ratings_users(users, users_num, coins_size);
//print_users(users, users_num, coins_size, coins);
//printf("\nSteps %d\n", __LINE__);
print_line();
print_msg_centre("2.2 Clustering_Recommendation (virtual users)");
print_line();
//Recommendation_2.2 --------------------------------------------------------------------------------------------------------------------------------------
//print_users(users, users_num, coins_size, coins);
clustering_recom_2_2(users_representatives, users_representatives_num, users, users_num, outputfile, dimension_of_coords, p_nearest, coins, coins_size, tweets_num);
clock_gettime(CLOCK_MONOTONIC_RAW, &recom_end);
recom_time = (double)((recom_end.tv_sec - recom_start.tv_sec) * 1000000 + (recom_end.tv_nsec - recom_start.tv_nsec) / 1000);
recom_time = (double)recom_time/1000;
//write results of clustering recommendation
write_to_outputfile_recom(outputfile, users, users_num, recom_time, 2);
//print results
print_all_results(users, users_num, recom_time, 2);
//print_users(users, users_num, coins_size, coins);
//deleteS --------------------------------------------------------------------------------
delete_users(users_representatives, users_representatives_num, coins_size);
delete_users(users, users_num, coins_size);
//delete_users(users_with_coins, users_with_coins_num, coins_size);
if(print_msgs_recom_on == 1)
printf("\n\nRecom Steps Out\n");
return 22;
}
double calculate_sentim_score(struct tweets* tweets[], long long tweets_num, struct sentim_score_map* sentim_map[], long long sentim_map_size)
{
double tot_score = 0.0, temp_score = 0.0;
for( long long y = 0; y < tweets_num; y++ )
{
struct tweet_content* templist = NULL;
templist = get_tweet_content_tweets(tweets, y);
char* tempstr = NULL;
while(templist!=NULL)
{
tempstr = get_word_tweet_content(templist);
temp_score += get_word_score(sentim_map, sentim_map_size, tempstr);
templist = get_next_tweet_content(templist);
}
//printf(" \n%lf", temp_score);
tot_score = (double) ( (double)temp_score )/( (double)sqrt( pow(temp_score, 2) + alpha ) );
//printf("\n%lf", tot_score);
save_tweet_score_tweets(tweets, y, tot_score);
temp_score = 0.0;
}
return 66;
}
void normalise_vector_user_crypto(struct users* users[], long long users_num, int coins_num)
{
double temp_score = 0.0;
for(long long i = 0; i < users_num; i++)
{
temp_score = 0;
for(int t = 0; t < coins_num; t++)
{
if((get_user_crypto_rating_users(users, i, t))!=INFINITY)
{
temp_score += get_user_crypto_rating_users(users, i, t) ;
}
}
temp_score = (double)temp_score / (double)(coins_num);
save_average_rating_users(users,i,temp_score);
double new_rating = 0;
for(int t = 0; t < coins_num; t++)
{
if((get_user_crypto_rating_users(users, i, t))!=INFINITY)
{
new_rating = get_user_crypto_rating_users(users, i, t) - temp_score;
save_user_crypto_rating_users(users, i, t, new_rating );
}
else
{
save_user_crypto_rating_users(users, i, t, 0);
}
}
}
}
void calculate_average_rating(struct users* users[], long long users_num, int coins_num)
{
int tot_coins_in_user = 0;
double temp_score = 0.0, cur_score = 0.0;
for(long long i = 0; i < users_num; i++)
{
temp_score = 0;
for(int t = 0; t < coins_num; t++)
{
cur_score = get_user_crypto_rating_users(users, i, t);
if(cur_score!=INFINITY)
{
temp_score += get_user_crypto_rating_users(users, i, t) ;
}
}
tot_coins_in_user = get_tot_coins_users(users,i);
if(!tot_coins_in_user)
{
temp_score = 0;
//printf("\nNO COINS");
}
else
{
temp_score = (double)temp_score / (double)(tot_coins_in_user);
}
save_average_rating_users(users,i,temp_score);
for(int t = 0; t < coins_num; t++)
{
cur_score = get_user_crypto_rating_users(users, i, t);
if(cur_score==INFINITY)
{
save_user_crypto_rating_users(users, i, t, temp_score) ;
}
}
// printf("\nScore: ");
// for(int t = 0; t < coins_num; t++)
// {
// cur_score = get_user_crypto_rating_users(users, i, t);
// printf("%lf " , get_user_crypto_rating_users(users, i, t) );
// }
}
}
int check_if_zero(struct users* users[], long long pos, int coins_num)
{
int its_zero = 1;
for(int i = 0; i < coins_num; i++)
{
if(((get_user_crypto_rating_users(users,pos,i)) != 0) && ((get_user_crypto_rating_users(users,pos,i)) != INFINITY))
{
its_zero = 0;
//save_empty_user_users(users,pos,its_zero);
return its_zero;
}
}
return its_zero;
}
long long count_users_with_coins(struct users* users[], long long users_num, int coins_num)
{
long long users_with_coins_num = 0;
int coins_temp = 0;
int all_zero = 1, aver_zero = 0;
for(long long y = 0; y < users_num; y++)
{
if((get_tot_tweets_users(users, y))>0)
{
all_zero = check_if_zero(users, y, coins_num);
// if(!get_average_rating_users(users, y))
// {
// aver_zero = 1;
// }
// else
// {
// aver_zero = 0;
// }
if(!all_zero)
{
users_with_coins_num++;
save_empty_user_users(users, y, 0);
}
else
{
save_empty_user_users(users, y, 1);
}
}
}
return users_with_coins_num;
}
void clear_users_with_no_coins(struct users* users[], long long users_num, struct users* users_with_coins[], long long users_with_coins_num, int coins_num)
{
long long counter = 0;
for (long i = 0; i < users_num; i++)
{
if((get_empty_user_users(users,i))==0)
{
copy_users(users, i, users_with_coins, counter, coins_num);
counter++;
}
}
}