aboutsummaryrefslogtreecommitdiff
path: root/part2/app/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'part2/app/Main.hs')
-rw-r--r--part2/app/Main.hs50
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 @@
1module Main
2 ( main
3 ) where
4
5import Control.Monad.Trans.State
6import Data.Map (Map)
7import qualified Data.Map as Map
8import Lib
9import System.Environment (getArgs, getProgName)
10import System.Exit (die)
11import System.IO
12import System.Random (getStdGen)
13import UnescapingPrint (ushow)
14
15saveDictionary :: FilePath -> Map String [String] -> IO ()
16saveDictionary filePath dict =
17 withFile filePath WriteMode $ \h ->
18 mapM_
19 (\(k, v) -> hPutStrLn h $ ushow k ++ ": " ++ ushow v)
20 (Map.toList dict)
21
22main :: IO ()
23main =
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]"