check emptiness

This commit is contained in:
Filip Gralinski 2015-11-06 22:42:08 +01:00 committed by Filip Gralinski
parent 570411b702
commit 7f3973890d
7 changed files with 18 additions and 2 deletions

View File

@ -32,6 +32,7 @@ library
, resourcet
, split
, text
, unix
default-language: Haskell2010
executable geval

View File

@ -26,9 +26,11 @@ import Data.Text
import Data.Text.Read as TR
import Control.Applicative
import Control.Exception
import Control.Conditional (unlessM)
import Control.Conditional (unlessM, whenM)
import qualified System.Directory as D
import System.Posix
import System.FilePath
import Data.Maybe
@ -79,6 +81,7 @@ data GEvalException = NoExpectedFile FilePath
| FileAlreadyThere FilePath
| TooFewLines
| TooManyLines
| EmptyOutput
deriving (Eq)
instance Exception GEvalException
@ -93,6 +96,7 @@ instance Show GEvalException where
show (FileAlreadyThere filePath) = somethingWrongWithFilesMessage "File already there" filePath
show TooFewLines = "Too few lines in the output file"
show TooManyLines = "Too many lines in the output file"
show EmptyOutput = "The output file is empty"
somethingWrongWithFilesMessage :: String -> FilePath -> String
somethingWrongWithFilesMessage msg filePath = Prelude.concat
@ -106,6 +110,10 @@ defaultGEvalSpecification = GEvalSpecification {
gesExpectedFile = defaultExpectedFile,
gesMetric = defaultMetric }
isEmptyFile :: FilePath -> IO (Bool)
isEmptyFile path = do
stat <- getFileStatus path
return ((fileSize stat) == 0)
geval :: GEvalSpecification -> IO (MetricValue)
geval gevalSpec = do
@ -131,6 +139,7 @@ gevalCore RMSE expectedFilePath outFilePath = do
gevalCore metric expectedFilePath outFilePath = do
unlessM (D.doesFileExist expectedFilePath) $ throwM $ NoExpectedFile expectedFilePath
unlessM (D.doesFileExist outFilePath) $ throwM $ NoOutFile outFilePath
whenM (isEmptyFile outFilePath) $ throwM $ EmptyOutput
gevalCore' metric expectedFilePath outFilePath
gevalCore' :: Metric -> String -> String -> IO (MetricValue)

View File

@ -38,7 +38,8 @@ main = hspec $ do
runGEvalTest "error-too-few-lines" `shouldThrow` (== TooFewLines)
it "too many lines are handled" $ do
runGEvalTest "error-too-many-lines" `shouldThrow` (== TooManyLines)
it "empty output is handled" $ do
runGEvalTest "empty-output" `shouldThrow` (== EmptyOutput)
extractVal :: (Either (ParserResult GEvalOptions) (Maybe MetricValue)) -> IO MetricValue
extractVal (Right (Just val)) = return val

View File

@ -0,0 +1 @@
--metric BLEU

View File

@ -0,0 +1,2 @@
Alice has a cat.
Basia has a dog.
1 Alice has a cat.
2 Basia has a dog.

View File

@ -0,0 +1,2 @@
Ala ma kota.
Basia ma psa.
1 Ala ma kota.
2 Basia ma psa.