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

Lift some constraint on the monad in and use that to pass the config around...

Lift some constraint on the monad in  and use that to pass the config around as a Reader in the new profile extractor script
parent 5806cf11
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ module System.Script ...@@ -3,6 +3,7 @@ module System.Script
, try , try
, warn ) where , warn ) where
import Control.Monad.IO.Class (MonadIO(..))
import System.Exit (die) import System.Exit (die)
import System.Environment (getProgName) import System.Environment (getProgName)
import System.IO (hPutStrLn, stderr) import System.IO (hPutStrLn, stderr)
...@@ -13,8 +14,8 @@ syntax s = do ...@@ -13,8 +14,8 @@ syntax s = do
this <- getProgName this <- getProgName
die $ printf "Syntax: %s %s" this s die $ printf "Syntax: %s %s" this s
try :: IO (Either String a) -> IO a try :: MonadIO m => m (Either String a) -> m a
try = (>>= either die pure) try = (>>= either (liftIO . die) pure)
warn :: String -> IO () warn :: String -> IO ()
warn = hPutStrLn stderr warn = hPutStrLn stderr
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
import Conllu.Parse (parseConllu) import Conllu.Parse (parseConllu)
import Conllu.Type (AW, CW(..), Doc, Feat(..), ID(..), Rel(..), Sent(..)) import Conllu.Type (AW, CW(..), Doc, Feat(..), ID(..), Rel(..), Sent(..))
import Control.Applicative ((<**>)) import Control.Applicative ((<**>))
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (ReaderT, asks, runReaderT)
import Data.ByteString.Lazy as ByteString (putStr) import Data.ByteString.Lazy as ByteString (putStr)
import Data.Csv (ToNamedRecord(..), encodeWith) import Data.Csv (ToNamedRecord(..), encodeWith)
import Data.List (find, foldl', partition, sort) import Data.List (find, foldl', partition, sort)
...@@ -50,11 +52,11 @@ instance HasDefaultHeader Position where ...@@ -50,11 +52,11 @@ instance HasDefaultHeader Position where
type Measure = ArticleRecord @ Position type Measure = ArticleRecord @ Position
profile :: Config -> ArticleRecord -> IO [WithDefaultHeader Measure] profile :: ArticleRecord -> ReaderT Config IO [WithDefaultHeader Measure]
profile (Config {inputRoot}) articleRecord = profile articleRecord = do
readFile inputPath >>= either skipAndWarn analyse . parseConllu inputPath path <- asks ((</> relativePath articleRecord "conllu") . inputRoot)
liftIO $ readFile path >>= either skipAndWarn analyse . parseConllu path
where where
inputPath = inputRoot </> relativePath articleRecord "conllu"
skipAndWarn msg = [] <$ warn msg skipAndWarn msg = [] <$ warn msg
analyse = pure . fmap (glue articleRecord) . findOccurrences analyse = pure . fmap (glue articleRecord) . findOccurrences
...@@ -63,7 +65,7 @@ findOccurrences doc = ...@@ -63,7 +65,7 @@ findOccurrences doc =
Position total <$> (zip (reverse offsets) doc >>= imperativeVerb) Position total <$> (zip (reverse offsets) doc >>= imperativeVerb)
where where
(offsets, total) = foldl' next ([], 0) $ _words <$> doc (offsets, total) = foldl' next ([], 0) $ _words <$> doc
next (tmpOffsets, tmpTotal) sentence = (tmpTotal:tmpOffsets, tmpTotal + length sentence) next (tmpOffsets, partial) s = (partial:tmpOffsets, partial + length s)
getSID :: ID -> Maybe Int getSID :: ID -> Maybe Int
getSID (SID n) = Just n getSID (SID n) = Just n
...@@ -94,11 +96,9 @@ syntax = build 0 . _words ...@@ -94,11 +96,9 @@ syntax = build 0 . _words
pointingTo n cw = maybe False (n ==) (getSID . _head =<< _rel cw) pointingTo n cw = maybe False (n ==) (getSID . _head =<< _rel cw)
main :: IO () main :: IO ()
main = do main = getConfig >>= runReaderT chain
config@(Config {inputTsv}) <- getConfig
try (readNamedTsv inputTsv)
>>= searchAndDisplay config
where where
searchAndDisplay config rows = do chain = try (asks inputTsv >>= liftIO . readNamedTsv) >>= searchAndDisplay
ByteString.putStr $ encodeWith toTsv [getHeader (for :: Measure)] searchAndDisplay rows = do
mapM_ (\ar -> profile config ar >>= tsvLines) rows liftIO . ByteString.putStr $ encodeWith toTsv [getHeader (for :: Measure)]
mapM_ (\ar -> profile ar >>= liftIO . tsvLines) rows
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