From a8a6137e2797c36b9882780aef5cc4f9448c426b Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Fri, 24 Nov 2023 18:55:49 +0700 Subject: [PATCH] use threshold scoring --- src/System/Integrate/Console/Karnel.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/System/Integrate/Console/Karnel.php b/src/System/Integrate/Console/Karnel.php index aef7820a..80f65219 100644 --- a/src/System/Integrate/Console/Karnel.php +++ b/src/System/Integrate/Console/Karnel.php @@ -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++; } @@ -91,22 +88,14 @@ public function handle($arguments) * * @return array 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);