Handle escaping spaces in configuration files

This commit is contained in:
Filip Gralinski 2020-10-17 18:56:30 +02:00
parent 819fbecedc
commit 40d5ac602e
5 changed files with 30 additions and 2 deletions

View File

@ -40,6 +40,9 @@ import GEval.Model
import GEval.ModelTraining
import Data.List (find, intercalate)
import Data.List.Utils (split)
import Data.Char (isSpace)
import Data.Conduit.SmartSource
import Data.CartesianStrings
@ -352,7 +355,26 @@ readOptsFromConfigFile :: [String] -> FilePath -> IO (Either (ParserResult GEval
readOptsFromConfigFile args configFilePath = do
configH <- openFile configFilePath ReadMode
contents <- hGetContents configH
getOptions' False ((words contents) ++ args)
getOptions' False ((parseConfigFileContents contents) ++ args)
data ConfigFileSymbol = Literal Char | Separator
deriving (Eq, Show)
-- very simplistic handling of backslash escaping
-- even "\\" is treated as double backslashes...
parseConfigFileContents :: String -> [String]
parseConfigFileContents contents =
filter (not . null)
$ map (map (\(Literal c) -> c))
$ split [Separator]
$ parseSymbols contents
where parseSymbols ('\\':c:t)
| isSpace c = (Literal c) : parseSymbols t
| otherwise = (Literal '\\'): (Literal c) :parseSymbols t
parseSymbols (c:t)
| isSpace c = Separator : parseSymbols t
| otherwise = (Literal c) : parseSymbols t
parseSymbols [] = []
attemptToReadOptsFromConfigFile :: [String] -> GEvalOptions -> IO (Either (ParserResult GEvalOptions) GEvalOptions)
attemptToReadOptsFromConfigFile args opts = do

View File

@ -131,9 +131,10 @@ main = hspec $ do
describe "CER" $ do
it "simple example" $
runGEvalTest "cer-simple" `shouldReturnAlmost` 0.28947368421
describe "CER" $ do
it "simple example (Mean/CER)" $
runGEvalTest "cer-mean-simple" `shouldReturnAlmost` 0.277777777777778
it "space escaping" $
runGEvalTest "cer-space-escaping" `shouldReturnAlmost` 0.0555555
describe "Accuracy" $ do
it "simple example" $
runGEvalTest "accuracy-simple" `shouldReturnAlmost` 0.6

View File

@ -0,0 +1,2 @@
foo bar
aaa bxb ccc
1 foo bar
2 aaa bxb ccc

View File

@ -0,0 +1 @@
--metric CER:s<\ +><\ >

View File

@ -0,0 +1,2 @@
foo bar
aaa bbb ccc
1 foo bar
2 aaa bbb ccc