diff --git a/geval.cabal b/geval.cabal index b12a371..d489ecb 100644 --- a/geval.cabal +++ b/geval.cabal @@ -21,6 +21,7 @@ library , GEval.BLEU , GEval.ClippEU , GEval.PrecisionRecall + , GEval.Common build-depends: base >= 4.7 && < 5 , cond , conduit diff --git a/src/GEval/Common.hs b/src/GEval/Common.hs new file mode 100644 index 0000000..a51c59d --- /dev/null +++ b/src/GEval/Common.hs @@ -0,0 +1,10 @@ +module GEval.Common + where + +(/.) :: (Eq a, Integral a) => a -> a -> Double +x /. 0 = 0.0 +x /. y = (fromIntegral x) / (fromIntegral y) + +safeDoubleDiv :: Double -> Double -> Double +safeDoubleDiv _ 0.0 = 0.0 +safeDoubleDiv x y = x / y diff --git a/src/GEval/Core.hs b/src/GEval/Core.hs index ed7cba2..2f7ab4b 100644 --- a/src/GEval/Core.hs +++ b/src/GEval/Core.hs @@ -36,6 +36,7 @@ import Data.Maybe import qualified Data.List.Split as DLS import GEval.BLEU +import GEval.Common type MetricValue = Double @@ -167,10 +168,6 @@ gevalCore' BLEU = gevalCore'' (Prelude.map Prelude.words . DLS.splitOn "\t" . un gevalCore' Accuracy = gevalCore'' strip strip hitOrMiss averageC id where hitOrMiss (x,y) = if x == y then 1.0 else 0.0 -(/.) :: Int -> Int -> Double -x /. 0 = 1.0 -x /. y = (fromIntegral x) / (fromIntegral y) - data SourceItem a = Got a | Done gevalCore'' :: (Text -> a) -> (Text -> b) -> ((a, b) -> c) -> (Sink c (ResourceT IO) d) -> (d -> Double) -> String -> String -> IO (MetricValue) diff --git a/src/GEval/PrecisionRecall.hs b/src/GEval/PrecisionRecall.hs index c1c4bef..4eefb33 100644 --- a/src/GEval/PrecisionRecall.hs +++ b/src/GEval/PrecisionRecall.hs @@ -5,6 +5,8 @@ module GEval.PrecisionRecall(fMeasure, f1Measure, f2Measure, precision, recall, precisionAndRecall, precisionAndRecallFromCounts) where +import GEval.Common + import Data.Graph.Inductive import Data.Graph.Inductive.Query.MaxFlow @@ -39,18 +41,7 @@ precisionAndRecall matchFun expected got precisionAndRecallFromCounts :: (Int, Int, Int) -> (Double, Double) precisionAndRecallFromCounts (tp, nbExpected, nbGot) = - (tp `safeDiv` nbGot, tp `safeDiv` nbExpected) - --- division with possible zero -safeDiv :: Int -> Int -> Double -safeDiv _ 0 = 1.0 -safeDiv x y = (fromIntegral x) / (fromIntegral y) - -safeDoubleDiv :: Double -> Double -> Double -safeDoubleDiv _ 0.0 = 0.0 -safeDoubleDiv x y = x / y - - + (tp /. nbGot, tp /. nbExpected) precision :: (a -> b -> Bool) -> [a] -> [b] -> Double precision matchFun expected got = fst $ precisionAndRecall matchFun expected got