You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cipher-analytical-machine/src/cipher_analytical_machine/symbols/frequencies.clj

93 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

(ns cipher-analytical-machine.symbols.frequencies
(:gen-class))
(defn map-to-string
"Convert the map of frequencies to the frequency string. Letters are sorted by frequency in descending order."
[frequency-map]
(->> frequency-map
(reduce (fn [acc el] (conj acc el)) [])
(sort-by last)
(map first)
(reverse)
(apply str)))
(def english-letter-frequencies {
\a 0.0804
\b 0.0154
\c 0.0306
\d 0.0399
\e 0.1251
\f 0.0230
\g 0.0196
\h 0.0549
\i 0.0726
\j 0.0016
\k 0.0067
\l 0.0414
\m 0.0253
\n 0.0709
\o 0.0760
\p 0.0200
\q 0.0011
\r 0.0612
\s 0.0654
\t 0.0925
\u 0.0271
\v 0.0099
\w 0.0192
\x 0.0019
\y 0.0173
\z 0.0009
})
(def ukrainian-letter-frequencies {
\а 0.064
\б 0.013
0.046
\г 0.013
0.0001
0.027
\е 0.042
0.005
0.007
0.020
0.055
\і 0.044
0.010
0.009
0.033
0.027
0.029
0.068
\о 0.086
\п 0.025
\р 0.043
\с 0.037
0.045
\у 0.027
0.003
\х 0.011
0.010
0.011
0.005
0.004
0.016
0.008
0.019
\ 0.138
})
(defn default-frequency-factory
"Returns a default map of letter frequencies for a language"
[language-code]
(cond
(= language-code "en")
english-letter-frequencies
(= language-code "uk")
ukrainian-letter-frequencies
:else
english-letter-frequencies))