2 Module : Gargantext.Ngrams
3 Description : Ngrams tools
4 Copyright : (c) CNRS, 2018
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 Definitions of ngrams.
13 n non negative integer
17 module Gargantext.Ngrams ( module Gargantext.Ngrams.Letters
18 --, module Gargantext.Ngrams.Hetero
19 , module Gargantext.Ngrams.CoreNLP
20 , module Gargantext.Ngrams.Parser
21 , module Gargantext.Ngrams.Occurrences
22 , module Gargantext.Ngrams.TextMining
23 , module Gargantext.Ngrams.Metrics
24 , Ngrams(..), ngrams, occ, sumOcc, text2fis
25 , ListName(..), equivNgrams, isGram
26 --, module Gargantext.Ngrams.Words
29 import Gargantext.Ngrams.Letters
30 --import Gargantext.Ngrams.Hetero
31 import Gargantext.Ngrams.CoreNLP
32 import Gargantext.Ngrams.Parser
34 import Gargantext.Ngrams.Occurrences
35 import Gargantext.Ngrams.TextMining
36 --import Gargantext.Ngrams.Words
38 import Gargantext.Ngrams.Metrics
39 import qualified Gargantext.Ngrams.FrequentItemSet as FIS
40 -----------------------------------------------------------------
42 import Data.List (sort)
43 import Data.Char (Char, isAlphaNum, isSpace)
44 import Data.Text (Text, words, filter, toLower)
45 import Data.Map.Strict (Map
47 , insertWith, unionWith
51 import qualified Data.Map.Strict as M (filter)
52 import Data.Foldable (foldl')
53 import Gargantext.Prelude hiding (filter)
55 -- Maybe useful later:
56 --import NLP.Stemmer (stem, Stemmer(..))
57 --import Language.Aspell (check, suggest, spellChecker, spellCheckerWithOptions)
58 --import Language.Aspell.Options (ACOption(..))
61 data ListName = Stop | Candidate | Graph
64 data Ngrams = Ngrams { _ngramsNgrams :: [Text]
65 , _ngramsStem :: [Text]
66 , _ngramsListName :: Maybe ListName
69 equivNgrams :: Ngrams -> Ngrams -> Bool
70 equivNgrams (Ngrams n1 s1 _) (Ngrams n2 s2 _)
71 = (sort n1) == (sort n2) || (sort s1) == (sort s2)
76 -- Data Ngrams = Monograms | MultiGrams
78 ngrams :: Text -> [Text]
79 ngrams xs = monograms $ toLower $ filter isGram xs
81 monograms :: Text -> [Text]
84 isGram :: Char -> Bool
85 isGram c = isAlphaNum c || isSpace c || c `elem` ['-','/']
87 -- | Compute the occurrences (occ)
88 occ :: Ord a => [a] -> Map a Occ
89 occ xs = foldl' (\x y -> insertWith (+) y 1 x) empty xs
91 -- TODO add groups and filter stops
92 sumOcc :: Ord a => [Map a Occ] -> Map a Occ
93 sumOcc xs = foldl' (unionWith (+)) empty xs
95 --noApax :: Ord a => Map a Occ -> Map a Occ
96 --noApax m = M.filter (>1) m
98 -- | /!\ indexes are not the same:
100 -- | Index ngrams from Map
101 --indexNgram :: Ord a => Map a Occ -> Map Index a
102 --indexNgram m = fromList (zip [1..] (keys m))
104 -- | Index ngrams from Map
105 --ngramIndex :: Ord a => Map a Occ -> Map a Index
106 --ngramIndex m = fromList (zip (keys m) [1..])
108 indexWith :: Ord a => Map a Occ -> [a] -> [Int]
109 indexWith m xs = unMaybe $ map (\x -> lookupIndex x m) xs
111 indexIt :: Ord a => [[a]] -> (Map a Int, [[Int]])
114 m = sumOcc (map occ xs)
115 is = map (indexWith m) xs
117 list2fis :: Ord a => FIS.Frequency -> [[a]] -> (Map a Int, [FIS.Fis])
118 list2fis n xs = (m', fs)
121 m' = M.filter (>50000) m
124 text2fis :: FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
125 text2fis n xs = list2fis n (map ngrams xs)
127 --text2fisWith :: FIS.Size -> FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
128 --text2fisWith = undefined