Skip to content
Snippets Groups Projects
Commit 603f11a8 authored by Alice Brenon's avatar Alice Brenon
Browse files

Various adjustments: declaring a new type constructor @, cleaning imports and...

Various adjustments: declaring a new type constructor @, cleaning imports and exports, improving design of Authors/Domains metadata
parent 262cd511
No related branches found
No related tags found
No related merge requests found
...@@ -9,13 +9,11 @@ module GEODE.Metadata ...@@ -9,13 +9,11 @@ module GEODE.Metadata
, module Types , module Types
, groupBy , groupBy
, indexBy , indexBy
, list
, sortBy ) where , sortBy ) where
import Data.Foldable as Foldable (toList) import Data.Foldable as Foldable (toList)
import Data.List (sortOn) import Data.List (sortOn)
import Data.Map.Strict as Map (Map, alter, empty, insert, toList) import Data.Map.Strict as Map (Map, alter, empty, insert, toList)
import Data.Text as Text (Text, intercalate, unpack)
import GEODE.Metadata.Contrastive as Contrastive import GEODE.Metadata.Contrastive as Contrastive
import GEODE.Metadata.Entry as Entry import GEODE.Metadata.Entry as Entry
( Entry(headWord, name, page), newEntry, normalize ) ( Entry(headWord, name, page), newEntry, normalize )
...@@ -26,9 +24,6 @@ import GEODE.Metadata.TSV.Header as TSV_Header ...@@ -26,9 +24,6 @@ import GEODE.Metadata.TSV.Header as TSV_Header
( DefaultFields(..), HasDefaultHeader(..), WithDefaultHeader(..), glue ) ( DefaultFields(..), HasDefaultHeader(..), WithDefaultHeader(..), glue )
import GEODE.Metadata.Types as Types import GEODE.Metadata.Types as Types
list :: [Text] -> String
list ts = Text.unpack $ ":" <> intercalate ":" ts <> ":"
sortBy :: (Foldable t, Ord k) => (a -> k) -> t a -> [a] sortBy :: (Foldable t, Ord k) => (a -> k) -> t a -> [a]
sortBy field = sortOn field . Foldable.toList sortBy field = sortOn field . Foldable.toList
......
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
module GEODE.Metadata.Contrastive module GEODE.Metadata.Contrastive
( Authors(..) ( Contrastive(..)
, Domains(..) ) where , MultiText(..)
, formatList ) where
import Data.Csv (Field, FromField(..), Parser, ToField(..)) import Data.Csv
import Data.Text (Text, intercalate, splitOn) ( FromField(..), FromNamedRecord(..), ToNamedRecord(..), ToField(..) )
newtype Authors = Authors import Data.Text (Text, intercalate, splitOn, uncons, unsnoc)
{ getAuthors :: [Text] } deriving Show import GEODE.Metadata.TSV.Header (DefaultFields(..), HasDefaultHeader(..))
newtype Domains = Domains import GHC.Generics (Generic)
{ getDomains :: [Text] } deriving Show
sepBy :: Text -> Field -> Parser [Text] newtype MultiText = MultiText
sepBy s = fmap (splitOn s) . parseField { getList :: [Text] } deriving (Show)
instance FromField Authors where formatList :: MultiText -> Text
parseField = fmap Authors . sepBy " & " formatList = colonFormat . getList
instance ToField Authors where where
toField = toField . intercalate " & ". getAuthors colonFormat ts = ":" <> intercalate ":" ts <> ":"
instance FromField Domains where instance FromField MultiText where
parseField = fmap Domains . sepBy " | " parseField f = parseField f >>= checkAndSplit
instance ToField Domains where where
toField = toField . intercalate " | ". getDomains popBoundaries t0 = do
(firstChar,t1) <- uncons t0
(middle,lastChar) <- unsnoc t1
pure (firstChar,middle,lastChar)
checkAndSplit t =
case popBoundaries t of
Just (':',fields,':') -> pure.MultiText $ splitOn ":" fields
_ -> mempty
instance ToField MultiText where
toField = toField . formatList
data Contrastive = Contrastive
{ authors :: MultiText
, domains :: MultiText } deriving (Generic, Show)
instance FromNamedRecord Contrastive
instance ToNamedRecord Contrastive
instance HasDefaultHeader Contrastive where
defaultFields = DefaultFields [ "authors", "domains" ]
...@@ -7,8 +7,7 @@ module GEODE.Metadata.PrimaryKey ...@@ -7,8 +7,7 @@ module GEODE.Metadata.PrimaryKey
import Data.Csv (FromField(..), FromNamedRecord(..), ToNamedRecord(..), ToField(..)) import Data.Csv (FromField(..), FromNamedRecord(..), ToNamedRecord(..), ToField(..))
import GEODE.Metadata.Types (Has(..)) import GEODE.Metadata.Types (Has(..))
import GEODE.Metadata.TSV.Header import GEODE.Metadata.TSV.Header (DefaultFields(..), HasDefaultHeader(..))
(DefaultFields(..), HasDefaultHeader(..), HasDefaultHeader(..))
import GHC.Generics (Generic) import GHC.Generics (Generic)
import System.FilePath ((</>), (<.>)) import System.FilePath ((</>), (<.>))
import Text.Printf (printf) import Text.Printf (printf)
...@@ -39,7 +38,7 @@ uid a = printf "%s_%d_%d" (show $ book) tome rank ...@@ -39,7 +38,7 @@ uid a = printf "%s_%d_%d" (show $ book) tome rank
relativePath :: Has PrimaryKey a => a -> String -> FilePath relativePath :: Has PrimaryKey a => a -> String -> FilePath
relativePath a extension = relativePath a extension =
(show book) </> (show tome) </> (show rank) <.> extension (show book) </> ("T" <> show tome) </> (show rank) <.> extension
where where
PrimaryKey {book, tome, rank} = get a PrimaryKey {book, tome, rank} = get a
......
...@@ -6,19 +6,21 @@ module GEODE.Metadata.Types ...@@ -6,19 +6,21 @@ module GEODE.Metadata.Types
import Data.Csv (FromNamedRecord(..), ToNamedRecord(..)) import Data.Csv (FromNamedRecord(..), ToNamedRecord(..))
import Data.HashMap.Strict (union) import Data.HashMap.Strict (union)
infixr 9 @
infixr 9 :@:
data a @ b = a :@: b
class Has a b where class Has a b where
get :: b -> a get :: b -> a
instance Has a (a, b) where instance Has a a where
get = fst get = id
instance Has a b => Has a (b, c) where instance Has a c => Has a (b @ c) where
get = get . fst get (_ :@: c) = get c
instance {-# OVERLAPS #-} Has a (b, a) where instance {-# OVERLAPS #-} Has a (a @ b) where
get = snd get (a :@: _) = a
data a @ b = a :@: b
instance (ToNamedRecord a, ToNamedRecord b) => ToNamedRecord (a @ b) where instance (ToNamedRecord a, ToNamedRecord b) => ToNamedRecord (a @ b) where
toNamedRecord (a :@: b) = union (toNamedRecord a) (toNamedRecord b) toNamedRecord (a :@: b) = union (toNamedRecord a) (toNamedRecord b)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment