Mired in code
Check-in [06eff72cda]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a -main to handle command line arguments and clean up *-monkey argument handling, some minor style tweaks, make read-words read from a file.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:06eff72cda3393a5a3e69a740b2f0711d6247ef6
User & Date: mwm@mired.org 2010-09-07 02:37:38
Context
2010-11-22
23:18
Add a trailing newline. check-in: b23579bfb1 user: mwm@mired.org tags: trunk
2010-09-07
02:37
Add a -main to handle command line arguments and clean up *-monkey argument handling, some minor style tweaks, make read-words read from a file. check-in: 06eff72cda user: mwm@mired.org tags: trunk
2010-09-06
00:47
Add the word monkey source code. check-in: 1a04a6f9bc user: mwm@mired.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to clojure/monkey.clj.

1
2


3

4



5
6
7
8
9
10


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

27
28
29
30
31
32




33
34
35
36











;;;; A variable-order word monkey (markov chain of words.



(ns monkey (:use [clojure.contrib.str-utils2 :only (split trim)]))





(defn read-words []
  (letfn [(read-unfiltered []
                           (if-let [line (read-line)]
                             (lazy-cat (split (trim line) #"[\s]+")
                                       (read-unfiltered))))]
    (filter seq (read-unfiltered))))



(defn add-map  [markov-map prefix-plus]
  (let [prefix (vec (butlast prefix-plus))]
    (assoc markov-map prefix (conj (markov-map prefix) (last prefix-plus)))))

(defn build-map [prefix-len col]
  (reduce add-map {} (partition (inc prefix-len) 1
                                (concat (repeat prefix-len nil) (col) [nil]))))

(defn generate [markov-map prefix]
  (let [suffixes (markov-map (vec prefix))]
    (if-let [suffix (nth suffixes (rand-int (count suffixes)))]
      (lazy-cat [suffix] (generate markov-map (concat (rest prefix) [suffix]))))))

(defn word-monkey
  ([maxgen] (word-monkey maxgen 2))

  ([maxgen prefix-len] 
     (let [markov-map (build-map prefix-len read-words)]
       (dorun (dec maxgen)
	      (map println (generate markov-map (repeat prefix-len nil)))))))

(defn char-monkey [file maxgen prefix-len]




  (let [markov-map (build-map prefix-len (fn [] (slurp file)))]
    (dorun (dec maxgen)
	   (map print (generate markov-map (repeat prefix-len nil))))))
	   













>
>
|
>

>
>
>
|

|
<
|
<
>
>











|



|
>
|
|



<
>
>
>
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13

14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;;;; A variable-order word monkey (markov chain of words.

(ns monkey (:gen-class)
    (:use [clojure.contrib.duck-streams :only (reader)])
    (:use [clojure.contrib.str-utils2 :only (split blank?)])
    (:use [clojure.contrib.command-line :only (with-command-line)]))

(def *maxgen* 1000)	; default maximum  number of objects to generate
(def *prefix-len* 2)	; default prefix length

(defn read-words [file]
  (letfn [(read-unfiltered []
                           (when-let [line (read-line)]

                             (concat (split line #"[\s]+") (read-unfiltered))))]

    (with-open [rdr (reader file)] 
      (binding [*in* rdr] (remove blank? (read-unfiltered))))))

(defn add-map  [markov-map prefix-plus]
  (let [prefix (vec (butlast prefix-plus))]
    (assoc markov-map prefix (conj (markov-map prefix) (last prefix-plus)))))

(defn build-map [prefix-len col]
  (reduce add-map {} (partition (inc prefix-len) 1
                                (concat (repeat prefix-len nil) (col) [nil]))))

(defn generate [markov-map prefix]
  (let [suffixes (markov-map (vec prefix))]
    (when-let [suffix (nth suffixes (rand-int (count suffixes)))]
      (lazy-cat [suffix] (generate markov-map (concat (rest prefix) [suffix]))))))

(defn word-monkey
  ([file] (word-monkey file *prefix-len* *maxgen*))
  ([file prefix-len] (word-monkey file prefix-len *maxgen*))
  ([file prefix-len maxgen] 
     (let [markov-map (build-map prefix-len (fn [] (read-words file)))]
       (dorun (dec maxgen)
	      (map println (generate markov-map (repeat prefix-len nil)))))))


(defn character-monkey
  ([file] (character-monkey file *prefix-len* *maxgen*))
  ([file prefix-len] (character-monkey file prefix-len *maxgen*))
  ([file prefix-len maxgen]
     (let [markov-map (build-map prefix-len (fn [] (slurp file)))]
       (dorun (dec maxgen)
	      (map print (generate markov-map (repeat prefix-len nil)))))))

(defn -main [& arguments]
  (with-command-line arguments
      "Run a character or word monkey."
      [[char-monkey? c? "Run a character monkey instead of a word monkey."]
       args]
    (if (< 0 (count args) 4)
      (apply (if char-monkey? character-monkey word-monkey)
	     (cons (first args) (map #(Integer/parseInt %) (rest args))))
      (binding [*out* *err*]
	(println "Usage: monkey [-c|--char-monkey] file [prefix-len [maxout]]"))))
  (flush))