39 lines
1.2 KiB
Clojure
39 lines
1.2 KiB
Clojure
(ns cipher-analytical-machine.analyzers.analyzers
|
|
(:require [clojure.string :as cs])
|
|
(:gen-class))
|
|
|
|
(defn count-characters
|
|
"Count the letters and return map."
|
|
[text]
|
|
(reduce
|
|
(fn [acc val]
|
|
(assoc acc val
|
|
(inc (get acc val 0))))
|
|
{} text))
|
|
|
|
(defn chi-squared-letter-statistic
|
|
"To calculate the frequency distribution of a letter."
|
|
[letter-count letter-frequence length-text]
|
|
(let [letter-frequence (* length-text letter-frequence)]
|
|
(/ (Math/pow
|
|
(- letter-count letter-frequence) 2)
|
|
letter-frequence)))
|
|
|
|
(defn chi-squared-statistic
|
|
"To calculate the frequency distribution of a language. More detail about the algorithm here: http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/"
|
|
[text letter-frequencies]
|
|
(let [length-text (count text)
|
|
letter-counts (count-characters (cs/lower-case text))]
|
|
;letter-counts
|
|
(reduce
|
|
(fn [acc [char count]]
|
|
(+ acc
|
|
(if (contains? letter-frequencies char)
|
|
(chi-squared-letter-statistic count
|
|
(get letter-frequencies char)
|
|
length-text)
|
|
0)))
|
|
0
|
|
letter-counts)))
|
|
|