Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
481735d9df | ||
|
|
d6ba958b5b | ||
|
|
d1b2ddc351 | ||
|
|
6a1ecf93db | ||
|
|
c03dc68458 |
@@ -11,6 +11,16 @@
|
|||||||
(inc (get acc val 0))))
|
(inc (get acc val 0))))
|
||||||
{} text))
|
{} 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
|
(defn chi-squared-letter-statistic
|
||||||
"To calculate the frequency distribution of a letter."
|
"To calculate the frequency distribution of a letter."
|
||||||
[letter-count letter-frequence length-text]
|
[letter-count letter-frequence length-text]
|
||||||
|
|||||||
@@ -7,16 +7,38 @@
|
|||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn get-possible-key-vector
|
(defn get-possible-key-vector
|
||||||
"Conver a possible key map to a vector of possible keys. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]]."
|
"Convert a possible key map to a vector of possible keys. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]]."
|
||||||
[possible-key-map symbols]
|
[possible-key-map symbols]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc el]
|
(fn [acc el]
|
||||||
(conj acc (get possible-key-map el)))
|
(conj acc (get possible-key-map el)))
|
||||||
[] symbols))
|
[] symbols))
|
||||||
|
|
||||||
(defn get-possible-keys
|
(defn get-possible-key-vector-from-reversed-count-map
|
||||||
"Return possible keys for a possible key vector. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]] and keys (\"aba\" \"abc\" ...)."
|
"Convert a reversed count map to a vector. Example, from {2 [\\a \\b] 1 [\\d] 3 [\\e]} to [[\\e] [\\a \\b] [\\d]])."
|
||||||
|
[reversed-count-map]
|
||||||
|
(->> reversed-count-map
|
||||||
|
(into [])
|
||||||
|
(sort-by #(- (first %)))
|
||||||
|
(map second)
|
||||||
|
(into [])))
|
||||||
|
|
||||||
|
(defn get-all-permutation-for-block
|
||||||
|
"Return a vector for all possible blocks that can be made from a vector of symbols."
|
||||||
|
[symbols]
|
||||||
|
(->> (comb/permutations symbols)
|
||||||
|
(map cs/join)
|
||||||
|
(into [])))
|
||||||
|
|
||||||
|
(defn get-all-combinations-for-blocks
|
||||||
|
"Return the all combination of blocks in a possible key vector. For example, if you have a map: {\\a [\\a \"bf\" \\c] \\b [\"bf\" \\c] \\c [\\a \\c]}, then the vector is [[\\a \"bf\" \\c] [\"bf\" \\c] [\\a \\c]] and keys (\"abfa\" \"abfc\" ...)."
|
||||||
[possible-key-vector]
|
[possible-key-vector]
|
||||||
(->> (apply comb/cartesian-product possible-key-vector)
|
(->> (apply comb/cartesian-product possible-key-vector)
|
||||||
(map cs/join)))
|
(map cs/join)))
|
||||||
|
|
||||||
|
(defn get-possible-key-combinations
|
||||||
|
"Return possible keys for a possible key vector. For example, if you have a map: {\\a [\\a \\b \\c] \\e [\\f \\g] \\c [\\a \\c]}, then the keys are (\"abcefg\" \"abcegf\" ...)."
|
||||||
|
[possible-key-vector]
|
||||||
|
(->> (map get-all-permutation-for-block possible-key-vector)
|
||||||
|
(get-all-combinations-for-blocks)))
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,13 @@
|
|||||||
(is (= {\a 2 \b 1}
|
(is (= {\a 2 \b 1}
|
||||||
(count-characters "aba")))))
|
(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
|
(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."
|
(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
|
(is (= 10.0
|
||||||
|
|||||||
@@ -13,11 +13,38 @@
|
|||||||
{\a [\a \b] \b [\b \a] \c [\a \c]} "abc" [[\a \b] [\b \a] [\a \c]]
|
{\a [\a \b] \b [\b \a] \c [\a \c]} "abc" [[\a \b] [\b \a] [\a \c]]
|
||||||
{\a [\a \b] \c [\b \a] \b [\a \c]} "abc" [[\a \b] [\a \c] [\b \a]])))
|
{\a [\a \b] \c [\b \a] \b [\a \c]} "abc" [[\a \b] [\a \c] [\b \a]])))
|
||||||
|
|
||||||
(deftest get-possible-keys-test
|
(deftest get-possible-key-vector-from-reversed-count-map-test
|
||||||
(testing "The keys must be the all combinations"
|
(testing "The block order must be in descending order"
|
||||||
|
(are [reversed-count-map expected-key-vector]
|
||||||
|
(= expected-key-vector (get-possible-key-vector-from-reversed-count-map reversed-count-map))
|
||||||
|
{1 [\a \c] 2 [\b]} [[\b] [\a \c]]
|
||||||
|
{2 [\a] 1 [\b] 3 [\c]} [[\c] [\a] [\b]])))
|
||||||
|
|
||||||
|
(deftest get-all-permutation-for-block-test
|
||||||
|
(testing "It must return all permutations"
|
||||||
|
(are [symbols expected-permutations]
|
||||||
|
(= expected-permutations (get-all-permutation-for-block symbols))
|
||||||
|
[\a \b] ["ab" "ba"]
|
||||||
|
[\a \b \c] ["abc" "acb" "bac" "bca" "cab" "cba"])))
|
||||||
|
|
||||||
|
(deftest get-all-combinations-for-blocks-test
|
||||||
|
(testing "The keys must be the all combinations of blocks"
|
||||||
(are [possible-keys-vector expected-keys]
|
(are [possible-keys-vector expected-keys]
|
||||||
(= expected-keys (get-possible-keys possible-keys-vector))
|
(= expected-keys (get-all-combinations-for-blocks possible-keys-vector))
|
||||||
[[\a \b] [\f] [\a \c] [\g]] '("afag" "afcg" "bfag" "bfcg")
|
[[\a \b] [\f] [\a \c] [\g]] '("afag" "afcg" "bfag" "bfcg")
|
||||||
[[\a \b] [\a \c]] '("aa" "ac" "ba" "bc")
|
[[\a \b] [\a \c]] '("aa" "ac" "ba" "bc")
|
||||||
[[\a \b \c] [\a \c] [\b \c]] '("aab" "aac" "acb" "acc" "bab" "bac" "bcb" "bcc" "cab" "cac" "ccb" "ccc"))))
|
[[\a \b \c] [\a \c] [\b \c]] '("aab" "aac" "acb" "acc" "bab" "bac" "bcb" "bcc" "cab" "cac" "ccb" "ccc"))))
|
||||||
|
|
||||||
|
(deftest get-possible-key-combinations-test
|
||||||
|
(testing "The keys must be the all combinations, but the blocks must create a permutations which will be joined as a combination"
|
||||||
|
(are [possible-keys-vector expected-keys]
|
||||||
|
(= expected-keys (get-possible-key-combinations possible-keys-vector))
|
||||||
|
[[\a \b] [\f] [\e \c] [\g]] '("abfecg" "abfceg" "bafecg" "bafceg")
|
||||||
|
[[\a \b] [\a \c]] '("abac" "abca" "baac" "baca")
|
||||||
|
[[\a \b \c] [\a \c] [\b \c]] '("abcacbc" "abcaccb" "abccabc" "abccacb"
|
||||||
|
"acbacbc" "acbaccb" "acbcabc" "acbcacb"
|
||||||
|
"bacacbc" "bacaccb" "baccabc" "baccacb"
|
||||||
|
"bcaacbc" "bcaaccb" "bcacabc" "bcacacb"
|
||||||
|
"cabacbc" "cabaccb" "cabcabc" "cabcacb"
|
||||||
|
"cbaacbc" "cbaaccb" "cbacabc" "cbacacb"))))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user