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:
parent
d131666ff5
commit
99f920f11d
2 changed files with 29 additions and 26 deletions
|
@ -40,6 +40,7 @@ register_module([
|
|||
|
||||
if(module_exists("feature-cli")) {
|
||||
cli_register("didyoumean", "Query and manipulate the didyoumean index", function(array $args) : int {
|
||||
global $settings;
|
||||
if(count($args) < 1) {
|
||||
echo("didyoumean: query and manipulate the didyoumean index
|
||||
Usage:
|
||||
|
@ -47,7 +48,8 @@ Usage:
|
|||
|
||||
Subcommands:
|
||||
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;
|
||||
}
|
||||
|
@ -57,12 +59,25 @@ Subcommands:
|
|||
search::didyoumean_rebuild();
|
||||
break;
|
||||
case "correct":
|
||||
search::didyoumean_load();
|
||||
if(count($args) < 2) {
|
||||
echo("Error: Not enough arguments\n");
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -324,18 +339,6 @@ class BkTree {
|
|||
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.
|
||||
* Note that the returned array is *not* sorted.
|
||||
|
|
|
@ -86,7 +86,7 @@ class search
|
|||
* Only populated if the feature-search-didyoumean module is present.
|
||||
* @var BkTree
|
||||
*/
|
||||
private static $didyoumeanindex = null;
|
||||
public static $didyoumeanindex = null;
|
||||
|
||||
/**
|
||||
* 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
|
||||
];
|
||||
}
|
||||
|
||||
// Correct typos, but only if that's enabled
|
||||
if(module_exists("feature-search-didyoumean") && $settings->search_didyoumean_enabled) {
|
||||
foreach($result["terms"] as $term_data) {
|
||||
if($term_data["exact"] || // Skip exact-only
|
||||
$term_data["weight"] < 1 || // Skip stop & irrelevant words
|
||||
self::invindex_term_exists($term_data["term"])) continue;
|
||||
$terms_count = count($result["terms"]);
|
||||
for($i = 0; $i < $terms_count; $i++) {
|
||||
if($result["terms"][$i]["exact"] || // Skip exact-only
|
||||
$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
|
||||
// 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
|
||||
if(!is_string($correction)) {
|
||||
$term_data["corrected"] = false;
|
||||
$result["terms"][$i]["corrected"] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
$term_data["term_before"] = $term_data["term"];
|
||||
$term_data["term"] = $correction;
|
||||
$term_data["corrected"] = true;
|
||||
$result["terms"][$i]["term_before"] = $result["terms"][$i]["term"];
|
||||
$result["terms"][$i]["term"] = $correction;
|
||||
$result["terms"][$i]["corrected"] = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue