Refactor line-by-line mode

This commit is contained in:
Filip Graliński 2019-02-14 16:25:28 +01:00
parent af21031172
commit b2e3293a12
2 changed files with 18 additions and 5 deletions

View File

@ -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)

View File

@ -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