Skip to content

Commit

Permalink
use threshold scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Nov 24, 2023
1 parent c272733 commit a8a6137
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/System/Integrate/Console/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public function handle($arguments)
// did you mean
$count = 0;
$similar = (new Style('Did you mean?'))->textLightYellow()->newLines();
foreach ($this->similar($baseArgs, $commands) as $term => $level) {
if ($level > 4) { // rating score
break;
}
foreach ($this->similar($baseArgs, $commands, 4) as $term => $level) {
$similar->push(' > ')->push($term)->textYellow()->newLines();
$count++;
}
Expand Down Expand Up @@ -91,22 +88,14 @@ public function handle($arguments)
*
* @return array<string, int> Sorted from simalar
*/
private function similar(string $find, $matchs)
private function similar(string $find, $matchs, int $threshold = -1)
{
$shortest = -1;
$closest = [];

foreach ($matchs as $match) {
$level = levenshtein($find, $match);
if (0 === $level) {
$closest[$match] = $level;
$shortest = 0;

break;
}
if ($level <= $shortest || $shortest < 0) {
if ($level <= $threshold) {
$closest[$match] = $level;
$shortest = $level;
}
}
asort($closest);
Expand Down

0 comments on commit a8a6137

Please sign in to comment.