aboutsummaryrefslogtreecommitdiff
path: root/lucky/index.php
blob: d320290d1bede6913f265ebcb72dd445ab7652d3 (plain)
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
<?php

$data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/db.json"), true);
$dict = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/dictionary.json"), true);

if (isset($_GET['q'])) {
    $query = strtolower(substr($_GET['q'], 0, 200));
    $query = preg_replace("/[^A-Za-z0-9 ]/", '', preg_replace("/[\.]/", ' ', $query));
} else {
    header("Location: /");
    die();
}

if (trim($query) === "") {
    header("Location: /");
    die();
}

// Autocorrect
$start_correct = microtime(true);
$fixed = false;
$fixes = [];
$fixesd = [];

foreach (explode(" ", $query) as $item) {
    $shortest = -1;
    foreach ($dict as $word) {

        $lev = levenshtein($item, $word);

        if ($lev == 0) {
            $closest = $word;
            $shortest = 0;

            break;
        }

        if ($lev <= $shortest || $shortest < 0) {
            $closest  = $word;
            $shortest = $lev;
        }
    }

    if ($shortest !== 0 && $shortest < 15 && $item !== "wolfeye" && $item !== "eye") {
        $fixed = true;
        $fixes[] = str_replace($item, $closest, $query);
        $fixesd[] = str_replace($item, "<b>$closest</b>", $query);
    }

    if ($query === "google") {
        $fixed = true;
        $fixes[] = str_replace($item, "duckduckgo", $query);
        $fixesd[] = str_replace($item, "<b>duckduckgo</b>", $query);
    }

    if ($query === "microsoft") {
        $fixed = true;
        $fixes[] = str_replace($item, "redhat", $query);
        $fixesd[] = str_replace($item, "<b>redhat</b>", $query);
    }

    if ($query === "windows") {
        $fixed = true;
        $fixes[] = str_replace($item, "linux", $query);
        $fixesd[] = str_replace($item, "<b>linux</b>", $query);
    }

    if ($query === "apple") {
        $fixed = true;
        $fixes[] = str_replace($item, "raspberry", $query);
        $fixesd[] = str_replace($item, "<b>raspberry</b>", $query);
    }

    if ($query === "france") {
        $fixed = true;
        $fixes[] = str_replace($item, "hell", $query);
        $fixesd[] = str_replace($item, "<b>hell</b>", $query);
    }
}

if ($fixed) {
    echo("<div class='dym-section'>");
    if (count($fixes) > 1) {
        echo("Showing results for \"$query\", did you mean:<ul>");
        foreach ($fixes as $i => $fix) {
            echo("<li>\"<a class='dym-link' href='/search?q=$fix'>$fixesd[$i]</a>\"?</li>");
        }
        echo("</ul>");
    } else {
        echo("Showing results for \"$query\", did you mean \"<a class='dym-link' href='/search?q=$fixes[0]'>$fixesd[0]</a>\"?");
    }
    echo("</div>");
}
$elapsed_correct = microtime(true) - $start_correct;

// Actual Search
$start_search = microtime(true);
$matches = [];
foreach ($data["entries"] as $id => $item) {
    $avg = -1;
    $aavg = [];
    $iparsed = strtolower(substr($item["title"], 0, 200));
    $iparsed = preg_replace("/[^A-Za-z0-9 ]/", '', preg_replace("/[\.]/", ' ', $iparsed));
    $qwords = explode(" ", $query);

    if (trim($iparsed) !== "") {
        foreach ($qwords as $qword) {
            $sim = similar_text($iparsed, $qword, $perc);
            if ($perc > 0) {
                $aavg[] = $perc;
            }
        }

        if (count($aavg) > 0) {
            $aavg = array_filter($aavg);
            $avg = (array_sum($aavg) / count($aavg)) / 50 - 1;
            $matches[] = [
                "id" => $id,
                "relevance" => $avg
            ];
        }
    }

    $avg = -1;
    $aavg = [];
    $iparsed = strtolower(substr($item["url"], 0, 200));
    $iparsed = preg_replace("/[^A-Za-z0-9 ]/", '', preg_replace("/[\.]/", ' ', $iparsed));
    $qwords = explode(" ", $query);

    if (trim($iparsed) !== "") {
        foreach ($qwords as $qword) {
            $sim = similar_text($iparsed, $qword, $perc);
            if ($perc > 0) {
                $aavg[] = $perc;
            }
        }

        if (count($aavg) > 0) {
            $aavg = array_filter($aavg);
            $avg = (array_sum($aavg) / count($aavg)) / 50 - 1;
            $matches[] = [
                "id" => $id,
                "relevance" => $avg
            ];
        }
    }
}

usort($matches, function($a, $b) {
    return $a['relevance'] <=> $b['relevance'];
});

$matches = array_reverse($matches);

$matches2 = [];
$prel = [];

foreach($matches as $match) {
    if (!isset($prel[(string)$match["relevance"]])) {
        $prel[(string)$match["relevance"]] = [$match];
    } else {
        $prel[(string)$match["relevance"]][] = $match;
    }
}

$elapsed_search = microtime(true) - $start_search;
$start_parse = microtime(true);
$entriesdom = [];
foreach($prel as $item => $items) {
    $shortest = -1;

    foreach ($items as $word) {
        $lev = levenshtein($query, $data["entries"][$word["id"]]["title"]);

        if ($lev == 0) {
            $closest = $word["id"];
            $shortest = 0;
            break;
        }

        if ($lev <= $shortest || $shortest < 0) {
            $closest  = $word["id"];
            $shortest = $lev;
        }
    }

    $entriesdom[] = [
        "link" => "/out?q=" . $query . "&u=" . uniqid() . "&i=" . $closest,
        "title" => $data["entries"][$closest]["title"],
        "url" => $data["entries"][$closest]["url"],
        "icon" => "/icon?q=" . $query . "&i=" . $closest,
    ];
}

header("Location: " . $entriesdom[0]["link"]);
die();