packages feed

text-metrics 0.3.1 → 0.3.2

raw patch · 3 files changed

+72/−77 lines, 3 filesdep ~QuickCheckdep ~basedep ~containers

Dependency ranges changed: QuickCheck, base, containers, criterion, deepseq, hspec, text, vector

Files

CHANGELOG.md view
@@ -1,3 +1,7 @@+## Text Metrics 0.3.2++* Works with `text-2.0`.+ ## Text Metrics 0.3.1  * Fixed a bug in the implementation of Jaro-Winkler distance when two
Data/Text/Metrics.hs view
@@ -43,6 +43,7 @@ import Data.Ratio import Data.Text import qualified Data.Text as T+import qualified Data.Text.Internal as T import qualified Data.Text.Unsafe as TU import qualified Data.Vector.Unboxed.Mutable as VUM import GHC.Exts (inline)@@ -83,31 +84,31 @@   | T.null a = (lenb, lenm)   | T.null b = (lena, lenm)   | otherwise = runST $ do-    let v_len = lenb + 1-    v <- VUM.unsafeNew (v_len * 2)-    let gov !i =-          when (i < v_len) $ do-            VUM.unsafeWrite v i i-            gov (i + 1)-        goi !i !na !v0 !v1 = do-          let !(TU.Iter ai da) = TU.iter a na-              goj !j !nb =-                when (j < lenb) $ do-                  let !(TU.Iter bj db) = TU.iter b nb-                      cost = if ai == bj then 0 else 1-                  x <- (+ 1) <$> VUM.unsafeRead v (v1 + j)-                  y <- (+ 1) <$> VUM.unsafeRead v (v0 + j + 1)-                  z <- (+ cost) <$> VUM.unsafeRead v (v0 + j)-                  VUM.unsafeWrite v (v1 + j + 1) (min x (min y z))-                  goj (j + 1) (nb + db)-          when (i < lena) $ do-            VUM.unsafeWrite v v1 (i + 1)-            goj 0 0-            goi (i + 1) (na + da) v1 v0-    gov 0-    goi 0 0 0 v_len-    ld <- VUM.unsafeRead v (lenb + if even lena then 0 else v_len)-    return (ld, lenm)+      let v_len = lenb + 1+      v <- VUM.unsafeNew (v_len * 2)+      let gov !i =+            when (i < v_len) $ do+              VUM.unsafeWrite v i i+              gov (i + 1)+          goi !i !na !v0 !v1 = do+            let !(TU.Iter ai da) = TU.iter a na+                goj !j !nb =+                  when (j < lenb) $ do+                    let !(TU.Iter bj db) = TU.iter b nb+                        cost = if ai == bj then 0 else 1+                    x <- (+ 1) <$> VUM.unsafeRead v (v1 + j)+                    y <- (+ 1) <$> VUM.unsafeRead v (v0 + j + 1)+                    z <- (+ cost) <$> VUM.unsafeRead v (v0 + j)+                    VUM.unsafeWrite v (v1 + j + 1) (min x (min y z))+                    goj (j + 1) (nb + db)+            when (i < lena) $ do+              VUM.unsafeWrite v v1 (i + 1)+              goj 0 0+              goi (i + 1) (na + da) v1 v0+      gov 0+      goi 0 0 0 v_len+      ld <- VUM.unsafeRead v (lenb + if even lena then 0 else v_len)+      return (ld, lenm)   where     lena = T.length a     lenb = T.length b@@ -144,36 +145,36 @@   | T.null a = (lenb, lenm)   | T.null b = (lena, lenm)   | otherwise = runST $ do-    let v_len = lenb + 1-    v <- VUM.unsafeNew (v_len * 3)-    let gov !i =-          when (i < v_len) $ do-            VUM.unsafeWrite v i i-            gov (i + 1)-        goi !i !na !ai_1 !v0 !v1 !v2 = do-          let !(TU.Iter ai da) = TU.iter a na-              goj !j !nb !bj_1 =-                when (j < lenb) $ do-                  let !(TU.Iter bj db) = TU.iter b nb-                      cost = if ai == bj then 0 else 1-                  x <- (+ 1) <$> VUM.unsafeRead v (v1 + j)-                  y <- (+ 1) <$> VUM.unsafeRead v (v0 + j + 1)-                  z <- (+ cost) <$> VUM.unsafeRead v (v0 + j)-                  let g = min x (min y z)-                  val <- (+ cost) <$> VUM.unsafeRead v (v2 + j - 1)-                  VUM.unsafeWrite v (v1 + j + 1) $-                    if i > 0 && j > 0 && ai == bj_1 && ai_1 == bj && val < g-                      then val-                      else g-                  goj (j + 1) (nb + db) bj-          when (i < lena) $ do-            VUM.unsafeWrite v v1 (i + 1)-            goj 0 0 'a'-            goi (i + 1) (na + da) ai v1 v2 v0-    gov 0-    goi 0 0 'a' 0 v_len (v_len * 2)-    ld <- VUM.unsafeRead v (lenb + (lena `mod` 3) * v_len)-    return (ld, lenm)+      let v_len = lenb + 1+      v <- VUM.unsafeNew (v_len * 3)+      let gov !i =+            when (i < v_len) $ do+              VUM.unsafeWrite v i i+              gov (i + 1)+          goi !i !na !ai_1 !v0 !v1 !v2 = do+            let !(TU.Iter ai da) = TU.iter a na+                goj !j !nb !bj_1 =+                  when (j < lenb) $ do+                    let !(TU.Iter bj db) = TU.iter b nb+                        cost = if ai == bj then 0 else 1+                    x <- (+ 1) <$> VUM.unsafeRead v (v1 + j)+                    y <- (+ 1) <$> VUM.unsafeRead v (v0 + j + 1)+                    z <- (+ cost) <$> VUM.unsafeRead v (v0 + j)+                    let g = min x (min y z)+                    val <- (+ cost) <$> VUM.unsafeRead v (v2 + j - 1)+                    VUM.unsafeWrite v (v1 + j + 1) $+                      if i > 0 && j > 0 && ai == bj_1 && ai_1 == bj && val < g+                        then val+                        else g+                    goj (j + 1) (nb + db) bj+            when (i < lena) $ do+              VUM.unsafeWrite v v1 (i + 1)+              goj 0 0 'a'+              goi (i + 1) (na + da) ai v1 v2 v0+      gov 0+      goi 0 0 'a' 0 v_len (v_len * 2)+      ld <- VUM.unsafeRead v (lenb + (lena `mod` 3) * v_len)+      return (ld, lenm)   where     lena = T.length a     lenb = T.length b@@ -246,7 +247,7 @@ -- __Heads up__, before version /0.3.0/ this function returned @'Maybe' -- 'Data.Numeric.Natural'@. hamming :: Text -> Text -> Maybe Int-hamming a b =+hamming a@(T.Text _ _ len) b =   if T.length a == T.length b     then Just (go 0 0 0)     else Nothing@@ -258,7 +259,6 @@               | na == len -> r               | cha /= chb -> go (na + da) (nb + db) (r + 1)               | otherwise -> go (na + da) (nb + db) r-    len = TU.lengthWord16 a  -- | Return the Jaro distance between two 'Text' values. Returned value is -- in the range from 0 (no similarity) to 1 (exact match).@@ -348,18 +348,9 @@  -- | Return the length of the common prefix two 'Text' values have. commonPrefix :: Text -> Text -> Int-commonPrefix a b = go 0 0 0-  where-    go !na !nb !r =-      let !(TU.Iter cha da) = TU.iter a na-          !(TU.Iter chb db) = TU.iter b nb-       in if-              | na == lena -> r-              | nb == lenb -> r-              | cha == chb -> go (na + da) (nb + db) (r + 1)-              | otherwise -> r-    lena = TU.lengthWord16 a-    lenb = TU.lengthWord16 b+commonPrefix a b = case T.commonPrefixes a b of+  Nothing -> 0+  Just (pref, _, _) -> T.length pref {-# INLINE commonPrefix #-}  ----------------------------------------------------------------------------
text-metrics.cabal view
@@ -1,11 +1,11 @@-cabal-version:   1.18+cabal-version:   2.4 name:            text-metrics-version:         0.3.1-license:         BSD3+version:         0.3.2+license:         BSD-3-Clause license-file:    LICENSE.md maintainer:      Mark Karpov <markkarpov92@gmail.com> author:          Mark Karpov <markkarpov92@gmail.com>-tested-with:     ghc ==8.8.4 ghc ==8.10.5 ghc ==9.0.1+tested-with:     ghc ==8.10.7 ghc ==9.0.1 ghc ==9.2.1 homepage:        https://github.com/mrkkrp/text-metrics bug-reports:     https://github.com/mrkkrp/text-metrics/issues synopsis:        Calculate various string metrics efficiently@@ -31,7 +31,7 @@     build-depends:         base >=4.13 && <5.0,         containers >=0.5 && <0.7,-        text >=0.2 && <1.3,+        text >=0.2 && <2.1,         vector >=0.11 && <0.13      if flag(dev)@@ -49,7 +49,7 @@         QuickCheck >=2.8 && <3.0,         base >=4.13 && <5.0,         hspec >=2.0 && <3.0,-        text >=0.2 && <1.3,+        text >=0.2 && <2.1,         text-metrics      if flag(dev)@@ -72,7 +72,7 @@         base >=4.13 && <5.0,         criterion >=0.6.2.1 && <1.6,         deepseq >=1.3 && <1.5,-        text >=0.2 && <1.3,+        text >=0.2 && <2.1,         text-metrics      if flag(dev)@@ -89,7 +89,7 @@     build-depends:         base >=4.13 && <5.0,         deepseq >=1.3 && <1.5,-        text >=0.2 && <1.3,+        text >=0.2 && <2.1,         text-metrics,         weigh >=0.0.4