]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Metrics.hs
[FEAT] CSV ngrams extraction, engine and search.
[gargantext.git] / src / Gargantext / Text / Metrics.hs
1 {-|
2 Module : Gargantext.Text.Metrics
3 Description : All parsers of Gargantext in one file.
4 Copyright : (c) CNRS, 2017 - present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Mainly reexport functions in @Data.Text.Metrics@
11 -}
12
13 {-# LANGUAGE NoImplicitPrelude #-}
14
15
16 module Gargantext.Text.Metrics (levenshtein
17 , levenshteinNorm
18 , damerauLevenshtein
19 , damerauLevenshteinNorm
20 , overlap
21 , jaccard
22 , hamming
23 ) where
24
25
26 import Data.Text (Text)
27 import GHC.Real (Ratio)
28 import qualified Data.Text.Metrics as DTM
29
30 import Gargantext.Prelude
31 {- * Example de titre
32 -}
33
34 -- | This module provide metrics to compare Text
35 -- starting as an API rexporting main functions of the great lib
36 -- text-metrics of Mark Karpov
37
38 -- | Levenshtein Distance
39 -- In information theory, Linguistics and computer science,
40 -- the Levenshtein distance is a string metric for measuring
41 -- the difference between two sequences.
42 -- See: https://en.wikipedia.org/wiki/Levenshtein_distance
43 --
44 levenshtein :: Text -> Text -> Int
45 levenshtein = DTM.levenshtein
46
47 -- | Return normalized Levenshtein distance between two 'Text' values.
48 -- Result is a non-negative rational number (represented as @'Ratio'
49 -- 'Data.Numeric.Natural'@), where 0 signifies no similarity between the
50 -- strings, while 1 means exact match.
51 --
52 levenshteinNorm :: Text -> Text -> Ratio Int
53 levenshteinNorm = DTM.levenshteinNorm
54
55 -- | Return Damerau-Levenshtein distance between two 'Text' values. The
56 -- function works like 'levenshtein', but the collection of allowed
57 -- operations also includes transposition of two /adjacent/ characters.
58 -- See also:
59 -- <https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance>
60 --
61 damerauLevenshtein :: Text -> Text -> Int
62 damerauLevenshtein = DTM.damerauLevenshtein
63
64 -- damerau-Levenshtein distance normalized
65 --
66 damerauLevenshteinNorm :: Text -> Text -> Ratio Int
67 damerauLevenshteinNorm = DTM.damerauLevenshteinNorm
68
69 -- Treating inputs like sets
70
71 -- | Return overlap coefficient for two 'Text' values. Returned value
72 -- is in the range from 0 (no similarity) to 1 (exact match). Return 1
73 -- if both 'Text' values are empty.
74 --
75 -- See also: <https://en.wikipedia.org/wiki/Overlap_coefficient>.
76 overlap :: Text -> Text -> Ratio Int
77 overlap = DTM.overlap
78
79
80 -- | Jaccard distance
81 -- measures dissimilarity between sample sets
82 jaccard :: Text -> Text -> Ratio Int
83 jaccard = DTM.jaccard
84
85 -- | Hamming Distance
86 -- In information theory, the Hamming distance between two strings of
87 -- equal length is the number of positions at which the corresponding
88 -- symbols are different. In other words, it measures the minimum number of
89 -- substitutions required to change one string into the other
90 -- See: https://en.wikipedia.org/wiki/Hamming_distance
91
92 hamming :: Text -> Text -> Maybe Int
93 hamming = DTM.hamming
94
95