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