From: epriestley Date: Thu, 21 Feb 2013 18:16:02 +0000 (-0800) Subject: Use Damerau-Levenshtein to improve british spellings X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3136edf9de0cf316171e9ea027bf8fd364d7217e;p=arcanist.git Use Damerau-Levenshtein to improve british spellings 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 --- diff --git a/src/configuration/ArcanistConfiguration.php b/src/configuration/ArcanistConfiguration.php index eed355d3..0cf66ed1 100644 --- a/src/configuration/ArcanistConfiguration.php +++ b/src/configuration/ArcanistConfiguration.php @@ -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]); } } diff --git a/src/configuration/__tests__/ArcanistBritishTestCase.php b/src/configuration/__tests__/ArcanistBritishTestCase.php index 67e219e2..3fe34688 100644 --- a/src/configuration/__tests__/ArcanistBritishTestCase.php +++ b/src/configuration/__tests__/ArcanistBritishTestCase.php @@ -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) {