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
32 -- ^authors: FirstName1, LastName1 ; FirstName2, LastName2 ; etc.
33 -- date : default : date of last change except if the following line is encountered ^@@date: 2021-09-10
34 -- source: Name of the root node except if the following line is encountered ^@@source:
35 -- By default, 1 framawrite node = 1 document. Option for further developments: allow to give a level at generation for the split within framawrite node : :
37 -- par défaut: un doc == 1 NodeWrite
38 -- ## 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.
46 -- , "^@@authors: FirstName1, LastName1; FirstName2, LastName2"
48 , "source: someSource"
49 , "document contents 1"
50 , "document contents 2"
53 sampleUnordered :: Text
59 , "document contents 1"
61 , "authors: FirstName1, LastName1; FirstName2, LastName2"
62 , "source: someSource"
63 , "document contents 2"
66 -- parseSample = parse documentP "sample" (unpack sample)
67 -- parseSampleUnordered = parse documentP "sampleUnordered" (unpack sampleUnordered)
68 parseLinesSample :: Either ParseError Parsed
69 parseLinesSample = parseLines sample
70 parseLinesSampleUnordered :: Either ParseError Parsed
71 parseLinesSampleUnordered = parseLines sampleUnordered
74 Author { firstName :: Text
79 Parsed { title :: Text
82 , source :: Maybe Text
95 Date { year :: Integer
109 parseLines :: Text -> Either ParseError Parsed
110 parseLines text = foldl f emptyParsed <$> lst
112 lst = parse documentLines "" (unpack text)
113 f (Parsed { .. }) (LAuthors as) = Parsed { authors = as , .. }
114 f (Parsed { .. }) (LContents c) = Parsed { contents = DT.unlines [contents, c], .. }
115 f (Parsed { .. }) (LDate d ) = Parsed { date = Just d , .. }
116 f (Parsed { .. }) (LSource s ) = Parsed { source = Just s , .. }
117 f (Parsed { .. }) (LTitle t ) = Parsed { title = t , .. }
119 -- Source should be the name of the node
120 -- First line of each Context should be the title.
121 documentLinesP :: Parser [Line]
124 ls <- lineP `sepBy` newline
125 pure $ [LTitle $ pack t] ++ ls
127 documentLines :: Parser [Line]
129 ls <- lineP `sepBy` newline
134 choice [ try authorsLineP
140 authorsLineP :: Parser Line
143 pure $ LAuthors authors
145 dateLineP :: Parser Line
150 sourceLineP :: Parser Line
153 pure $ LSource $ pack source
155 contentsLineP :: Parser Line
157 contents <- many (noneOf "\n")
158 pure $ LContents $ pack contents
164 -- a <- optionMaybe authorsP
165 -- d <- optionMaybe dateP
166 -- s <- optionMaybe sourceP
168 -- pure $ Parsed { title = pack t
169 -- , authors = fromMaybe [] a
170 -- , date = pack <$> d
171 -- , source = pack <$> s
172 -- , contents = pack c }
174 titleDelimiterP :: Parser ()
177 -- _ <- try (string "==")
180 titleP :: Parser [Char]
181 titleP = manyTill anyChar (try titleDelimiterP)
183 authorsPrefixP :: Parser [Char]
185 _ <- string "authors:"
187 authorsP :: Parser [Author]
188 authorsP = try authorsPrefixP *> sepBy authorP (char ';')
189 authorP :: Parser Author
191 fn <- manyTill anyChar (char ',')
193 --ln <- manyTill anyChar (void (char ';') <|> tokenEnd)
194 --ln <- manyTill anyChar (tokenEnd)
195 ln <- many (noneOf "\n")
196 pure $ Author { firstName = pack fn, lastName = pack ln }
197 -- manyTill anyChar (void (char '\n') <|> eof)
199 datePrefixP :: Parser [Char]
204 dateP = try datePrefixP
206 -- *> many (noneOf "\n")
208 dateISOP :: Parser Date
210 year <- rd <$> number
212 month <- rd <$> number
215 _ <- many (noneOf "\n" )
216 pure $ Date { year, month, day }
218 rd = read :: [Char] -> Integer
221 sourcePrefixP :: Parser [Char]
223 _ <- string "source:"
225 sourceP :: Parser [Char]
226 sourceP = try sourcePrefixP
227 *> many (noneOf "\n")
229 -- contentsP :: Parser String
230 -- contentsP = many anyChar
232 tokenEnd :: Parser ()
233 tokenEnd = void (char '\n') <|> eof
236 text2titleParagraphs :: Int -> Text -> [(Text, Text)]
237 text2titleParagraphs n = catMaybes
241 . DT.intercalate " " -- ". "
242 . List.filter (/= "")
246 doTitle :: [Text] -> Maybe (Text, Text)
247 doTitle (t:ts) = Just (t, DT.concat ts)
251 clean :: Text -> Text
252 clean = DT.unwords . List.filter (\w -> DT.length w < 25) . DT.words