2 Module : Gargantext.Text.List.CSV
4 Copyright : (c) CNRS, 2018-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 CSV parser for Gargantext corpus files.
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE DeriveGeneric #-}
18 module Gargantext.Text.List.CSV where
20 import GHC.IO (FilePath)
22 import Control.Applicative
23 import Control.Monad (mzero)
25 import Data.Char (ord)
27 import Data.Either (Either(Left, Right))
28 import Data.Text (Text, pack)
29 import qualified Data.ByteString.Lazy as BL
31 import Data.Vector (Vector)
32 import qualified Data.Vector as V
34 import Gargantext.Prelude hiding (length)
35 -- import Gargantext.Text.List.Types
36 ------------------------------------------------------------------------
38 --csv2lists :: Vector CsvList -> Lists
39 --csv2lists v = V.foldl' (\e (CsvList listType label forms) -> insertLists lt label forms e) emptyLists v
41 ------------------------------------------------------------------------
42 data CsvListType = CsvMap | CsvStop | CsvCandidate
43 deriving (Read, Show, Eq)
44 ------------------------------------------------------------------------
45 -- CSV List Main Configuration
46 csvListFieldDelimiter :: Char
47 csvListFieldDelimiter = '\t'
49 csvListFormsDelimiter :: Text
50 csvListFormsDelimiter = "|&|"
51 ------------------------------------------------------------------------
52 data CsvList = CsvList
53 { csvList_status :: !CsvListType
54 , csvList_label :: !Text
55 , csvList_forms :: !Text
58 ------------------------------------------------------------------------
59 instance FromNamedRecord CsvList where
60 parseNamedRecord r = CsvList <$> r .: "status"
64 instance ToNamedRecord CsvList where
65 toNamedRecord (CsvList s l f) =
66 namedRecord [ "status" .= s
70 ------------------------------------------------------------------------
71 instance FromField CsvListType where
72 parseField "map" = pure CsvMap
73 parseField "main" = pure CsvCandidate
74 parseField "stop" = pure CsvStop
77 instance ToField CsvListType where
78 toField CsvMap = "map"
79 toField CsvCandidate = "main"
80 toField CsvStop = "stop"
81 ------------------------------------------------------------------------
82 csvDecodeOptions :: DecodeOptions
83 csvDecodeOptions = (defaultDecodeOptions
84 {decDelimiter = fromIntegral $ ord csvListFieldDelimiter}
87 csvEncodeOptions :: EncodeOptions
88 csvEncodeOptions = ( defaultEncodeOptions
89 {encDelimiter = fromIntegral $ ord csvListFieldDelimiter}
91 ------------------------------------------------------------------------
92 fromCsvListFile :: FilePath -> IO (Header, Vector CsvList)
93 fromCsvListFile fp = do
94 csvData <- BL.readFile fp
95 case decodeByNameWith csvDecodeOptions csvData of
96 Left e -> panic (pack e)
97 Right csvList -> pure csvList
98 ------------------------------------------------------------------------
99 toCsvListFile :: FilePath -> (Header, Vector CsvList) -> IO ()
100 toCsvListFile fp (h, vs) = BL.writeFile fp $
101 encodeByNameWith csvEncodeOptions h (V.toList vs)
102 ------------------------------------------------------------------------