]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Text/Corpus/Parsers/GrandDebat.hs
[framewrite] better line parsing
[gargantext.git] / src / Gargantext / Core / Text / Corpus / Parsers / GrandDebat.hs
1 {-|
2 Module : Gargantext.Core.Text.Corpus.Parsers.GrandDebat
3 Description : Grand Debat Types
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 _flowCorpusDebat :: FlowCmdM env err m
11 => User -> Either CorpusName [CorpusId]
12 -> Limit -> FilePath
13 -> m CorpusId
14 _flowCorpusDebat u n l fp = do
15 docs <- liftBase ( splitEvery 500
16 <$> take l
17 <$> readFile' fp
18 :: IO [[GD.GrandDebatReference ]]
19 )
20 flowCorpus u n (Multi FR) (map (map toHyperdataDocument) docs)
21
22
23 -}
24
25
26 module Gargantext.Core.Text.Corpus.Parsers.GrandDebat
27 where
28
29 import Data.Aeson (ToJSON, FromJSON)
30 import Data.Text (Text)
31 import GHC.Generics (Generic)
32 import Gargantext.Core (Lang(..))
33 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..), ToHyperdataDocument, toHyperdataDocument)
34 import Gargantext.Prelude
35 import Gargantext.Database.GargDB
36 import qualified Data.ByteString.Lazy as DBL
37 import qualified Data.JsonStream.Parser as P
38 import qualified Data.Text as Text
39
40 data GrandDebatReference = GrandDebatReference
41 { id :: !(Maybe Text)
42 , reference :: !(Maybe Text)
43 , title :: !(Maybe Text)
44
45 , createdAt :: !(Maybe Text)
46 , publishedAt :: !(Maybe Text)
47 , updatedAt :: !(Maybe Text)
48
49 , trashed :: !(Maybe Bool)
50 , trashedStatus :: !(Maybe Text)
51
52 , authorId :: !(Maybe Text)
53 , authorType :: !(Maybe Text)
54 , authorZipCode :: !(Maybe Text)
55
56 , responses :: !(Maybe [GrandDebatResponse])
57 }
58 deriving (Show, Generic)
59
60
61 data GrandDebatResponse = GrandDebatResponse
62 { questionId :: !(Maybe Text)
63 , questionTitle :: !(Maybe Text)
64 , value :: !(Maybe Text)
65 , formattedValue :: !(Maybe Text)
66 }
67 deriving (Show, Generic)
68
69 instance FromJSON GrandDebatResponse
70 instance FromJSON GrandDebatReference
71
72 instance ToJSON GrandDebatResponse
73 instance ToJSON GrandDebatReference
74
75
76 instance ToHyperdataDocument GrandDebatReference
77 where
78 toHyperdataDocument (GrandDebatReference id' _ref title'
79 _createdAt' publishedAt' _updatedAt
80 _trashed _trashedStatus
81 _authorId authorType' authorZipCode'
82 responses') =
83 HyperdataDocument (Just "GrandDebat") id'
84 Nothing Nothing Nothing Nothing
85 title' authorType' authorType' authorZipCode'
86 (toAbstract <$> responses')
87 publishedAt'
88 Nothing Nothing Nothing Nothing Nothing Nothing
89 (Just $ Text.pack $ show FR)
90 where
91 toAbstract = (Text.intercalate " . ") . ((filter (/= "")) . (map toSentence))
92 toSentence (GrandDebatResponse _id _qtitle _qvalue r) = case r of
93 Nothing -> ""
94 Just r' -> case Text.length r' > 10 of
95 True -> r'
96 False -> ""
97
98 instance ReadFile [GrandDebatReference]
99 where
100 -- | read json: 3 version below are working but with increased optimization
101 --readFile fp = maybe [] identity <$> decode <$> DBL.readFile fp
102 --readFile fp = either (panic . Text.pack) identity <$> P.eitherDecode <$> DBL.readFile fp
103 readFile' fp = P.parseLazyByteString (P.arrayOf P.value) <$> DBL.readFile fp
104
105
106