Skip to content

Commit

Permalink
change method name
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Jul 2, 2024
1 parent 7497ca4 commit 12fce9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 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, 0.8) as $term => $score) {
foreach ($this->getSimilarity($baseArgs, $commands, 0.8) as $term => $score) {
$similar->push(' > ')->push($term)->textYellow()->newLines();
$count++;
}
Expand Down Expand Up @@ -139,14 +139,14 @@ public function call(string $signature, array $parameter = []): int
*
* @return array<string, float> Sorted from simalar
*/
private function similar(string $find, array $commands, float $threshold = 0.8): array
private function getSimilarity(string $find, array $commands, float $threshold = 0.8): array
{
$closest = [];
$findLower = strtolower($find);

foreach ($commands as $command) {
$commandLower = strtolower($command);
$similarity = $this->similarity($findLower, $commandLower);
$similarity = $this->jaroWinkler($findLower, $commandLower);

if ($similarity >= $threshold) {
$closest[$command] = $similarity;
Expand All @@ -163,19 +163,18 @@ private function similar(string $find, array $commands, float $threshold = 0.8):
*
* @return float Similarity score (between 0 and 1)
*/
private function similarity(string $find, string $command): float
private function jaroWinkler(string $find, string $command): float
{
$jaro = $this->jaro($find, $command);

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

break;
$prefixLength++;
}

return $jaro + ($prefixLength * 0.1 * (1 - $jaro));
Expand Down Expand Up @@ -203,15 +202,13 @@ private function jaro(string $find, string $command): float
$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]) {
continue;
}
if ($find[$i] !== $command[$j]) {
if ($str2Matches[$j] || $find[$i] !== $command[$j]) {
continue;
}
$str1Matches[$i] = true;
Expand All @@ -225,6 +222,7 @@ private function jaro(string $find, string $command): float
return 0.0;
}

// Count transpositions
$k = 0;
for ($i = 0; $i < $len1; $i++) {
if (false === $str1Matches[$i]) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Integrate/Console/KarnelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function itCanCallCommand()
public function itCanGetSimilarCommand()
{
$karnel = new Karnel($this->app);
$result = (fn () => $this->{'similar'}('make:view', ['view:clear', 'make:view', 'make:controller']))->call($karnel);
$result = (fn () => $this->{'getSimilarity'}('make:view', ['view:clear', 'make:view', 'make:controller']))->call($karnel);
$this->assertArrayHasKey('make:view', $result);
$this->assertArrayHasKey('make:controller', $result);
}
Expand Down

0 comments on commit 12fce9a

Please sign in to comment.