fully handle multiple outputs

This commit is contained in:
Filip Graliński 2018-06-28 16:22:22 +02:00
parent ba26cdb9e0
commit 338ddb7fbf
3 changed files with 31 additions and 7 deletions

View File

@ -13,6 +13,10 @@ import System.Exit
import Data.Conduit.SmartSource
import System.FilePath
import Data.List (intercalate)
main :: IO ()
main = do
args <- getArgs
@ -24,7 +28,21 @@ main = do
showTheResult :: GEvalOptions -> [(SourceSpec, [MetricValue])] -> IO ()
showTheResult opts [(_, vals)] = showTheResult' opts vals
showTheResult _ _ = error "multiple outputs not handled yet"
showTheResult opts [] = error "no output given"
showTheResult opts multipleResults = showTable opts multipleResults
showTable :: GEvalOptions -> [(SourceSpec, [MetricValue])] -> IO ()
showTable opts multipleResults = do
case metrics of
[singleMetric] -> return ()
[] -> error "no metric given"
metrics -> putStrLn $ intercalate "\t" ("File name" : map show metrics)
mapM_ (\entry -> putStrLn $ formatTableEntry opts entry) multipleResults
where metrics = gesMetrics $ geoSpec opts
formatTableEntry :: GEvalOptions -> (SourceSpec, [MetricValue]) -> String
formatTableEntry opts (sourceSpec, metrics) = intercalate "\t" (formatSourceSpec sourceSpec : vals)
where vals = map (formatTheResult (gesPrecision $ geoSpec opts)) metrics
showTheResult' :: GEvalOptions -> [MetricValue] -> IO ()
-- do not show the metric if just one was given
@ -34,6 +52,10 @@ showTheResult' opts [] = do
exitFailure
showTheResult' opts vals = mapM_ putStrLn $ map (formatTheMetricAndResult (gesPrecision $ geoSpec opts)) $ zip (gesMetrics $ geoSpec opts) vals
formatSourceSpec :: SourceSpec -> String
formatSourceSpec (FilePathSpec fp) = dropExtensions $ takeFileName fp
formatSourceSpec spec = show spec
formatTheMetricAndResult :: Maybe Int -> (Metric, MetricValue) -> String
formatTheMetricAndResult mPrecision (metric, val) = (show metric) ++ "\t" ++ (formatTheResult mPrecision val)

View File

@ -71,6 +71,7 @@ executable geval
, geval
, optparse-applicative
, fgl
, filepath
default-language: Haskell2010
test-suite geval-test

View File

@ -276,11 +276,12 @@ data LineSource m = LineSource (Source m Text) SourceSpec Word32
geval :: GEvalSpecification -> IO [(SourceSpec, [MetricValue])]
geval gevalSpec = do
(inputSource, expectedSource, outSources) <- checkAndGetFiles False gevalSpec
case outSources of
[outSource] -> do
results <- Prelude.mapM (\metric -> gevalCore metric inputSource expectedSource outSource) metrics
return [(outSource, results)]
_ -> error $ "multiple outputs not handled yet"
Prelude.mapM (gevalOnSingleOut gevalSpec inputSource expectedSource) outSources
gevalOnSingleOut :: GEvalSpecification -> SourceSpec -> SourceSpec -> SourceSpec -> IO (SourceSpec, [MetricValue])
gevalOnSingleOut gevalSpec inputSource expectedSource outSource = do
vals <- Prelude.mapM (\metric -> gevalCore metric inputSource expectedSource outSource) metrics
return (outSource, vals)
where metrics = gesMetrics gevalSpec
checkAndGetFilesSingleOut :: Bool -> GEvalSpecification -> IO (SourceSpec, SourceSpec, SourceSpec)
@ -332,7 +333,7 @@ checkAndGetFiles forceInput gevalSpec = do
checkMultipleOuts :: GEvalSpecification -> IO (Maybe [FilePath])
checkMultipleOuts gevalSpec = do
isSimpleOutThere <- D.doesFileExist (outTestDirectory </> outFile)
let patterns = Prelude.map (\ext -> compile ("out-*" ++ ext)) ["", ".gz", ".bz2", ".xz"]
let patterns = Prelude.map (\ext -> compile ("out-*.tsv" ++ ext)) ["", ".gz", ".bz2", ".xz"]
multipleOuts <- Prelude.concat <$> globDir patterns outTestDirectory
if outFile == "out.tsv" && not isSimpleOutThere && multipleOuts /= []
then