From b2e3293a1243a334b4ed41a2357516fab538474d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Grali=C5=84ski?= Date: Thu, 14 Feb 2019 16:25:28 +0100 Subject: [PATCH] Refactor line-by-line mode --- src/GEval/Core.hs | 17 +++++++++++++++-- src/GEval/LineByLine.hs | 6 +++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/GEval/Core.hs b/src/GEval/Core.hs index 9fc0a55..0451514 100644 --- a/src/GEval/Core.hs +++ b/src/GEval/Core.hs @@ -39,7 +39,8 @@ module GEval.Core checkMultipleOuts, checkMultipleOutsCore, gesMainMetric, - gesPreprocess + gesPreprocess, + threeLineSource ) where import Data.Conduit @@ -898,7 +899,11 @@ instance (MonadUnliftIO m, MonadIO m, MonadThrow m) => EvaluationContext (WithIn checkStepM _ (_, WrappedParsedRecordWithInput Done Done Done) = return Nothing - +threeLineSource :: (MonadUnliftIO m, MonadIO m, MonadThrow m) => WithInput m Text Text Text -> ConduitT () (WrappedParsedRecord (WithInput m Text Text Text)) (ResourceT m) () +threeLineSource (WithInput inputLineSource expectedLineSource outLineSource) = getZipSource $ (\x (y,z) -> WrappedParsedRecordWithInput x y z) + <$> ZipSource (linesAsItems inputLineSource) <*> (ZipSource $ getZipSource $ (,) + <$> ZipSource (linesAsItems expectedLineSource) + <*> ZipSource (linesAsItems outLineSource)) averageC :: MonadResource m => ConduitT Double Void m Double averageC = getZipSink @@ -906,12 +911,20 @@ averageC = getZipSink <$> ZipSink CC.sum <*> ZipSink CC.length +-- | Takes a source of lines and returns a source of lines and returns a conduit of +-- items (using a given preprocessor and parser). items :: MonadResource m => LineSource m -> (Text -> Either String a) -> ConduitT () (SourceItem a) m () items (LineSource lineSource preprocess _ _) parser = (lineSource .| CL.map (toItem . parser . preprocess)) >> yield Done where toItem (Right x) = Got x toItem (Left m) = Wrong m +-- | Takes a source of lines and returns a conduit of lines represented as +-- items (without preprocessing and parsing!) to be used in line-by-line modes. +linesAsItems :: MonadResource m => LineSource m -> ConduitT () (SourceItem Text) m () +linesAsItems (LineSource lineSource _ _ _) = + (lineSource .| CL.map Got) >> yield Done + itemAbsoluteError :: (Double, Double) -> Double itemAbsoluteError (exp, out) = abs (exp-out) diff --git a/src/GEval/LineByLine.hs b/src/GEval/LineByLine.hs index e63419e..54172a0 100644 --- a/src/GEval/LineByLine.hs +++ b/src/GEval/LineByLine.hs @@ -417,9 +417,9 @@ gevalLineByLineSource :: Metric -> (Text -> Text) -> SourceSpec -> SourceSpec -> gevalLineByLineSource metric preprocess inputSource expectedSource outSource = (getZipSource $ (,) <$> ZipSource (CL.sourceList [1..]) - <*> (ZipSource $ recordSource context parserSpec)) .| CL.mapM (checkStepM evaluateLine) .| CL.catMaybes - where parserSpec = (ParserSpecWithInput (Right . id) (Right . id) (Right . id)) - context = (WithInput inputLineSource expectedLineSource outputLineSource) + <*> (ZipSource $ threeLineSource context)) .| CL.mapM (checkStepM evaluateLine) .| CL.catMaybes + where context = (WithInput inputLineSource expectedLineSource outputLineSource) + -- preparing sources, `id` means that no preprocessing is done (to avoid double preprocessing) inputLineSource = fileAsLineSource inputSource id expectedLineSource = fileAsLineSource expectedSource id outputLineSource = fileAsLineSource outSource id