From 62902501253f0717cf37c5703e70035a8d7d7432 Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Fri, 21 Aug 2015 22:56:32 +0200 Subject: [PATCH] introduce GEvalSpecification --- app/Main.hs | 2 +- geval.cabal | 1 + src/GEval.hs | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- stack.yaml | 2 +- test/Spec.hs | 2 +- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 816adc1..7a88db3 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -6,5 +6,5 @@ import System.Environment main :: IO () main = do [expectedFilePath, outFilePath] <- getArgs - result <- geval expectedFilePath outFilePath + result <- gevalCore MSE expectedFilePath outFilePath print $ result diff --git a/geval.cabal b/geval.cabal index 02a5ab2..47d1d35 100644 --- a/geval.cabal +++ b/geval.cabal @@ -20,6 +20,7 @@ library , conduit , conduit-combinators , conduit-extra + , filepath , resourcet , text default-language: Haskell2010 diff --git a/src/GEval.hs b/src/GEval.hs index ba34be4..87dc636 100644 --- a/src/GEval.hs +++ b/src/GEval.hs @@ -1,5 +1,9 @@ module GEval - ( geval + ( geval, + gevalCore, + Metric(..), + GEvalSpecification(..), + defaultGEvalSpecification ) where import Data.Conduit @@ -12,8 +16,48 @@ import Data.Text import Data.Text.Read as TR import Control.Applicative -geval :: String -> String -> IO (Double) -geval expectedFilePath outFilePath = do +import System.FilePath +import Data.Maybe + +data Metric = MSE | BLEU + +defaultOutDirectory = "." +defaultTestName = "test-A" +defaultOutFile = "out.tsv" +defaultExpectedFile = "expected.tsv" + +defaultMetric :: Metric +defaultMetric = MSE + +data GEvalSpecification = GEvalSpecification + { gesOutDirectory :: String, + gesExpectedDirectory :: Maybe String, + gesTestName :: String, + gesOutFile :: String, + gesExpectedFile :: String, + gesMetric :: Metric } + + +defaultGEvalSpecification = GEvalSpecification { + gesOutDirectory = defaultOutDirectory, + gesExpectedDirectory = Nothing, + gesTestName = defaultTestName, + gesOutFile = defaultOutFile, + gesExpectedFile = defaultExpectedFile, + gesMetric = defaultMetric } + + +geval :: GEvalSpecification -> IO (Double) +geval gevalSpec = gevalCore metric expectedFilePath outFilePath + where expectedFilePath = expectedDirectory testName (gesExpectedFile gevalSpec) + outFilePath = outDirectory testName (gesOutFile gevalSpec) + expectedDirectory = fromMaybe outDirectory $ gesExpectedDirectory gevalSpec + outDirectory = gesOutDirectory gevalSpec + testName = gesTestName gevalSpec + metric = gesMetric gevalSpec + +gevalCore :: Metric -> String -> String -> IO (Double) +gevalCore MSE expectedFilePath outFilePath = do mse <- runResourceT $ (getZipSource $ (,) <$> ZipSource (items expectedFilePath) diff --git a/stack.yaml b/stack.yaml index 4cde5ad..58cfb8e 100644 --- a/stack.yaml +++ b/stack.yaml @@ -2,4 +2,4 @@ flags: {} packages: - '.' extra-deps: [] -resolver: lts-2.19 +resolver: nightly-2015-08-06 diff --git a/test/Spec.hs b/test/Spec.hs index 9c14fd0..3aae6a3 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -7,7 +7,7 @@ main :: IO () main = hspec $ do describe "mean square error" $ do it "simple test" $ do - geval "test/mse-simple/mse-simple/test-A/expected.tsv" "test/mse-simple/mse-simple-solution/test-A/out.tsv" `shouldReturnAlmost` 0.64549722436790 + geval (defaultGEvalSpecification {gesExpectedDirectory=Just "test/mse-simple/mse-simple", gesOutDirectory="test/mse-simple/mse-simple-solution"}) `shouldReturnAlmost` 0.64549722436790 class AEq a where (=~) :: a -> a -> Bool