2 Module : Gargantext.Text
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 #-}
18 {-# LANGUAGE OverloadedStrings #-}
20 module Gargantext.Text
23 import Data.Char (Char, isAlphaNum, isSpace)
24 import Data.Text (Text, filter, toLower, split, splitOn)
25 import qualified Data.Text as DT
26 --import Data.Text.IO (readFile)
29 import qualified Data.Set as S
30 import Data.Map.Strict (Map
32 , insertWith, unionWith
36 import qualified Data.Map.Strict as M (filter)
37 import Data.Foldable (foldl')
39 -----------------------------------------------------------------
40 import Gargantext.Text.Ngrams.Stem.En
42 import qualified Gargantext.Text.Metrics.FrequentItemSet as FIS
43 import Gargantext.Prelude hiding (filter)
44 -----------------------------------------------------------------
47 data ListName = Stop | Candidate | Graph
52 data Ngroup = Ngroup { _ngroup_label :: Ngrams
53 , _ngroup_ngrams :: [Ngrams]
57 data Ngrams = Ngrams { _ngrams_label :: [Text]
58 , _ngrams_stem :: Set Text
61 text2ngrams :: Text -> Ngrams
62 text2ngrams txt = Ngrams txt' (S.fromList $ map stem txt')
64 txt' = splitOn " " txt
66 equivNgrams :: Ngrams -> Ngrams -> Bool
67 equivNgrams (Ngrams _ s1) (Ngrams _ s2) = s1 `S.isSubsetOf` s2
68 || s2 `S.isSubsetOf` s1
74 -- Data Ngrams = Monograms | MultiGrams
76 ngrams :: Text -> [Text]
77 ngrams xs = monograms $ toLower $ filter isGram xs
80 clean txt = DT.map clean' txt
85 monograms :: Text -> [Text]
86 monograms txt = split isWord txt
88 isWord c = c `elem` [' ', '\'', ',', ';']
90 isGram :: Char -> Bool
91 isGram c = isAlphaNum c || isSpace c || c `elem` ['-','/','\'']
93 -- | Compute the occurrences (occ)
94 occ :: Ord a => [a] -> Map a Occ
95 occ xs = foldl' (\x y -> insertWith (+) y 1 x) empty xs
97 -- TODO add groups and filter stops
98 sumOcc :: Ord a => [Map a Occ] -> Map a Occ
99 sumOcc xs = foldl' (unionWith (+)) empty xs
101 --noApax :: Ord a => Map a Occ -> Map a Occ
102 --noApax m = M.filter (>1) m
104 -- | /!\ indexes are not the same:
106 -- | Index ngrams from Map
107 --indexNgram :: Ord a => Map a Occ -> Map Index a
108 --indexNgram m = fromList (zip [1..] (keys m))
110 -- | Index ngrams from Map
111 --ngramIndex :: Ord a => Map a Occ -> Map a Index
112 --ngramIndex m = fromList (zip (keys m) [1..])
114 indexWith :: Ord a => Map a Occ -> [a] -> [Int]
115 indexWith m xs = unMaybe $ map (\x -> lookupIndex x m) xs
117 indexIt :: Ord a => [[a]] -> (Map a Int, [[Int]])
120 m = sumOcc (map occ xs)
121 is = map (indexWith m) xs
123 list2fis :: Ord a => FIS.Frequency -> [[a]] -> (Map a Int, [FIS.Fis])
124 list2fis n xs = (m', fs)
127 m' = M.filter (>50000) m
130 text2fis :: FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
131 text2fis n xs = list2fis n (map ngrams xs)
133 --text2fisWith :: FIS.Size -> FIS.Frequency -> [Text] -> (Map Text Int, [FIS.Fis])
134 --text2fisWith = undefined
136 -------------------------------------------------------------------
139 sentences :: Text -> [Text]
140 sentences txt = split isStop txt
142 isStop :: Char -> Bool
143 isStop c = c `elem` ['.','?','!']
145 ---- | https://en.wikipedia.org/wiki/Text_mining
147 --testText = DT.pack "Text mining, also referred to as text data mining, roughly equivalent to text analytics, is the process of deriving high-quality information from text. High-quality information is typically derived through the devising of patterns and trends through means such as statistical pattern learning. Text mining usually involves the process of structuring the input text (usually parsing, along with the addition of some derived linguistic features and the removal of others, and subsequent insertion into a database), deriving patterns within the structured data, and finally evaluation and interpretation of the output. 'High quality' in text mining usually refers to some combination of relevance, novelty, and interestingness. Typical text mining tasks include text categorization, text clustering, concept/entity extraction, production of granular taxonomies, sentiment analysis, document summarization, and entity relation modeling (i.e., learning relations between named entities). Text analysis involves information retrieval, lexical analysis to study word frequency distributions, pattern recognition, tagging/annotation, information extraction, data mining techniques including link and association analysis, visualization, and predictive analytics. The overarching goal is, essentially, to turn text into data for analysis, via application of natural language processing (NLP) and analytical methods. A typical application is to scan a set of documents written in a natural language and either model the document set for predictive classification purposes or populate a database or search index with the information extracted."
152 ----ngramsTest :: [Text]
155 -- --txt = concat <$> lines <$> clean <$> readFile filePath
156 -- txt = clean $ testText
157 -- -- | Number of sentences
158 -- ls = sentences $ txt
159 -- -- | Number of monograms used in the full text