on master help sign in
Give a slightly not terrible error message
commit8349a85b0fdb7378c13bc286e8b26146e5b9b64f authorHuw Campbell <huw.campbell@ambiata.com> dateThu 4 Jan 2018, 16:16 (9 years ago) parentcb2eb0d
2 files changed+5−4
examples/main/shakespeare.hs+1−1 src/Grenade/Utils/OneHot.hs+4−3
examples/main/shakespeare.hs+1−1
@@ -69,7 +69,7 @@ loadShakespeare :: FilePath -> ExceptT String IO (Vector Int, M.Map Char Int, Ve
6969loadShakespeare path = do
7070 contents <- lift $ readFile path
7171 let annotated = annotateCapitals contents
72− (m,cs) <- ExceptT . return . note "Couldn't fit data in hotMap" $ hotMap (Proxy :: Proxy 40) annotated
72+ (m,cs) <- ExceptT . return $ hotMap (Proxy :: Proxy 40) annotated
7373 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
7474 return (V.fromList hot, m, cs)
7575
src/Grenade/Utils/OneHot.hs+4−3
@@ -52,15 +52,16 @@ oneHot hot =
5252
5353-- | Create a one hot map from any enumerable.
5454-- Returns a map, and the ordered list for the reverse transformation
55−hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Maybe (Map a Int, Vector a)
55+hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Either String (Map a Int, Vector a)
5656hotMap n as =
5757 let len = fromIntegral $ natVal n
5858 uniq = [ c | (c:_) <- group $ sort as]
5959 hotl = length uniq
6060 in if hotl == len
6161 then
62− Just (M.fromList $ zip uniq [0..], V.fromList uniq)
63− else Nothing
62+ Right (M.fromList $ zip uniq [0..], V.fromList uniq)
63+ else
64+ Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
6465
6566-- | From a map and value, create a 1D Shape
6667-- with one index hot (1) with the rest 0.