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 {-# LANGUAGE NoImplicitPrelude #-}
19 module Gargantext.Ngrams ( module Gargantext.Ngrams.Letters
20 --, module Gargantext.Ngrams.Hetero
21 , module Gargantext.Ngrams.CoreNLP
22 , module Gargantext.Ngrams.Parser
23 , module Gargantext.Ngrams.Occurrences
24 , module Gargantext.Ngrams.TextMining
25 , module Gargantext.Ngrams.Metrics
26 , Ngrams(..), ngrams, occ, sumOcc, text2fis
27 , ListName(..), equivNgrams, isGram
28 --, module Gargantext.Ngrams.Words
31 import Gargantext.Ngrams.Letters
32 --import Gargantext.Ngrams.Hetero
33 import Gargantext.Ngrams.CoreNLP
34 import Gargantext.Ngrams.Parser
36 import Gargantext.Ngrams.Occurrences
37 import Gargantext.Ngrams.TextMining
38 --import Gargantext.Ngrams.Words
40 import Gargantext.Ngrams.Metrics
41 import qualified Gargantext.Ngrams.FrequentItemSet as FIS
42 -----------------------------------------------------------------
44 import Data.List (sort)
45 import Data.Char (Char, isAlphaNum, isSpace)
46 import Data.Text (Text, words, filter, toLower)
47 import Data.Map.Strict (Map
49 , insertWith, unionWith
53 import qualified Data.Map.Strict as M (filter)
54 import Data.Foldable (foldl')
55 import Gargantext.Prelude hiding (filter)
57 -- Maybe useful later:
58 --import NLP.Stemmer (stem, Stemmer(..))
59 --import Language.Aspell (check, suggest, spellChecker, spellCheckerWithOptions)
60 --import Language.Aspell.Options (ACOption(..))
63 data ListName = Stop | Candidate | Graph
66 data Ngrams = Ngrams { _ngramsNgrams :: [Text]
67 , _ngramsStem :: [Text]
68 , _ngramsListName :: Maybe ListName
71 equivNgrams :: Ngrams -> Ngrams -> Bool
72 equivNgrams (Ngrams n1 s1 _) (Ngrams n2 s2 _)
73 = (sort n1) == (sort n2) || (sort s1) == (sort s2)
78 -- Data Ngrams = Monograms | MultiGrams
80 ngrams :: Text -> [Text]
81 ngrams xs = monograms $ toLower $ filter isGram xs
83 monograms :: Text -> [Text]
86 isGram :: Char -> Bool
87 isGram c = isAlphaNum c || isSpace c || c `elem` ['-','/']
89 -- | Compute the occurrences (occ)
90 occ :: Ord a => [a] -> Map a Occ
91 occ xs = foldl' (\x y -> insertWith (+) y 1 x) empty xs
93 -- TODO add groups and filter stops
94 sumOcc :: Ord a => [Map a Occ] -> Map a Occ
95 sumOcc xs = foldl' (unionWith (+)) empty xs
97 --noApax :: Ord a => Map a Occ -> Map a Occ
98 --noApax m = M.filter (>1) m
100 -- | /!\ indexes are not the same:
102 -- | Index ngrams from Map
103 --indexNgram :: Ord a => Map a Occ -> Map Index a
104 --indexNgram m = fromList (zip [1..] (keys m))
106 -- | Index ngrams from Map
107 --ngramIndex :: Ord a => Map a Occ -> Map a Index
108 --ngramIndex m = fromList (zip (keys m) [1..])
110 indexWith :: Ord a => Map a Occ -> [a] -> [Int]
111 indexWith m xs = unMaybe $ map (\x -> lookupIndex x m) xs
113 indexIt :: Ord a => [[a]] -> (Map a Int, [[Int]])
116 m = sumOcc (map occ xs)
117 is = map (indexWith m) xs
119 list2fis :: Ord a => FIS.Frequency -> [[a]] -> (Map a Int, [FIS.Fis])
120 list2fis n xs = (m', fs)
123 m' = M.filter (>50000) m
126 text2fis :: FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
127 text2fis n xs = list2fis n (map ngrams xs)
129 --text2fisWith :: FIS.Size -> FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
130 --text2fisWith = undefined