]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Corpus/Parsers/GrandDebat.hs
Merge branch 'dev' into dev-list-charts
[gargantext.git] / src / Gargantext / Text / Corpus / Parsers / GrandDebat.hs
1 {-|
2 Module : Gargantext.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.Text.Corpus.Parsers.GrandDebat
27 where
28
29 import Data.Aeson (ToJSON, FromJSON)
30 import qualified Data.ByteString.Lazy as DBL
31 import qualified Data.JsonStream.Parser as P
32 import Data.Maybe (Maybe())
33 import Data.Text (Text)
34 import qualified Data.Text as Text
35 import GHC.Generics (Generic)
36
37 import Gargantext.Core (Lang(..))
38 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..), ToHyperdataDocument, toHyperdataDocument)
39 import Gargantext.Prelude
40 import Gargantext.Prelude.Utils
41
42
43 data GrandDebatReference = GrandDebatReference
44 { id :: !(Maybe Text)
45 , reference :: !(Maybe Text)
46 , title :: !(Maybe Text)
47
48 , createdAt :: !(Maybe Text)
49 , publishedAt :: !(Maybe Text)
50 , updatedAt :: !(Maybe Text)
51
52 , trashed :: !(Maybe Bool)
53 , trashedStatus :: !(Maybe Text)
54
55 , authorId :: !(Maybe Text)
56 , authorType :: !(Maybe Text)
57 , authorZipCode :: !(Maybe Text)
58
59 , responses :: !(Maybe [GrandDebatResponse])
60 }
61 deriving (Show, Generic)
62
63
64 data GrandDebatResponse = GrandDebatResponse
65 { questionId :: !(Maybe Text)
66 , questionTitle :: !(Maybe Text)
67 , value :: !(Maybe Text)
68 , formattedValue :: !(Maybe Text)
69 }
70 deriving (Show, Generic)
71
72 instance FromJSON GrandDebatResponse
73 instance FromJSON GrandDebatReference
74
75 instance ToJSON GrandDebatResponse
76 instance ToJSON GrandDebatReference
77
78
79 instance ToHyperdataDocument GrandDebatReference
80 where
81 toHyperdataDocument (GrandDebatReference id' _ref title'
82 _createdAt' publishedAt' _updatedAt
83 _trashed _trashedStatus
84 _authorId authorType' authorZipCode'
85 responses') =
86 HyperdataDocument (Just "GrandDebat") id'
87 Nothing Nothing Nothing Nothing
88 title' authorType' authorType' authorZipCode'
89 (toAbstract <$> responses')
90 publishedAt'
91 Nothing Nothing Nothing Nothing Nothing Nothing
92 (Just $ Text.pack $ show FR)
93 where
94 toAbstract = (Text.intercalate " . ") . ((filter (/= "")) . (map toSentence))
95 toSentence (GrandDebatResponse _id _qtitle _qvalue r) = case r of
96 Nothing -> ""
97 Just r' -> case Text.length r' > 10 of
98 True -> r'
99 False -> ""
100
101 instance ReadFile [GrandDebatReference]
102 where
103 -- | read json: 3 version below are working but with increased optimization
104 --readFile fp = maybe [] identity <$> decode <$> DBL.readFile fp
105 --readFile fp = either (panic . Text.pack) identity <$> P.eitherDecode <$> DBL.readFile fp
106 readFile' fp = P.parseLazyByteString (P.arrayOf P.value) <$> DBL.readFile fp
107
108
109