2 Module : Gargantext.Core.Text.Corpus.Parsers.FrameWrite
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 module Gargantext.Core.Text.Corpus.Parsers.FrameWrite
15 import Control.Applicative ((*>))
16 import Control.Monad (void)
19 import Data.Text hiding (foldl)
20 import Gargantext.Core.Text (sentences)
21 import Gargantext.Prelude
22 import Prelude ((++), read)
23 import Text.Parsec hiding (Line)
24 import Text.Parsec.String
25 import qualified Data.Text as DT
26 import qualified Data.List as List
29 -- https://gitlab.iscpif.fr/gargantext/purescript-gargantext/issues/331
31 -- Authors : default : anonymous ; except if the following line is encountered ^@@authors: FirstName1, LastName1 ; FirstName2, LastName2 ; etc.
32 -- date : default : date of last change except if the following line is encountered ^@@date: 2021-09-10
33 -- source: Name of the root node except if the following line is encountered ^@@source:
34 -- By default, 1 framawrite node = 1 document. Option for further developments: allow to give a level at generation for the split within framawrite node : :
36 -- par défaut: un doc == 1 NodeWrite
37 -- ## mean each ## section will be a new document with title the subsubsection title. Either it features options for author, date etc. or it will inherit the document's option.
45 -- , "^@@authors: FirstName1, LastName1; FirstName2, LastName2"
47 , "source: someSource"
48 , "document contents 1"
49 , "document contents 2"
52 sampleUnordered :: Text
58 , "document contents 1"
60 , "authors: FirstName1, LastName1; FirstName2, LastName2"
61 , "source: someSource"
62 , "document contents 2"
65 -- parseSample = parse documentP "sample" (unpack sample)
66 -- parseSampleUnordered = parse documentP "sampleUnordered" (unpack sampleUnordered)
67 parseLinesSample :: Either ParseError Parsed
68 parseLinesSample = parseLines sample
69 parseLinesSampleUnordered :: Either ParseError Parsed
70 parseLinesSampleUnordered = parseLines sampleUnordered
73 Author { firstName :: Text
78 Parsed { title :: Text
81 , source :: Maybe Text
94 Date { year :: Integer
107 parseLines :: Text -> Either ParseError Parsed
108 parseLines text = foldl f emptyParsed <$> lst
110 lst = parse documentLinesP "" (unpack text)
111 f (Parsed { .. }) (LAuthors as) = Parsed { authors = as, .. }
112 f (Parsed { .. }) (LContents c) = Parsed { contents = concat [contents, c], .. }
113 f (Parsed { .. }) (LDate d ) = Parsed { date = Just d, .. }
114 f (Parsed { .. }) (LSource s ) = Parsed { source = Just s, .. }
115 f (Parsed { .. }) (LTitle t ) = Parsed { title = t, .. }
117 documentLinesP :: Parser [Line]
120 ls <- lineP `sepBy` newline
121 pure $ [LTitle $ pack t] ++ ls
125 choice [ try authorsLineP
130 authorsLineP :: Parser Line
133 pure $ LAuthors authors
135 dateLineP :: Parser Line
140 sourceLineP :: Parser Line
143 pure $ LSource $ pack source
145 contentsLineP :: Parser Line
147 contents <- many (noneOf "\n")
148 pure $ LContents $ pack contents
154 -- a <- optionMaybe authorsP
155 -- d <- optionMaybe dateP
156 -- s <- optionMaybe sourceP
158 -- pure $ Parsed { title = pack t
159 -- , authors = fromMaybe [] a
160 -- , date = pack <$> d
161 -- , source = pack <$> s
162 -- , contents = pack c }
164 titleDelimiterP :: Parser ()
167 -- _ <- try (string "==")
169 titleP :: Parser [Char]
170 titleP = manyTill anyChar (try titleDelimiterP)
172 authorsPrefixP :: Parser [Char]
174 _ <- string "authors:"
176 authorsP :: Parser [Author]
177 authorsP = try authorsPrefixP *> sepBy authorP (char ';')
178 authorP :: Parser Author
180 fn <- manyTill anyChar (char ',')
182 --ln <- manyTill anyChar (void (char ';') <|> tokenEnd)
183 --ln <- manyTill anyChar (tokenEnd)
184 ln <- many (noneOf "\n")
185 pure $ Author { firstName = pack fn, lastName = pack ln }
186 -- manyTill anyChar (void (char '\n') <|> eof)
188 datePrefixP :: Parser [Char]
193 dateP = try datePrefixP
195 -- *> many (noneOf "\n")
197 dateISOP :: Parser Date
199 year <- rd <$> number
201 month <- rd <$> number
204 _ <- many (noneOf "\n" )
205 pure $ Date { year, month, day }
207 rd = read :: [Char] -> Integer
210 sourcePrefixP :: Parser [Char]
212 _ <- string "source:"
214 sourceP :: Parser [Char]
215 sourceP = try sourcePrefixP
216 *> many (noneOf "\n")
218 -- contentsP :: Parser String
219 -- contentsP = many anyChar
221 tokenEnd :: Parser ()
222 tokenEnd = void (char '\n') <|> eof
226 text2paragraphs :: Int -> Text -> [Text]
227 text2paragraphs n = List.map DT.concat
228 . splitEvery n . List.map clean
229 . sentences . DT.concat . DT.lines
231 clean :: Text -> Text
232 clean = DT.unwords . List.filter (\w -> DT.length w < 25) . DT.words