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