Commits

Steve Losh  committed f8dd933

First working version.

  • Participants
  • Parent commits cbd74ca

Comments (0)

Files changed (3)

File resources/public/css/style.less

     overflow-y: scroll;
 }
 #content {
-    width: 600px;
+    width: 400px;
     margin: 40px auto;
 }
 header {
     text-align: center;
 }
 textarea {
-    width: 300px;
+    width: 390px;
     height: 200px;
 }
 .form-stacked {
         margin-top: 10px;
     }
 }
+.lesson {
+    font-family: 'Menlo', monospace;
+}

File src/typkov/templates.clj

   (:use [noir.core :only [defpartial]]
         [hiccup.page-helpers :only [include-css html5 include-js link-to]]))
 
+
 (defpartial base [title & content]
   (html5
     [:head
           [:textarea#text.error {:name "text"} text]]
          [:button {:type "submit" :class "btn primary"} "Get a Lesson"]]
         (when lesson
-          [:pre lesson])))
+          [:textarea.lesson lesson])))

File src/typkov/views/core.clj

 (ns typkov.views.core
   (:require [typkov.templates :as t])
   (:require [noir.validation :as valid])
+  (:use [clojure.string :only [split trim]])
   (:use [noir.core :only [render defpage]]))
 
+
+; Utils -----------------------------------------------------------------------
+(defn random-choice [coll]
+  (nth coll (rand-int (count coll))))
+
+
+; Gtypist ---------------------------------------------------------------------
+
+(defn to-gtypist-single [lesson n]
+  (str "*:S_LESSON" n "\n"
+       "B:Lesson " n "\n"
+       (apply str (map #(str %2 ":" % "\n")
+                       lesson
+                       (concat ["D"] (cycle [" "]))))
+       "G:MENU\n"))
+
+(defn to-gtypist [lessons]
+  (str "G:MENU\n\n"
+       (apply str (interleave (map to-gtypist-single
+                                   lessons
+                                   (range 1 (inc (count lessons))))
+                              (cycle ["\n"])))
+       "\n*:MENU\n"
+       "M: \"Lesson generated by Typkov\"\n"
+       (apply str (map #(str " :S_LESSON" % " \"Lesson " % "\"\n")
+                       (range 1 (inc (count lessons)))))))
+
+
+; Lesson generation -----------------------------------------------------------
+(defn markov-dict [words]
+  (zipmap words (cycle [""])))
+
+(defn markov-get [dict]
+  (random-choice (keys dict)))
+
+(defn get-line [dict]
+  (let [words (repeatedly 10 (partial markov-get dict))]
+    (trim (apply str (interleave words (cycle [" "]))))))
+
+(defn get-single [dict n]
+  (repeatedly 4 (partial get-line dict)))
+
+(defn get-lesson [text]
+  (let [words (doall (filter (comp not empty?) (split text #"\s+")))
+        dict (markov-dict words)
+        lessons (map (partial get-single dict) (range 0 10))]
+    (to-gtypist lessons)))
+
+
+; Home page -------------------------------------------------------------------
 (defn text-valid? [{:keys [text]}]
   (valid/rule (valid/min-length? text 24)
               [:text "You must enter some text!"])
   (not (valid/errors? :text)))
 
+
 (defpage "/" {:as data}
   (t/home (:text data) nil))
 
 (defpage [:post "/"] {:as data}
   (if (text-valid? data)
-    (t/home (:text data) "some lesson here")
+    (t/home (:text data)
+            (get-lesson (:text data)))
     (render "/" data)))