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