diff --git a/src/cipher_analytical_machine/analyzers/analyzers.clj b/src/cipher_analytical_machine/analyzers/analyzers.clj index 19a7d53..b648c1b 100644 --- a/src/cipher_analytical_machine/analyzers/analyzers.clj +++ b/src/cipher_analytical_machine/analyzers/analyzers.clj @@ -11,6 +11,16 @@ (inc (get acc val 0)))) {} text)) +(defn reverse-count-characters + "Convert a map of pairs to a map where the characters grouped by the count and a count is the key." + [count-map] + (reduce + (fn [acc [symbol count]] + (->> (conj (get acc count []) symbol) + (into []) + (assoc acc count))) + {} count-map)) + (defn chi-squared-letter-statistic "To calculate the frequency distribution of a letter." [letter-count letter-frequence length-text] diff --git a/test/cipher_analytical_machine/analyzers/analyzers_test.clj b/test/cipher_analytical_machine/analyzers/analyzers_test.clj index 1271dc1..f923a48 100644 --- a/test/cipher_analytical_machine/analyzers/analyzers_test.clj +++ b/test/cipher_analytical_machine/analyzers/analyzers_test.clj @@ -8,6 +8,13 @@ (is (= {\a 2 \b 1} (count-characters "aba"))))) +(deftest reverse-count-characters-test + (testing "The symbols must be grouped as a vector and the count must be a key." + (are [count-map expected-map] + (= expected-map (reverse-count-characters count-map)) + {\a 1 \b 2 \c 1} {1 [\a \c] 2 [\b]} + {\a 1 \b 2 \c 3} {1 [\a] 2 [\b] 3 [\c]}))) + (deftest chi-squared-letter-statistic-test (testing "If the frequence of A is 0.1, the text length is 100 and contains 20 times, then the chi-squared is 10." (is (= 10.0