Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7ed79bb8a9
Fetching contributors…

Cannot retrieve contributors at this time

file 35 lines (32 sloc) 1.231 kb
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
(ns marky.generate
  (:require [cbdrawer.view :as cbv]
            [cbdrawer.client :as cb]))

(defn pick [wset]
  (when-not (empty? wset)
    (let [tot (reduce + (map :value wset))]
      (loop [i (rand tot)
             [{weight :value :as item} & more] (seq wset)]
        (if (>= weight i)
          item
          (recur (- i weight) more))))))

(defn next-word [viewuri word]
  (let [start [word]
        end [word {}]
        candidates (cbv/view-seq viewuri
                                 {:startkey start
                                  :endkey end
                                  :reduce true
                                  :group_level 2
                                  :stale false})
        selected (pick candidates)]
    (or (last (:key selected))
        (recur viewuri nil))))

(defn generate-text [cfg length]
  (let [fact (cb/factory (:bucket cfg) (:pass cfg) (:cburl cfg))
        capis (cb/capi-bases fact)
        viewuri (cbv/view-url capis "marky" "marky")]
    (with-out-str (loop [i length
                         w nil]
                    (let [word (next-word viewuri w)]
                      (print (str word " "))
                      (when (<= 0 i) (recur (- i (count w)) word)))))))
Something went wrong with that request. Please try again.