-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-engine.php
More file actions
202 lines (169 loc) · 6.53 KB
/
search-engine.php
File metadata and controls
202 lines (169 loc) · 6.53 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
<?php
// Find the files to read in the inverted index
function get_filenames($keywords) {
require('config.php');
$files = array();
foreach ($keywords as $keyword) {
if (strlen($keyword) < $min_length_keyword) {
continue;
}
$first_letter = mb_substr($keyword, 0, 1);
$second_letter = mb_substr($keyword, 1, 1);
$two_letters = $first_letter . $second_letter;
if (ctype_alpha($two_letters)) {
$file = strtoupper($first_letter) . '/' . $two_letters . $index_ext;
}
else {
if (ctype_lower($first_letter)) {
$file = strtoupper($first_letter) . '/' . $first_letter . '-sp' . $index_ext;
}
elseif (ctype_lower($second_letter)) {
$file = 'SP/sp-' . $second_letter . $index_ext;
}
else {
$file = 'SP/sp-sp' . $index_ext;
}
}
if (!in_array($file, $files)) {
$files[] = $file;
}
}
return $files;
}
// Read the inverted-index
function read_invert_index($files, $keywords) {
require('config.php');
$words = array();
// For each language folder (ex: en)
foreach ($languages as $language) {
// For each file to open (ex: aa.sif)
foreach ($files as $file) {
$path = $index_folder . strtoupper($language) . '/' . $file;
if (file_exists($path)) {
$json = json_decode(file_get_contents($path), true);
// For each keyword of query
foreach ($keywords as $keyword) {
// If this keyword is in the inverted-index
if (isset($json[$keyword])) {
if (!isset($words[$keyword]['nb_results'])) {
$words[$keyword]['nb_results'] = 0;
}
// For each website
foreach ($json[$keyword] as $id => $tf) {
$words[$keyword]['results'][$id] = $tf;
$words[$keyword]['nb_results'] += 1;
}
}
}
}
}
}
return $words;
}
// Ranking
function compute_tfidf($words, $index_size) {
require('config.php');
$rank_results = array();
// #1 TF-IDF
foreach ($words as $word => $ids_per_word) {
$idf = log($index_size / $ids_per_word['nb_results']);
foreach ($ids_per_word['results'] as $id => $tf) {
$tf_idf = ($ids_per_word['results'][$id] * $idf);
if (!isset($rank_results[$id])) {
// Create it
$rank_results[$id] = $tf_idf;
}
else {
// Update it
$rank_results[$id] += $tf_idf;
}
}
}
return $rank_results;
}
function search($keywords, $domain) {
require('config.php');
require('db.php');
$nb_keywords = count($keywords);
// Find the files to read in the inverted index
if ($nb_keywords > 0) {
$files = get_filenames($keywords, $min_length_keyword, $index_ext);
}
// Read the inverted-index
if (isset($files) && !empty($files)) {
$words = read_invert_index($files, $keywords, $languages, $index_folder);
}
$results = array();
$nb_results = 0;
$real_nb_results = 0;
$pages = 1;
$page = 1;
// Ranking
if (isset($words) && !empty($words)) {
// Get number of websites in our index
$index_size = get_index_size($db);
$rank_results = compute_tfidf($words, $index_size);
if (count($rank_results) > 0) {
// Limit results to $max_results
arsort($rank_results);
$rank_results = array_slice($rank_results, 0, $max_results, true);
$real_nb_results = count($rank_results);
$ids_array = array_keys($rank_results);
$in = str_repeat('?,', $real_nb_results - 1) . '?';
if ($domain !== '') {
$sql = "SELECT id, url, popularity, score, homepage FROM website w WHERE id IN ($in) AND w.domain LIKE '%$domain%'";
}
else {
$sql = "SELECT id, url, popularity, score, homepage FROM website WHERE id IN ($in)";
}
$stmt = $db->prepare($sql);
$stmt->execute($ids_array);
$websites = $stmt->fetchAll();
// #2 Score, #3 Popularity, #4 Homepage, #5 URL
$rank_results2 = array();
foreach ($websites as $website) {
$rank_results2[$website['id']] = $rank_results[$website['id']];
$score = $website['score'] / 4;
$popularity = $website['popularity'] * 0.05;
$homepage = 0;
if ($nb_keywords == 1 && $website['homepage'] == '1') {
$homepage = 1;
}
$keyword_url = 0;
foreach ($keywords as $keyword) {
if (strpos($website['url'], $keyword) !== false) {
$keyword_url = 2;
break;
}
}
$spam = ($rank_results2[$website['id']] > 1) ? true : false;
$rank_results2[$website['id']] += log($score + $popularity + $homepage + $keyword_url);
if ($spam) {
$rank_results2[$website['id']] /= 3;
}
}
}
$nb_results = count($rank_results2); // Real number of results (after SQL query)
if ($nb_results > 0) {
arsort($rank_results2); // Sort the results by their final score
$ids_array = array_keys($rank_results2);
// Pagination
$pages = ceil($nb_results / $max_results_per_page);
$page = isset($_GET['p']) && ctype_digit(strval($_GET['p'])) ? $_GET['p'] : 1;
if ($page < 1 || $page > $pages) {
$page = 1;
}
$first_site = ($page - 1) * $max_results_per_page;
$final_ids_array = array_slice($ids_array, $first_site, $max_results_per_page);
$nb_ids = count($final_ids_array);
$in = str_repeat('?,', $nb_ids - 1) . '?';
// Get final results
$sql = "SELECT title, description, url, favicon FROM website WHERE id IN ($in)";
$stmt = $db->prepare($sql);
$stmt->execute($final_ids_array);
$results = $stmt->fetchAll();
}
}
return array($results, $nb_results, $real_nb_results, $pages, $page);
}
?>