From dc8f78256944885108f1d52b822c8677bf9d964c Mon Sep 17 00:00:00 2001 From: Minteck Date: Sun, 12 Dec 2021 11:19:38 +0100 Subject: Initial commit --- search/index.php | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 search/index.php (limited to 'search/index.php') diff --git a/search/index.php b/search/index.php new file mode 100644 index 0000000..709a239 --- /dev/null +++ b/search/index.php @@ -0,0 +1,270 @@ + + + + + + <?= ucfirst($query) ?> on WolfEye + + + + +
+ +
+ + +
+
+ +
$closest", $query); + } + + if ($query === "google") { + $fixed = true; + $fixes[] = str_replace($item, "duckduckgo", $query); + $fixesd[] = str_replace($item, "duckduckgo", $query); + } + + if ($query === "microsoft") { + $fixed = true; + $fixes[] = str_replace($item, "redhat", $query); + $fixesd[] = str_replace($item, "redhat", $query); + } + + if ($query === "windows") { + $fixed = true; + $fixes[] = str_replace($item, "linux", $query); + $fixesd[] = str_replace($item, "linux", $query); + } + + if ($query === "apple") { + $fixed = true; + $fixes[] = str_replace($item, "raspberry", $query); + $fixesd[] = str_replace($item, "raspberry", $query); + } + + if ($query === "france") { + $fixed = true; + $fixes[] = str_replace($item, "hell", $query); + $fixesd[] = str_replace($item, "hell", $query); + } + } + + if ($fixed) { + echo("
"); + if (count($fixes) > 1) { + echo("Showing results for \"$query\", did you mean:"); + } else { + echo("Showing results for \"$query\", did you mean \"$fixesd[0]\"?"); + } + echo("
"); + } + $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, + ]; + } + $elapsed_parse = microtime(true) - $start_parse; + ?> +

results

+ +
+ + + +
+ $entry): ?> + $id, + "url" => "https://" . $domain . "/", + "domain" => $domainstr, + "relevance" => $perc / 50 - 1 + ]; + ?> + $b['relevance']; + }); + + $domains = array_reverse($domains); if ($domains[0]["relevance"] > 0): ?> + + + + + +
+ + + + + -- cgit