Skip to content

Commit

Permalink
refactor: change Karnel Console find similarity using Jaro-Winkler
Browse files Browse the repository at this point in the history
…algo (#358)
  • Loading branch information
SonyPradana authored Jul 2, 2024
1 parent 214dd08 commit 6ffc4c6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 12 deletions.
109 changes: 98 additions & 11 deletions src/System/Integrate/Console/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function handle($arguments)
// did you mean
$count = 0;
$similar = (new Style('Did you mean?'))->textLightYellow()->newLines();
foreach ($this->similar($baseArgs, $commands, 4) as $term => $level) {
foreach ($this->getSimilarity($baseArgs, $commands, 0.8) as $term => $score) {
$similar->push(' > ')->push($term)->textYellow()->newLines();
$count++;
}
Expand Down Expand Up @@ -135,26 +135,113 @@ public function call(string $signature, array $parameter = []): int
/**
* Return similar from given array, compare with key.
*
* @param string[] $matchs
* @param string[] $commands
*
* @return array<string, int> Sorted from simalar
* @return array<string, float> Sorted from simalar
*/
private function similar(string $find, $matchs, int $threshold = -1)
private function getSimilarity(string $find, array $commands, float $threshold = 0.8): array
{
$closest = [];
$find = strtolower($find);
$closest = [];
$findLower = strtolower($find);

foreach ($matchs as $match) {
$level = levenshtein($find, strtolower($match));
if ($level <= $threshold) {
$closest[$match] = $level;
foreach ($commands as $command) {
$commandLower = strtolower($command);
$similarity = $this->jaroWinkler($findLower, $commandLower);

if ($similarity >= $threshold) {
$closest[$command] = $similarity;
}
}
asort($closest);

arsort($closest);

return $closest;
}

/**
* Calculate the similarity between two strings.
*
* @return float Similarity score (between 0 and 1)
*/
private function jaroWinkler(string $find, string $command): float
{
$jaro = $this->jaro($find, $command);

// Calculate the prefix length (maximum of 4 characters)
$prefixLength = 0;
$maxPrefixLength = min(strlen($find), strlen($command), 4);
for ($i = 0; $i < $maxPrefixLength; $i++) {
if ($find[$i] !== $command[$i]) {
break;
}
$prefixLength++;
}

return $jaro + ($prefixLength * 0.1 * (1 - $jaro));
}

/**
* Calculate the Jaro similarity between two strings.
*
* @return float the Jaro similarity score (between 0 and 1)
*/
private function jaro(string $find, string $command): float
{
$len1 = strlen($find);
$len2 = strlen($command);

if ($len1 === 0) {
return $len2 === 0 ? 1.0 : 0.0;
}

$matchDistance = (int) floor(max($len1, $len2) / 2) - 1;

$str1Matches = array_fill(0, $len1, false);
$str2Matches = array_fill(0, $len2, false);

$matches = 0;
$transpositions = 0;

// Find matching characters
for ($i = 0; $i < $len1; $i++) {
$start = max(0, $i - $matchDistance);
$end = min($i + $matchDistance + 1, $len2);

for ($j = $start; $j < $end; $j++) {
if ($str2Matches[$j] || $find[$i] !== $command[$j]) {
continue;
}
$str1Matches[$i] = true;
$str2Matches[$j] = true;
$matches++;
break;
}
}

if ($matches === 0) {
return 0.0;
}

// Count transpositions
$k = 0;
for ($i = 0; $i < $len1; $i++) {
if (false === $str1Matches[$i]) {
continue;
}
while (false === $str2Matches[$k]) {
$k++;
}
if ($find[$i] !== $command[$k]) {
$transpositions++;
}
$k++;
}

$transpositions /= 2;

return (($matches / $len1) + ($matches / $len2) + (($matches - $transpositions) / $matches)) / 3.0;
}

/**
* Get karne exit status code.
*
Expand Down
15 changes: 14 additions & 1 deletion tests/Integrate/Console/KarnelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,14 @@ public function itCanReturnSuggestionCommand()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'test']);
$exit = $karnel->handle(['cli', 'use:patern']);
$out = ob_get_clean();

$this->assertEquals(1, $exit);
$condition = Str::contains($out, 'Did you mean?');
$this->assertTrue($condition);
$condition = Str::contains($out, 'use:pattern');
$this->assertTrue($condition);
}

/** @test */
Expand Down Expand Up @@ -230,6 +232,17 @@ public function itCanCallCommand()

$this->assertEquals(0, $exit);
}

/**
* @test
*/
public function itCanGetSimilarCommand()
{
$karnel = new Karnel($this->app);
$result = (fn () => $this->{'getSimilarity'}('make:view', ['view:clear', 'make:view', 'make:controller']))->call($karnel);
$this->assertArrayHasKey('make:view', $result);
$this->assertArrayHasKey('make:controller', $result);
}
}

class NormalCommand extends Karnel
Expand Down

0 comments on commit 6ffc4c6

Please sign in to comment.