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