diff options
| author | 2026-11-08 04:40:54 +0000 | |
|---|---|---|
| committer | 2026-11-08 04:40:54 +0000 | |
| commit | 863a5d838205c52c9a0134897b5ee28164f87295 (patch) | |
| tree | 5da38214ca58017ace71a500fbd25b5e7d341994 /part2/app/Main.hs | |
| download | coursework-863a5d838205c52c9a0134897b5ee28164f87295.tar.gz | |
Diffstat (limited to 'part2/app/Main.hs')
| -rw-r--r-- | part2/app/Main.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/part2/app/Main.hs b/part2/app/Main.hs new file mode 100644 index 0000000..73a7e55 --- /dev/null +++ b/part2/app/Main.hs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | module Main | ||
| 2 | ( main | ||
| 3 | ) where | ||
| 4 | |||
| 5 | import Control.Monad.Trans.State | ||
| 6 | import Data.Map (Map) | ||
| 7 | import qualified Data.Map as Map | ||
| 8 | import Lib | ||
| 9 | import System.Environment (getArgs, getProgName) | ||
| 10 | import System.Exit (die) | ||
| 11 | import System.IO | ||
| 12 | import System.Random (getStdGen) | ||
| 13 | import UnescapingPrint (ushow) | ||
| 14 | |||
| 15 | saveDictionary :: FilePath -> Map String [String] -> IO () | ||
| 16 | saveDictionary filePath dict = | ||
| 17 | withFile filePath WriteMode $ \h -> | ||
| 18 | mapM_ | ||
| 19 | (\(k, v) -> hPutStrLn h $ ushow k ++ ": " ++ ushow v) | ||
| 20 | (Map.toList dict) | ||
| 21 | |||
| 22 | main :: IO () | ||
| 23 | main = | ||
| 24 | getArgs >>= \args -> | ||
| 25 | getStdGen >>= \gen -> | ||
| 26 | case args of | ||
| 27 | (dict1Path:initial:dict2Path:nstr:[]) -> | ||
| 28 | readFile dict1Path >>= \text1 -> | ||
| 29 | readFile dict2Path >>= \text2 -> | ||
| 30 | let dict1 = buildDictionary (processText text1) | ||
| 31 | dict2 = buildDictionary (processText text2) | ||
| 32 | n = read nstr :: Int | ||
| 33 | in saveDictionary "./tmp/dict1.txt" dict1 >> | ||
| 34 | saveDictionary "./tmp/dict2.txt" dict2 >> | ||
| 35 | mapM_ | ||
| 36 | putStrLn | ||
| 37 | (execLogStdGen gen $ twoModelDialog dict1 dict2 initial n) | ||
| 38 | (dictPath:initial:[]) -> | ||
| 39 | readFile dictPath >>= \text -> | ||
| 40 | let dict = buildDictionary (processText text) | ||
| 41 | in saveDictionary "./tmp/dict.txt" dict >> | ||
| 42 | case evalState (processInput dict initial) gen of | ||
| 43 | Just s -> putStrLn s | ||
| 44 | Nothing -> | ||
| 45 | die $ | ||
| 46 | "There is no \"" ++ | ||
| 47 | initial ++ "\" in the constructed dictionary!" | ||
| 48 | _ -> | ||
| 49 | getProgName >>= \arg0 -> | ||
| 50 | die $ "Usage: " ++ arg0 ++ " DICT1 INITIAL [DICT2 N]" | ||
