···6969loadShakespeare path = do
7070 contents <- lift $ readFile path
7171 let annotated = annotateCapitals contents
7272- (m,cs) <- ExceptT . return . note "Couldn't fit data in hotMap" $ hotMap (Proxy :: Proxy 40) annotated
7272+ (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
+4-3
src/Grenade/Utils/OneHot.hs
···52525353-- | Create a one hot map from any enumerable.
5454-- Returns a map, and the ordered list for the reverse transformation
5555-hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Maybe (Map a Int, Vector a)
5555+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
6262- Just (M.fromList $ zip uniq [0..], V.fromList uniq)
6363- else Nothing
6262+ Right (M.fromList $ zip uniq [0..], V.fromList uniq)
6363+ else
6464+ Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
64656566-- | From a map and value, create a 1D Shape
6667-- with one index hot (1) with the rest 0.