aboutsummaryrefslogtreecommitdiff
path: root/part2/src/Lib.hs
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 04:40:54 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 04:40:54 +0000
commit863a5d838205c52c9a0134897b5ee28164f87295 (patch)
tree5da38214ca58017ace71a500fbd25b5e7d341994 /part2/src/Lib.hs
downloadcoursework-863a5d838205c52c9a0134897b5ee28164f87295.tar.gz
publishHEADmaster
Diffstat (limited to 'part2/src/Lib.hs')
-rw-r--r--part2/src/Lib.hs136
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 @@
1module Lib
2 ( processText
3 , buildDictionary
4 , processInput
5 , twoModelDialog
6 , execLogStdGen
7 ) where
8
9import Control.Monad.Trans.Class (lift)
10import Control.Monad.Trans.State
11import Control.Monad.Trans.Writer
12import Data.Char (isLetter, isSpace, toLower)
13import Data.Function ((&))
14import Data.Functor.Identity
15import Data.List (nub, tails)
16import Data.Map (Map)
17import qualified Data.Map as Map
18import System.Random (StdGen, randomR)
19
20type Log = [String]
21type LogStdGen a = WriterT Log (StateT StdGen Identity) a
22
23split :: (Char -> Bool) -> String -> [String]
24split _ [] = []
25split predicate s =
26 let (prefix, rest) = break predicate s
27 in prefix :
28 case rest of
29 [] -> []
30 (_:xs) -> split predicate xs
31
32processText :: String -> [[String]]
33processText 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
41buildDictionary :: [[String]] -> Map String [String]
42buildDictionary 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
59random :: Int -> Int -> State StdGen Int
60random f t =
61 get >>= \gen ->
62 let (len, gen') = randomR (f, t) gen
63 in put gen' >> return len
64
65generatePhrase :: Map String [String] -> String -> State StdGen String
66generatePhrase 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
81processInput :: Map String [String] -> String -> State StdGen (Maybe String)
82processInput dict input =
83 if Map.member input dict
84 then generatePhrase dict input >>= \p -> return $ Just p
85 else return Nothing
86
87interleave :: [a] -> [a] -> [a]
88interleave as bs = concat $ zipWith (\a b -> [a] ++ [b]) as bs
89
90findKeyForResponse :: Map String [String] -> [String] -> Maybe String
91findKeyForResponse 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
101execLogStdGen :: StdGen -> LogStdGen a -> Log
102execLogStdGen gen app = runIdentity $ evalStateT (execWriterT app) gen
103
104dialogStep ::
105 Map String [String] -> String -> String -> LogStdGen (Maybe String)
106dialogStep 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
113twoModelDialog ::
114 Map String [String]
115 -> Map String [String]
116 -> String
117 -> Int
118 -> LogStdGen ()
119twoModelDialog 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)