Skip to content

Commit 194493f

Browse files
committed
Use edit-distannce instead of handrolling
1 parent 2e284c5 commit 194493f

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ library
6060
, Diff ^>=0.5 || ^>=1.0.0
6161
, directory
6262
, dlist
63+
, edit-distance
6364
, enummapset
6465
, exceptions
6566
, extra >=1.7.14
@@ -83,7 +84,6 @@ library
8384
, list-t
8485
, lsp ^>=2.7
8586
, lsp-types ^>=2.3
86-
, MemoTrie
8787
, mtl
8888
, opentelemetry >=0.6.1
8989
, optparse-applicative
Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
module Text.Fuzzy.Levenshtein where
22

3-
import Data.Function (fix)
43
import Data.List (sortOn)
5-
import Data.MemoTrie
4+
import Data.Text (Text)
65
import qualified Data.Text as T
7-
import qualified Data.Text.Array as T
8-
import Data.Text.Internal (Text (..))
6+
import Text.EditDistance
97
import Text.Fuzzy.Parallel
108

11-
-- | Same caveats apply w.r.t. ASCII as in 'Text.Fuzzy.Parallel'.
12-
-- Might be worth optimizing this at some point, but it's good enoughᵗᵐ for now
13-
levenshtein :: Text -> Text -> Int
14-
levenshtein a b | T.null a = T.length b
15-
levenshtein a b | T.null b = T.length a
16-
levenshtein (Text aBuf aOff aLen) (Text bBuf bOff bLen) = do
17-
let aTot = aOff + aLen
18-
bTot = bOff + bLen
19-
go' _ (!aIx, !bIx) | aIx >= aTot || bIx >= bTot = max (aTot - aIx) (bTot - bIx)
20-
go' f (!aIx, !bIx) | T.unsafeIndex aBuf aIx == T.unsafeIndex bBuf bIx = f (aIx + 1, bIx + 1)
21-
go' f (!aIx, !bIx) =
22-
minimum
23-
[ 2 + f (aIx + 1, bIx + 1), -- Give substitutions a heavier cost, so multiple typos cost more
24-
1 + f (aIx + 1, bIx),
25-
1 + f (aIx, bIx + 1)
26-
]
27-
go = fix (memo . go')
28-
go (aOff, bOff)
29-
309
-- | Sort the given list according to it's levenshtein distance relative to the
3110
-- given string.
3211
levenshteinScored :: Int -> Text -> [Text] -> [Scored Text]
33-
levenshteinScored chunkSize needle haystack =
12+
levenshteinScored chunkSize needle haystack = do
13+
let levenshtein = levenshteinDistance $ defaultEditCosts {substitutionCosts=ConstantCost 2}
3414
sortOn score $
3515
matchPar chunkSize needle haystack id $
36-
\a b -> Just $ levenshtein a b
16+
\a b -> Just $ levenshtein (T.unpack a) (T.unpack b)

0 commit comments

Comments
 (0)