diff options
Diffstat (limited to 'part2/src')
| -rw-r--r-- | part2/src/Lib.hs | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/part2/src/Lib.hs b/part2/src/Lib.hs new file mode 100644 index 0000000..22b54e9 --- /dev/null +++ b/part2/src/Lib.hs | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | module Lib | ||
| 2 | ( processText | ||
| 3 | , buildDictionary | ||
| 4 | , processInput | ||
| 5 | , twoModelDialog | ||
| 6 | , execLogStdGen | ||
| 7 | ) where | ||
| 8 | |||
| 9 | import Control.Monad.Trans.Class (lift) | ||
| 10 | import Control.Monad.Trans.State | ||
| 11 | import Control.Monad.Trans.Writer | ||
| 12 | import Data.Char (isLetter, isSpace, toLower) | ||
| 13 | import Data.Function ((&)) | ||
| 14 | import Data.Functor.Identity | ||
| 15 | import Data.List (nub, tails) | ||
| 16 | import Data.Map (Map) | ||
| 17 | import qualified Data.Map as Map | ||
| 18 | import System.Random (StdGen, randomR) | ||
| 19 | |||
| 20 | type Log = [String] | ||
| 21 | type LogStdGen a = WriterT Log (StateT StdGen Identity) a | ||
| 22 | |||
| 23 | split :: (Char -> Bool) -> String -> [String] | ||
| 24 | split _ [] = [] | ||
| 25 | split predicate s = | ||
| 26 | let (prefix, rest) = break predicate s | ||
| 27 | in prefix : | ||
| 28 | case rest of | ||
| 29 | [] -> [] | ||
| 30 | (_:xs) -> split predicate xs | ||
| 31 | |||
| 32 | processText :: String -> [[String]] | ||
| 33 | processText text = text | ||
| 34 | & fmap (toLower) | ||
| 35 | & split (`elem` ".!?;:()") | ||
| 36 | & fmap (split isSpace) | ||
| 37 | & (fmap . fmap) (filter isLetter) | ||
| 38 | & fmap (filter (not . null)) | ||
| 39 | & filter (\s -> length s >= 2) | ||
| 40 | |||
| 41 | buildDictionary :: [[String]] -> Map String [String] | ||
| 42 | buildDictionary sentences = | ||
| 43 | let bigrams = [(w1, w2) | s <- sentences, (w1:w2:_) <- tails s] | ||
| 44 | trigrams = [(w1, w2, w3) | s <- sentences, (w1:w2:w3:_) <- tails s] | ||
| 45 | bigramEntries = | ||
| 46 | foldr | ||
| 47 | (\(w1, w2) acc -> Map.insertWith (++) w1 [w2] acc) | ||
| 48 | Map.empty | ||
| 49 | bigrams | ||
| 50 | trigramEntries = | ||
| 51 | foldr | ||
| 52 | (\(w1, w2, w3) acc -> | ||
| 53 | Map.insertWith (++) w1 [w2 ++ " " ++ w3] $ | ||
| 54 | Map.insertWith (++) (w1 ++ " " ++ w2) [w3] acc) | ||
| 55 | Map.empty | ||
| 56 | trigrams | ||
| 57 | in Map.map nub $ Map.unionWith (++) bigramEntries trigramEntries | ||
| 58 | |||
| 59 | random :: Int -> Int -> State StdGen Int | ||
| 60 | random f t = | ||
| 61 | get >>= \gen -> | ||
| 62 | let (len, gen') = randomR (f, t) gen | ||
| 63 | in put gen' >> return len | ||
| 64 | |||
| 65 | generatePhrase :: Map String [String] -> String -> State StdGen String | ||
| 66 | generatePhrase dict start = | ||
| 67 | random 2 15 >>= \len -> | ||
| 68 | go start [] len | ||
| 69 | where | ||
| 70 | go :: String -> [String] -> Int -> State StdGen String | ||
| 71 | go key acc n | ||
| 72 | | n <= 0 = return $ unwords $ reverse acc | ||
| 73 | | otherwise = | ||
| 74 | case Map.lookup key dict of | ||
| 75 | Nothing -> return $ unwords $ reverse acc | ||
| 76 | Just conts -> | ||
| 77 | random 0 (length conts - 1) >>= \i -> | ||
| 78 | let next = conts !! i | ||
| 79 | in go next (next : acc) (n - length (words next)) | ||
| 80 | |||
| 81 | processInput :: Map String [String] -> String -> State StdGen (Maybe String) | ||
| 82 | processInput dict input = | ||
| 83 | if Map.member input dict | ||
| 84 | then generatePhrase dict input >>= \p -> return $ Just p | ||
| 85 | else return Nothing | ||
| 86 | |||
| 87 | interleave :: [a] -> [a] -> [a] | ||
| 88 | interleave as bs = concat $ zipWith (\a b -> [a] ++ [b]) as bs | ||
| 89 | |||
| 90 | findKeyForResponse :: Map String [String] -> [String] -> Maybe String | ||
| 91 | findKeyForResponse dict ws = | ||
| 92 | case dropWhile (\w -> Map.notMember w dict) keys of | ||
| 93 | [] -> Nothing | ||
| 94 | (x:_) -> Just x | ||
| 95 | where | ||
| 96 | rws = reverse ws | ||
| 97 | keys = | ||
| 98 | (interleave (zipWith (\a b -> b ++ " " ++ a) rws (tail $ rws)) rws) ++ | ||
| 99 | [head ws] | ||
| 100 | |||
| 101 | execLogStdGen :: StdGen -> LogStdGen a -> Log | ||
| 102 | execLogStdGen gen app = runIdentity $ evalStateT (execWriterT app) gen | ||
| 103 | |||
| 104 | dialogStep :: | ||
| 105 | Map String [String] -> String -> String -> LogStdGen (Maybe String) | ||
| 106 | dialogStep dict prevPhrase prefix = | ||
| 107 | case findKeyForResponse dict (words prevPhrase) of | ||
| 108 | Nothing -> tell [prefix ++ ": There is no continuation"] >> return Nothing | ||
| 109 | Just key -> | ||
| 110 | lift (generatePhrase dict key) >>= \p -> | ||
| 111 | tell [prefix ++ ": " ++ key ++ " => " ++ p] >> return (Just p) | ||
| 112 | |||
| 113 | twoModelDialog :: | ||
| 114 | Map String [String] | ||
| 115 | -> Map String [String] | ||
| 116 | -> String | ||
| 117 | -> Int | ||
| 118 | -> LogStdGen () | ||
| 119 | twoModelDialog dict1 dict2 start len = | ||
| 120 | if Map.member start dict1 | ||
| 121 | then loop dict1 dict2 start 0 len | ||
| 122 | else tell ["No such key in DICT1: " ++ start] >> return () | ||
| 123 | where | ||
| 124 | loop :: | ||
| 125 | Map String [String] | ||
| 126 | -> Map String [String] | ||
| 127 | -> String | ||
| 128 | -> Int | ||
| 129 | -> Int | ||
| 130 | -> LogStdGen () | ||
| 131 | loop _ _ _ _ 0 = return () | ||
| 132 | loop d od prev i l = | ||
| 133 | dialogStep d prev ("Model " ++ show (i `mod` 2 + 1)) >>= \resp -> | ||
| 134 | case resp of | ||
| 135 | Nothing -> return () | ||
| 136 | Just r -> loop od d r (i + 1) (l - 1) | ||
