]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Ngrams/Occurrences.hs
[REST] Document View route.
[gargantext.git] / src / Gargantext / Ngrams / Occurrences.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Gargantext.Ngrams.Occurrences where
4
5 import Gargantext.Prelude
6
7 import Control.Monad ((>>),(>>=))
8 import Data.String (String())
9 import Data.Attoparsec.Text
10 import Data.Text (Text)
11
12 import Data.Either.Extra(Either(..))
13 import qualified Data.Text as T
14 import Control.Applicative
15
16
17 occurrenceParser :: Text -> Parser Bool
18 occurrenceParser txt = manyTill anyChar (string txt) >> pure True
19
20 occurrencesParser :: Text -> Parser Int
21 occurrencesParser txt = case txt of
22 "" -> pure 0
23 _ -> many (occurrenceParser txt') >>= \matches -> pure (length matches)
24 where
25 txt' = T.toLower txt
26
27 parseOccurrences :: Text -> Text -> Either String Int
28 parseOccurrences x = parseOnly (occurrencesParser x)