-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertforMonogramAnalysis.hs
48 lines (38 loc) · 1.7 KB
/
convertforMonogramAnalysis.hs
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
import NaturalLanguageModule (catalogiseMonogramms, monogramValueList)
import BlindtextModule (cryptotext2)
import MascModule (Direction (..), MascKey, masc, initializeMascGenome)
import Data.Maybe (fromMaybe)
import System.Environment (getArgs)
import Control.Monad (liftM)
main :: IO()
main = do
numberString <- liftM head getArgs
logfile <- readFile ("solvingLogs/output" ++ numberString ++ ".txt")
let key = getKeyFromLogfile logfile
writeFile ("solvingLogs/output" ++ numberString ++ "MonogramAnalysis.txt") (analyse key)
getKeyFromLogfile :: String -> String
getKeyFromLogfile = stripQuotes . last . words . number4frombehind . lines
where
number4frombehind :: [a] -> a
number4frombehind = last . init . init . init
stripQuotes :: String -> String
stripQuotes = filter (not . (==) '\"')
suggestedCleartext :: MascKey -> String
suggestedCleartext key = masc Decrypt key problem
problem :: String
problem = cryptotext2
statisticList :: [(Char, Int)]
statisticList = map (\(x,d) -> (x, round ((*) 0.01 . (*) d . intToDouble . length $ problem))) $ monogramValueList
intToDouble :: Int -> Double
intToDouble = fromIntegral
analyse :: MascKey -> String
analyse key = unlines . (header:) . map showCatalogEntry . map (analyseChar (catalogiseMonogramms . suggestedCleartext $ key)) $ statisticList
where
showCatalogEntry :: (Char, Int, Int) -> String
showCatalogEntry (c, n, m) = [c] ++ " " ++ show n ++ " " ++ show m
header :: String
header = "Characters expected found"
analyseChar :: (Eq a) => [(Char,Int)] -> (Char, a) -> (Char, a, Int)
analyseChar list (c,a) = (c, a, justLookup list 0 c)
justLookup :: (Eq a) => [(a,b)] -> b -> a -> b
justLookup list defaultValue key = fromMaybe defaultValue (lookup key list)