Give a slightly not terrible error message

This commit is contained in:
Huw Campbell 2018-01-04 16:16:34 +11:00
parent cb2eb0db8f
commit 8349a85b0f
2 changed files with 5 additions and 4 deletions

View File

@ -69,7 +69,7 @@ loadShakespeare :: FilePath -> ExceptT String IO (Vector Int, M.Map Char Int, Ve
loadShakespeare path = do
contents <- lift $ readFile path
let annotated = annotateCapitals contents
(m,cs) <- ExceptT . return . note "Couldn't fit data in hotMap" $ hotMap (Proxy :: Proxy 40) annotated
(m,cs) <- ExceptT . return $ hotMap (Proxy :: Proxy 40) annotated
hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
return (V.fromList hot, m, cs)

View File

@ -52,15 +52,16 @@ oneHot hot =
-- | Create a one hot map from any enumerable.
-- Returns a map, and the ordered list for the reverse transformation
hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Maybe (Map a Int, Vector a)
hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Either String (Map a Int, Vector a)
hotMap n as =
let len = fromIntegral $ natVal n
uniq = [ c | (c:_) <- group $ sort as]
hotl = length uniq
in if hotl == len
then
Just (M.fromList $ zip uniq [0..], V.fromList uniq)
else Nothing
Right (M.fromList $ zip uniq [0..], V.fromList uniq)
else
Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
-- | From a map and value, create a 1D Shape
-- with one index hot (1) with the rest 0.