Update Caesar Analyzers to be case-insensitive and testable.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
(ns cipher-analytical-machine.caesar-analyzers
|
||||
(:require [cipher-analytical-machine.caesar :as caesar]
|
||||
[clojure.string :as cs]
|
||||
[cipher-analytical-machine.cipher-analyzers :as ca])
|
||||
(:gen-class))
|
||||
|
||||
@@ -12,23 +13,36 @@
|
||||
(conj acc [key (caesar/decrypt-message ciphertext key symbols)]))
|
||||
'() keys)))
|
||||
|
||||
(defn get-all-scores
|
||||
"For pairs (key, plaintext) finds scores and return list of pairs [key, score]."
|
||||
[letter-frequences pairs]
|
||||
(map (fn [[key text]]
|
||||
[key (ca/chi-squared-statistic text letter-frequences)])
|
||||
pairs))
|
||||
|
||||
(defn get-min-score-pair
|
||||
"For pairs (key, plaintext) finds scores and return list of pairs [key, score]."
|
||||
[pairs]
|
||||
(reduce
|
||||
(fn [[key value] [new-key new-value]]
|
||||
(if (> value new-value) [new-key new-value]
|
||||
[key value]))
|
||||
[0 Double/MAX_VALUE]
|
||||
pairs))
|
||||
|
||||
(defn get-key
|
||||
"To find the key with frequencies of letters."
|
||||
[ciphertext symbols letter-frequences]
|
||||
(->> (get-all-texts ciphertext symbols)
|
||||
(map (fn [[key text]]
|
||||
[key (ca/chi-squared-statistic text letter-frequences)]))
|
||||
(reduce
|
||||
(fn [[key value] [new-key new-value]]
|
||||
(if (> value new-value) [new-key new-value]
|
||||
[key value]))
|
||||
[0 Double/MAX_VALUE])
|
||||
(get-all-scores letter-frequences)
|
||||
(get-min-score-pair)
|
||||
(first)))
|
||||
|
||||
(defn get-plaintext
|
||||
"Return the plaintext from a ciphertext."
|
||||
"Return the plaintext from a ciphertext. The function is case-insensitive."
|
||||
[ciphertext symbols letter-frequences]
|
||||
(caesar/decrypt-message ciphertext
|
||||
(get-key ciphertext symbols letter-frequences)
|
||||
symbols))
|
||||
(let [ciphertext (cs/lower-case ciphertext)]
|
||||
(caesar/decrypt-message ciphertext
|
||||
(get-key ciphertext symbols letter-frequences)
|
||||
symbols)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user