]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Use Damerau-Levenshtein to improve british spellings
authorepriestley <git@epriestley.com>
Thu, 21 Feb 2013 18:16:02 +0000 (10:16 -0800)
committerepriestley <git@epriestley.com>
Thu, 21 Feb 2013 18:16:02 +0000 (10:16 -0800)
Summary: Price transpositions very cheaply. We might need to edit these weights a bit, but this covers the two previous cases ("test", "alnd") and gets them right.

Test Plan: Unit tests, various `arc x` tests.

Reviewers: vrana

Reviewed By: vrana

CC: aran
Differential Revision: https://secure.phabricator.com/D5034

src/configuration/ArcanistConfiguration.php
src/configuration/__tests__/ArcanistBritishTestCase.php

index eed355d3fc7bb5496b1b4aee468480b5af2eab28..0cf66ed129c696bee79023bbebb068d6b4e7c68d 100644 (file)
@@ -196,9 +196,25 @@ class ArcanistConfiguration {
     array $options,
     $max_distance) {
 
+    // Adjust to the scaled edit costs we use below, so "2" roughly means
+    // "2 edits".
+    $max_distance = $max_distance * 3;
+
+    // These costs are somewhat made up, but the theory is that it is far more
+    // likely you will mis-strike a key ("lans" for "land") or press two keys
+    // out of order ("alnd" for "land") than omit keys or press extra keys.
+    $matrix = id(new PhutilEditDistanceMatrix())
+      ->setInsertCost(4)
+      ->setDeleteCost(4)
+      ->setReplaceCost(3)
+      ->setTransposeCost(2);
+
     $distances = array();
+    $commandv = str_split($command);
     foreach ($options as $option) {
-      $distances[$option] = levenshtein($option, $command);
+      $optionv = str_split($option);
+      $matrix->setSequences($optionv, $commandv);
+      $distances[$option] = $matrix->getEditDistance();
     }
 
     asort($distances);
@@ -218,7 +234,7 @@ class ArcanistConfiguration {
     }
 
     foreach ($distances as $option => $distance) {
-      if (strlen($option) <= 2 * $distance) {
+      if (strlen($option) < $distance) {
         unset($distances[$option]);
       }
     }
index 67e219e2d40b154df6d10f23184ec1bfb33ae0ac..3fe34688c3c25656cb8b8d0f4e96cca23219ebff 100644 (file)
@@ -4,7 +4,7 @@ final class ArcanistBritishTestCase extends ArcanistTestCase {
 
   public function testCompletion() {
     $this->assertCompletion(
-      array('land', 'amend'),
+      array('land'),
       'alnd',
       array('land', 'amend'));
 
@@ -17,6 +17,26 @@ final class ArcanistBritishTestCase extends ArcanistTestCase {
       array(),
       'test',
       array('list', 'unit'));
+
+    $this->assertCompletion(
+      array('list'),
+      'lists',
+      array('list'));
+
+    $this->assertCompletion(
+      array('diff'),
+      'dfif',
+      array('diff'));
+
+    $this->assertCompletion(
+      array('unit'),
+      'uint',
+      array('unit', 'lint', 'list'));
+
+    $this->assertCompletion(
+      array('list', 'lint'),
+      'nilt',
+      array('unit', 'lint', 'list'));
   }
 
   private function assertCompletion($expect, $input, $commands) {