module Text.Madlibs.Internal.Utils where
import Text.Madlibs.Internal.Types
import Text.Megaparsec.Text
import Text.Megaparsec.Error
import qualified Data.Text as T
import System.IO.Unsafe
import Lens.Micro
dropExtension :: FilePath -> FilePath
dropExtension = reverse . drop 1 . (dropWhile (/='.')) . reverse
getDir :: FilePath -> FilePath
getDir = reverse . (dropWhile (/='/')) . reverse
(.$) :: (a -> a -> b) -> a -> b
(.$) f x = f x x
normalize :: [(Prob, [PreTok])] -> [(Prob, [PreTok])]
normalize list = map (over _1 (/total)) list
where total = sum . map fst $ list
cdf :: [Prob] -> [Prob]
cdf = (drop 2) . (scanl (+) 0) . ((:) 0)
show' :: (Show a) => a -> T.Text
show' = (T.drop 1) . T.init . T.pack . show
parseErrorPretty' :: ParseError Char Dec -> T.Text
parseErrorPretty' = T.pack . parseErrorPretty
unTok :: PreTok -> T.Text
unTok (PreTok txt) = ""
unTok (Name txt _) = txt
readFile' :: FilePath -> IO T.Text
readFile' = (fmap T.pack) . readFile