From 863a5d838205c52c9a0134897b5ee28164f87295 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 07:40:54 +0300 Subject: publish --- part2/app/Main.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 part2/app/Main.hs (limited to 'part2/app/Main.hs') 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 @@ +module Main + ( main + ) where + +import Control.Monad.Trans.State +import Data.Map (Map) +import qualified Data.Map as Map +import Lib +import System.Environment (getArgs, getProgName) +import System.Exit (die) +import System.IO +import System.Random (getStdGen) +import UnescapingPrint (ushow) + +saveDictionary :: FilePath -> Map String [String] -> IO () +saveDictionary filePath dict = + withFile filePath WriteMode $ \h -> + mapM_ + (\(k, v) -> hPutStrLn h $ ushow k ++ ": " ++ ushow v) + (Map.toList dict) + +main :: IO () +main = + getArgs >>= \args -> + getStdGen >>= \gen -> + case args of + (dict1Path:initial:dict2Path:nstr:[]) -> + readFile dict1Path >>= \text1 -> + readFile dict2Path >>= \text2 -> + let dict1 = buildDictionary (processText text1) + dict2 = buildDictionary (processText text2) + n = read nstr :: Int + in saveDictionary "./tmp/dict1.txt" dict1 >> + saveDictionary "./tmp/dict2.txt" dict2 >> + mapM_ + putStrLn + (execLogStdGen gen $ twoModelDialog dict1 dict2 initial n) + (dictPath:initial:[]) -> + readFile dictPath >>= \text -> + let dict = buildDictionary (processText text) + in saveDictionary "./tmp/dict.txt" dict >> + case evalState (processInput dict initial) gen of + Just s -> putStrLn s + Nothing -> + die $ + "There is no \"" ++ + initial ++ "\" in the constructed dictionary!" + _ -> + getProgName >>= \arg0 -> + die $ "Usage: " ++ arg0 ++ " DICT1 INITIAL [DICT2 N]" -- cgit v1.3.1