-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.php
499 lines (476 loc) · 13.9 KB
/
statistics.php
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
/* this php will contain some statistic class, which for
* AOL 24 hour timestamp log
* the base Statistics class will count the hourly query-url click count under
* certain boundary
* the NavieCluster will find out the similar urls-click behavior under a
* certain query.
* the Entropy will find out if the urls-click behavior is larger than
* the threshold
*/
include_once("connection.php");
mysql_select_db($database_cnn,$b95119_cnn);
class Statistics{
public $lb = -1;
public $up = -1;
public $DB = NULL;
public $safe_q = NULL;
public $unsafe_q = NULL;
public function __construct($para){
if (isset($para["TB"])){
$this->DB = $para["TB"];
}else{
echo "para['TB'] missing, the object failed to be created\n";
return -1;
}
if (isset($para["low"])){
$this->lb = intval($para["low"]);
}
if (isset($para["up"])){
$this->up = intval($para["up"]);
}
}
function CountQueryURLPair(){
$sum = 0.0;
for ($i = 0; $i < 24;$i++){
$sum += (double) $this->CountQueryURLHourlyPair($i);
}
$average = $sum / 24.0;
echo $sum."\n";
echo $average."\n";
return $sum;
}
function CountQueryURLHourlyPair($t){
// when this->lb == -1 and this->up == -1, it will get the all records.
$query = sprintf("
select `query`, `url` from `%s` where `%d` > %d
", $this->DB, $t ,$this->lb);
if ($this->up > 0){
$query = sprintf("%s and `%d` < %d", $query, $t, $this->up);
}
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$num = mysql_num_rows($result);
return $num;
}
public function FindQuery(){
$query = sprintf("select distinct `query` from `%s` ", $this->DB);
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$this->safe_q = array();
$this->unsafe_q = array();
while ($row = mysql_fetch_row($result)){
$this->safe_q[] = preg_replace("/\'/", "\\\'", $row[0]);
$this->unsafe_q[] = $row[0];
}
return $this->safe_q;
}
}
class NavieCluster extends Statistics{
public $q;
public $Threshold = 3;
public $Cluster;
public $group = array(); // from group index to query-url
public $group_invert = array(); // from query-url to group index
function __construct($para){
parent::__construct($para);
if (isset($para["Threshold"])){
$this->Threshold = intval($para["Threshold"]);
}
}
public function FindQuery(){
$query = sprintf("select distinct `query` from `%s` ", $this->DB);
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$this->q = array();
while ($row = mysql_fetch_row($result)){
$this->q[] = preg_replace("/\'/", "\\\'", $row[0]);
}
return $this->q;
}
public function ClusterUrlPairs(){
$this->FindQuery();
$this->Cluster = array();
for ($i= 0;$i< count($this->q);$i++){
$query = sprintf("select * from `%s` where `query` = '%s'",
$this->DB, $this->q[$i]);
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$tmp = $this->_FindCluster($result);
if (!empty($tmp)){
$this->Cluster[$this->q[$i]] = $tmp;
}
}
//print_r($this->Cluster);
return $this->Cluster;
}
private function _FindCluster($result){
$rows = array();
while ($row = mysql_fetch_row($result)){
$rows[] = $row;
}
$GroupSet = array();
foreach ($rows as $i => $unGroupURL){
$CreateFlag = true;
foreach($GroupSet as $j => $Group){
foreach($Group as $k => $GroupURL){
if ($this->isSimilar($unGroupURL, $GroupURL)){
$GroupSet[$j][] = $unGroupURL;
$CreateFlag = false;
break;
}
}
if ($CreateFlag == false){
break;
}
}
if ($CreateFlag == true){
$newGroupIndex = count($GroupSet);
$GroupSet[$newGroupIndex] = array();
$GroupSet[$newGroupIndex][0] = $unGroupURL;
}
}
foreach($GroupSet as $j => $Group){
foreach($Group as $k => $GroupURL){
$output[$j][$k] = $GroupURL[2];
}
}
return $output;
}
private function isSimilar($row1,$row2){
$counter = 0;
for ($i = 3; $i < 27; $i++){
if ( abs($row1[$i] - $row2[$i]) < $this->Threshold ){
$counter++;
}
}
if ($counter >=24) {
return true;
}else{
return false;
}
}
public function displaySimilar(){
foreach ($this->Cluster as $q => $cluster){
foreach ($cluster as $urls){
if (count($urls) > 1){
echo $q."\n";
foreach ($urls as $u){
echo "\t".$u."\n";
}
}
}
}
}
}
class Entropy extends Statistics{
private $query_weight = array();
private $url_weight = array();
private $query_url_weight = array();
//private $safe_q = null;
//private $unsafe_q = null;
public function __construct($para){
parent::__construct($para);
if (!isset($para["low"])){
$this->lb = 1;
}
}
public function FindQuery(){
$query = sprintf("select distinct `query` from `%s` ", $this->DB);
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$this->safe_q = array();
$this->unsafe_q = array();
while ($row = mysql_fetch_row($result)){
$this->safe_q[] = preg_replace("/\'/", "\\\'", $row[0]);
$this->unsafe_q[] = $row[0];
}
return $this->safe_q;
}
public function AverageInHour($t){
// it calculate the average entropy and the max in time t
$result = $this->_AverageInHour($t);
$ret["average"] = $result["average"];
$ret["max_set"] = $result["max_set"];
print_r($ret);
return $ret;
}
public function _AverageInHour($t){
// it calculate the average entropy and the max in time t
if ($this->safe_q == NULL){
$this->FindQuery();
}
$sum = 0.0;
$weight_sum = 0.0;
$max_set = null;
$max = 0.0;
foreach($this->safe_q as $i => $v){
$Q_URLs[$v] = $this->SpecificQ_URLsInHour($v, $t);
$sum += $Q_URLs[$v]["entropy"] * $Q_URLs[$v]["click"];
$weight_sum += $Q_URLs[$v]["click"];
if ($max < $Q_URLs[$v]["entropy"]) {
$max_set = array(); // clear
$max_set[$v] = $Q_URLs[$v];
$max = $Q_URLs[$v]["entropy"];
}else if ($max == $Q_URLs[$v]["entropy"]){
$max_set[$v] = $Q_URLs[$v]; // add more
}
}
$ret["Q_URLs"] = $Q_URLs;
$ret["average"] = $sum / $weight_sum;
$ret["max_set"] = $max_set;
$ret["sum"] = $sum;
$ret["weight_sum"] = $weight_sum;
//echo $ret["average"];
//print_r($ret);
return $ret;
}
public function DistributionInHour($t){
// it calculate the distribution of entropy and the max in time t
// it will replace the function _AverageInHour in the future
if ($this->safe_q == NULL){
$this->FindQuery();
}
$sum = 0.0;
$weight_sum = 0.0;
$max_set = null;
$max = 0.0;
$distribution = array();
foreach($this->safe_q as $i => $v){
$Q_URLs[$v] = $this->SpecificQ_URLsInHour($v, $t);
$sum += $Q_URLs[$v]["entropy"] * $Q_URLs[$v]["click"];
$weight_sum += $Q_URLs[$v]["click"];
if ($max < $Q_URLs[$v]["entropy"]) {
$max_set = array(); // clear
$max_set[$v] = $Q_URLs[$v];
$max = $Q_URLs[$v]["entropy"];
}else if ($max == $Q_URLs[$v]["entropy"]){
$max_set[$v] = $Q_URLs[$v]; // add more
}
$string_entropy = sprintf("%.2lf", $Q_URLs[$v]["entropy"]);
if (!isset($distribution[$string_entropy])){
$distribution[$string_entropy]["click"] = 0.0;
}
$distribution[$string_entropy]["click"] += $Q_URLs[$v]["click"];
$distribution[$string_entropy]["Q_URLs"][$v] = $Q_URLs[$v];
}
foreach($distribution as $i => $v){
$distribution[$i]["prob"] = $distribution[$i]["click"] / $weight_sum;
}
ksort($distribution);
$ret["distribution"] = $distribution;
//print_r($distribution);
$ret["Q_URLs"] = $Q_URLs;
$ret["average"] = $sum / $weight_sum;
$ret["max_set"] = $max_set;
$ret["sum"] = $sum;
$ret["weight_sum"] = $weight_sum;
return $ret;
}
public function AverageInDay(){
// it calculate the average entropy and the max in a day
if ($this->safe_q == NULL){
$this->FindQuery();
}
$sum = 0.0;
$weight_sum = 0.0;
$max_set = null;
$max = 0.0;
for ($t = 0; $t < 24;$t++){
$result = $this->_AverageInHour($t);
//$max_set[$t] = $result["max_set"];
$sum += $result["sum"];
$weight_sum += $result["weight_sum"];
echo $t."\n";
print_r($result["max_set"]);
foreach ($result["max_set"] as $i => $v){
if ($max < $result["max_set"][$i]["entropy"]) {
$max_set = array(); // clear
$max_set[$t] = $result["max_set"];
$max = $result["max_set"][$i]["entropy"];
}else if ($max == $result["max_set"][$i]["entropy"]){
$max_set[$t] = $result["max_set"]; // add more
}
break;
}
}
//print_r($Q_URLs);
$ret["average"] = $sum / $weight_sum;
$ret["max_set"] = $max_set;
//echo $ret["average"];
print_r($ret);
return $ret;
}
public function DistributionInDay(){
// it calculate the average entropy and the max in a day
if ($this->safe_q == NULL){
$this->FindQuery();
}
$sum = 0.0;
$weight_sum = 0.0;
$max_set = null;
$max = 0.0;
$distribution = array();
for ($t = 0; $t < 24;$t++){
$result = $this->DistributionInHour($t);
$sum += $result["sum"];
$weight_sum += $result["weight_sum"];
echo $t."\n";
//print_r($result["max_set"]);
foreach ($result["max_set"] as $i => $v){
if ($max < $result["max_set"][$i]["entropy"]) {
$max_set = array(); // clear
$max_set[$t] = $result["max_set"];
$max = $result["max_set"][$i]["entropy"];
}else if ($max == $result["max_set"][$i]["entropy"]){
$max_set[$t] = $result["max_set"]; // add more
}
break;
}
foreach ($result["distribution"] as $string_entropy => $dist_value){
if (!isset($distribution[$string_entropy])){
$distribution[$string_entropy]["click"] = 0.0;
}
$distribution[$string_entropy]["click"] += $dist_value["click"];
$distribution[$string_entropy]["Q_URLs"][$t] = $dist_value["Q_URLs"];
}
}
foreach($distribution as $i => $v){
$distribution[$i]["prob"] = $distribution[$i]["click"] / $weight_sum;
}
ksort($distribution);
$ret["distribution"] = $distribution;
$ret["average"] = $sum / $weight_sum;
$ret["max_set"] = $max_set;
//echo $ret["average"];
//print_r($ret);
return $ret;
}
public function SpecificQ_URLsInHour($q, $t){
// measure the entropy of Query q => URls in time t
// it will return the entropy of the $q and the total click caused by $q
// in bound
$query = sprintf("
select `url`, `%d` from `%s` where `query` = '%s' and `%d` >= %d
", $t, $this->DB, $q , $t , $this->lb );
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
/*
$num = mysql_num_rows($result);
if ($num == 0){
//echo "no result for ".$q." in ".$t." under lower bound =".$this->lb."\n";
return 0.0;
}*/
$sum = 0.0;
while($row = mysql_fetch_row($result)){
$sum += doubleval($row[1]);
$url[$row[0]] = doubleval($row[1]);
}
// outsize bound will consider as other.
$query = sprintf("
select sum(`%d`) from `%s` where `query` = '%s' and `%d` < %d
", $t, $this->DB, $q , $t , $this->lb );
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$counter = 0;
while($row = mysql_fetch_row($result)){
$sum += doubleval($row[0]);
$url["LOWREBOUND"] = doubleval($row[0]);
if ($counter >0){
echo "error: counter should be 0\n";
}
}
$entropy = 0.0;
foreach ($url as $i => $v){
if ($v > 0.0){
$p = $v / $sum;
$entropy -= $p * log($p);
}
}
$ret["entropy"] = $entropy;
$ret["click"] = $sum;
return $ret;
}
public function NonSpecificQ_URLsInHour($t){
// measure the entropy of URls being click in time t
// it will return the entropy and the total click in time t
// in bound
$query = sprintf("
select `url`, `%d` from `%s` where `%d` >= %d
", $t, $this->DB, $t , $this->lb );
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$sum = 0.0;
while($row = mysql_fetch_row($result)){
$sum += doubleval($row[1]);
$url[$row[0]] = doubleval($row[1]);
}
// outsize bound will consider as other.
$query = sprintf("
select sum(`%d`) from `%s` where `%d` < %d
", $t, $this->DB, $t , $this->lb );
$result = mysql_query($query) or die(mysql_error()."\nerror query\n".$query);
$counter = 0;
while($row = mysql_fetch_row($result)){
$sum += doubleval($row[0]);
$url["LOWREBOUND"] = doubleval($row[0]);
if ($counter >0){
echo "error: counter should be 0\n";
}
}
$entropy = 0.0;
foreach ($url as $i => $v){
if ($v > 0.0){
$p = $v / $sum;
$entropy -= $p * log($p);
}
}
$ret["entropy"] = $entropy;
$ret["click"] = $sum;
return $ret;
}
public function NonSpecificQ_DistributionInDay(){
// it calculate the distribution of entropy and
// find the max value accross the time t in day
$sum = 0.0;
$weight_sum = 0.0;
$max_set = null;
$max = 0.0;
$distribution = array();
for ($t = 0;$t < 24;$t++){
$T_Result[$t] = $this->NonSpecificQ_URLsInHour($t);
$sum += $T_Result[$t]["entropy"] * $T_Result[$t]["click"]; // for calculating mean
$weight_sum += $T_Result[$t]["click"];
if ($max < $T_Result[$t]["entropy"]) {
$max_set = array(); // clear
$max_set[$t] = $T_Result[$t];
$max = $T_Result[$t]["entropy"];
}else if ($max == $T_Result[$t]["entropy"]){
$max_set[$t] = $T_Result[$t]; // add more
}
// for calculation distribution
$string_entropy = sprintf("%.2lf", $T_Result[$t]["entropy"]);
if (!isset($distribution[$string_entropy])){
$distribution[$string_entropy]["click"] = 0.0;
}
$distribution[$string_entropy]["click"] += $T_Result[$t]["click"];
$distribution[$string_entropy]["T_Result"][$t] = $T_Result[$t];
}
foreach($distribution as $i => $v){
$distribution[$i]["prob"] = $distribution[$i]["click"] / $weight_sum;
}
ksort($distribution);
$ret["distribution"] = $distribution;
//print_r($distribution);
$ret["T_Result"] = $T_Result;
$ret["average"] = $sum / $weight_sum;
$ret["max_set"] = $max_set;
$ret["sum"] = $sum;
$ret["weight_sum"] = $weight_sum;
return $ret;
}
}
function ParameterParser($argc, $argv){
$para = array();
for ($i = 0; $i< $argc - 1; $i++){
$ret = preg_match("/^-(.*)/", $argv[$i], $match);
if ($ret == 1){
$para[$match[1]] = $argv[$i+1];
$i = $i +1;
}
}
return $para;
}
?>