1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 04:23:01 +00:00

Fiddle with it a bit, but it looks like further investigation is needed.

This commit is contained in:
Starbeamrainbowlabs 2020-03-15 18:26:17 +00:00
parent d131666ff5
commit 99f920f11d
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 29 additions and 26 deletions

View file

@ -40,6 +40,7 @@ register_module([
if(module_exists("feature-cli")) { if(module_exists("feature-cli")) {
cli_register("didyoumean", "Query and manipulate the didyoumean index", function(array $args) : int { cli_register("didyoumean", "Query and manipulate the didyoumean index", function(array $args) : int {
global $settings;
if(count($args) < 1) { if(count($args) < 1) {
echo("didyoumean: query and manipulate the didyoumean index echo("didyoumean: query and manipulate the didyoumean index
Usage: Usage:
@ -47,7 +48,8 @@ Usage:
Subcommands: Subcommands:
rebuild Rebuilds the didyoumean index rebuild Rebuilds the didyoumean index
correct {word} Corrects {word} using the didyoumean index (careful: it's case-sensitive and operates on transliterated text *only*) correct {word} Corrects {word} using the didyoumean index (careful: the index is case-sensitive and operates on transliterated text *only*)
lookup {word} Looks up {word} in the didyoumean index and displays all the (unsorted) results.
"); ");
return 0; return 0;
} }
@ -57,12 +59,25 @@ Subcommands:
search::didyoumean_rebuild(); search::didyoumean_rebuild();
break; break;
case "correct": case "correct":
search::didyoumean_load();
if(count($args) < 2) { if(count($args) < 2) {
echo("Error: Not enough arguments\n"); echo("Error: Not enough arguments\n");
return 1; return 1;
} }
echo("Correction: ".search::didyoumean_correct($args[1])."\n"); $correction = search::didyoumean_correct($args[1]);
if($correction === null) $correction = "(nothing found)";
echo("Correction: $correction\n");
break;
case "lookup":
if(count($args) < 2) {
echo("Error: Not enough arguments\n");
return 1;
}
search::didyoumean_load();
$results = search::$didyoumeanindex->lookup(
$args[1],
$settings->search_didyoumean_editdistance
);
var_dump($results);
break; break;
} }
@ -324,18 +339,6 @@ class BkTree {
return $stack; return $stack;
} }
/**
* Convenience function that returns just the first result when looking up a string.
* @param string $string The string to lookup
* @param integer $distance The maximum edit distance to search.
* @return string|null The first matching string, or null if no results were found.
*/
public function lookup_one(string $string, int $distance = 1) : ?string {
$result = $this->lookup($string, $distance, 1);
if(empty($result)) return null;
return $result[0];
}
/** /**
* Generator that walks the BK-Tree and iteratively yields results. * Generator that walks the BK-Tree and iteratively yields results.
* Note that the returned array is *not* sorted. * Note that the returned array is *not* sorted.

View file

@ -86,7 +86,7 @@ class search
* Only populated if the feature-search-didyoumean module is present. * Only populated if the feature-search-didyoumean module is present.
* @var BkTree * @var BkTree
*/ */
private static $didyoumeanindex = null; public static $didyoumeanindex = null;
/** /**
* The transliterator that can be used to transliterate strings. * The transliterator that can be used to transliterate strings.
@ -670,26 +670,26 @@ class search
"exact" => $exact // If true then we shouldn't try to autocorrect it "exact" => $exact // If true then we shouldn't try to autocorrect it
]; ];
} }
// Correct typos, but only if that's enabled // Correct typos, but only if that's enabled
if(module_exists("feature-search-didyoumean") && $settings->search_didyoumean_enabled) { if(module_exists("feature-search-didyoumean") && $settings->search_didyoumean_enabled) {
foreach($result["terms"] as $term_data) { $terms_count = count($result["terms"]);
if($term_data["exact"] || // Skip exact-only for($i = 0; $i < $terms_count; $i++) {
$term_data["weight"] < 1 || // Skip stop & irrelevant words if($result["terms"][$i]["exact"] || // Skip exact-only
self::invindex_term_exists($term_data["term"])) continue; $result["terms"][$i]["weight"] < 1 || // Skip stop & irrelevant words
self::invindex_term_exists($result["terms"][$i]["term"])) continue;
// It's not a stop word or in the index, try and correct it // It's not a stop word or in the index, try and correct it
// self::didyoumean_correct auto-loads the didyoumean index on-demand // self::didyoumean_correct auto-loads the didyoumean index on-demand
$correction = self::didyoumean_correct($term_data["term"]); $correction = self::didyoumean_correct($result["terms"][$i]["term"]);
// Make a note if we fail to correct a term // Make a note if we fail to correct a term
if(!is_string($correction)) { if(!is_string($correction)) {
$term_data["corrected"] = false; $result["terms"][$i]["corrected"] = false;
continue; continue;
} }
$term_data["term_before"] = $term_data["term"]; $result["terms"][$i]["term_before"] = $result["terms"][$i]["term"];
$term_data["term"] = $correction; $result["terms"][$i]["term"] = $correction;
$term_data["corrected"] = true; $result["terms"][$i]["corrected"] = true;
} }
} }