aboutsummaryrefslogtreecommitdiff
path: root/part2/app/Main.hs
blob: 73a7e55aad6e12164e04d6f0e8afce2185872fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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]"