2 Module : Gargantext.Text.Parsers.CSV
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE DeriveGeneric #-}
18 module Gargantext.Text.Parsers.CSV where
20 import GHC.Generics (Generic)
21 import GHC.IO (FilePath)
22 import Data.Either (Either(Left, Right))
23 import Data.Text (Text)
24 import Control.Applicative
25 import qualified Data.ByteString.Lazy as BL
27 import qualified Data.Vector as V
28 import Data.Text (pack)
30 import Data.Char (ord)
31 import Gargantext.Prelude
36 , publication_year :: !Int
37 , publication_month :: !Int
38 , publication_day :: !Int
42 deriving (Show, Generic)
44 instance FromNamedRecord CsvDoc where
45 parseNamedRecord r = CsvDoc <$> r .: "title"
47 <*> r .: "publication_year"
48 <*> r .: "publication_month"
49 <*> r .: "publication_day"
53 instance ToNamedRecord CsvDoc where
54 toNamedRecord (CsvDoc t s py pm pd abst aut) =
55 namedRecord [ "title" .= t
57 , "publication_year" .= py
58 , "publication_month" .= pm
59 , "publication_day" .= pd
65 csvDecodeOptions :: DecodeOptions
66 csvDecodeOptions = (defaultDecodeOptions {decDelimiter = fromIntegral $ ord '\t'} )
68 csvEncodeOptions :: EncodeOptions
69 csvEncodeOptions = (defaultEncodeOptions {encDelimiter = fromIntegral $ ord '\t'} )
72 readCsv :: FilePath -> IO (Header, V.Vector CsvDoc)
74 csvData <- BL.readFile fp
75 case decodeByNameWith csvDecodeOptions csvData of
76 Left e -> panic (pack e)
77 Right csvDocs -> pure csvDocs
79 writeCsv :: FilePath -> (Header, V.Vector CsvDoc) -> IO ()
80 writeCsv fp (h, vs) = BL.writeFile fp $
81 encodeByNameWith csvEncodeOptions h (V.toList vs)