Advertisement
_eremec_

Untitled

Oct 18th, 2017
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns lab1.core
  2.   (:require
  3.     [clojure.string :as str]
  4.     [clojure.math.numeric-tower :as math]
  5.     [lab1.letters :refer [ltrs-freqs]]))
  6.  
  7. ;Consts
  8. ;---------------------------------------------------
  9.  
  10. (def text
  11.   "ЧПДШЭЗ ЖЮШ ГЮДШИП ГЯНФШЭШФ ЯГЧШЭЗБАУНПО ЕЭО ПАЮУТЮЯЫЯИПМЯЯ ШКЗУИЮП БПЕПТЯУ ЧШДШЭО Я ГФОБПТТШЦШ Г ТЯН ЯЕУТЮЯЫЯИПЮШДП ЧШЭЗБШФПЮУЭО ДПГГНПЮДЯФПУЮГО ИПИ ЕШИШБПЮУЭЗГЮФШ ЧДПФП ТП ЯГЧШЭЗБШФПТЯУ ФШБНШСТШГЮУЯ ГФОБПТТРВ Г ЕПТТРН ЯЕУТЮЯЫЯИПЮШДШН ЧШЭЗБШФПЮУЭО")
  12.  
  13. (def text2 "ИКЯЭ КИУЫИПТРИ КФИЕИТЭО ВУЮБРФЮЬПКО ЫЮКУЫРПРНЭ УИН ПВ ЫЮТЗХИ ФЫИНИТЭ ПВ ИЦВ КЯИЕАИП АГЭПЗ ЫЮФТВ УЮУ Э ПВЦВ УВНА ВТ ЖПЭ КФИЕИТЭО ВПУЫРЯ ФВИТТВИ ЭКУАККПФВ")
  14.  
  15. (def text3 "ЦУЖХЙСЙТТЫЙ НТЩУХСЕЪНУТТЫЙ ЧЙЬТУРУЗНН ФХЙИЦЧЕЖРДГЧ ЦУЁУО ЦУЖУПШФТУЦЧА ФХУЪЙЦЦУЖ Н СЙЧУИУЖ ЦЁУХЕ УЁХЕЁУЧПН ФХЙУЁХЕМУЖЕТНД ЬХЕТЙТНД Н ХЕЦФХЙИЙРЙТНД НТЩУХСЕЪНН Ж ВРЙПЧХУТТУС ЖНИЙ")
  16.  
  17. (def text-len (count text3))
  18.  
  19. ;---------------------------------------------------
  20.  
  21. (defn find-nearests [s n x]
  22.   (take n (sort-by #(math/abs (- x %)) s)))
  23.  
  24. (defn comp-freqs [freqs n]
  25.   (->> (vals freqs)
  26.        (map #(float (/ % text-len)))
  27.        (map (partial find-nearests (keys ltrs-freqs) n))
  28.        (zipmap (keys freqs))))
  29.  
  30. (defn decode-symb [comp-table sym]
  31.   (->> sym
  32.        (get comp-table)
  33.        (map (partial get ltrs-freqs))))    
  34.  
  35. (defn make-block [comp-table freqs word]
  36.   (as-> word block
  37.         (map (partial decode-symb comp-table) block)
  38.         (apply map list block)
  39.         (conj block (seq word))))  
  40.  
  41.  
  42. ;Output
  43. ;---------------------------------------------------
  44. (defn sym->str [sym]
  45.   (format "%2s" sym))
  46.  
  47. (defn line->str [line]
  48.   (str/join (map sym->str line)))
  49.  
  50. (defn block->str [block]
  51.   (str/join "\n" (map line->str block)))  
  52.  
  53. (defn print-blocks [blocks]
  54.   (println (str/join "\n\n"(map block->str blocks))))
  55.  
  56. ;--------------------------------------------------
  57.  
  58. (defn handle-input []
  59.   (println """L1 swap L2""")
  60.   (read-line))
  61.  
  62. (defn handle-swap1 [l1 l2 [line1 & lines]]
  63.   (conj lines (map #(case % l1 l2 l2 l1 %) line1)))
  64.  
  65. (defn handle-swap [l1 l2 [line1 & lines]]
  66.   (conj lines (replace {l1 l2, l2 l1} line1)))
  67.  
  68. (defn swap-letters [blocks]
  69.   (let [in (handle-input)]
  70.     (map (partial handle-swap (first in) (second in)) blocks)))      
  71.  
  72. (defn prepare-text [text n]
  73.   (let [freqs (frequencies text)
  74.         comp-table (comp-freqs freqs n)]
  75.     (as-> text text
  76.           (str/split text #" ")
  77.           (map (partial make-block comp-table freqs) text))))
  78.  
  79. (defn decode [text n]
  80.   (loop [blocks (prepare-text text n)]
  81.     (print-blocks blocks)
  82.     (recur (swap-letters blocks))))
  83.  
  84. (defn pretty [coll]
  85.   (->> coll
  86.        (map (partial str/join " : "))
  87.        (str/join "\n")))
  88.  
  89. (defn helper [text]
  90.   (let [freqs (frequencies text)]
  91.     (->> (vals freqs)
  92.          (map #(format "%.3f" (float (/ % text-len))))
  93.          (zipmap (keys freqs))
  94.          (sort-by val)
  95.          (pretty))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement