- Source http://tinyurl.com/alice-clj
-
-
Save skuro/9f31097cd20573f4fe3334981070a350 to your computer and use it in GitHub Desktop.
markov-chain-meetup-dojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
into it llustration you can t s the | |
ho cares for he fee as set forth | |
or the court round | |
t seem to feel which seemed went on a | |
of her lice felt quite | |
llustration of white iterary | |
than a ventured perhaps not claim a | |
archbishop find another moment hat size round | |
hite he said ound | |
times as well as she did the animals that lay on | |
them all the begged the ouse | |
of or immediate adoption of the lice | |
by e ooks by that they re a tidy | |
ouse in any work providing | |
uchess cross and one | |
quiver all the little quicker | |
in a minute or really what does it was labeled | |
of hy only wish to open any part | |
it quite a bit hurt the in rench | |
as she heard this very good naturedly began running about for | |
that it at her sister roofreading | |
sentence set out of time | |
llustration arroll | |
over with his e ook of footsteps | |
and conquest lice to llustration | |
provisions from the face and rapped loudly | |
dear he game said | |
lice sir roduced by the | |
she ran out of ouse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns markov-dojo.core | |
(:require [clojure.java.io :as io] | |
[clojure.string :as s])) | |
(defn load-resource | |
[res-name] | |
(slurp (io/resource res-name))) | |
(def end-of-sentence #".*[.;:?!]$") | |
(defn split-by-word | |
[txt] | |
(s/split txt #"[ -_]")) | |
(defn foo | |
"I don't do a whole lot." | |
[x] | |
(println x "Hello, World!")) | |
(defn pairs | |
[s] | |
(map vector s (drop 1 s))) | |
(defn comb | |
[acc [a b]] | |
(let [merge-fn (fn [one two] (apply conj one two))] | |
(merge-with merge-fn acc {a [b]}))) | |
(defn markov-chain | |
[pairs] | |
(reduce comb {} pairs)) | |
(defn random-token | |
[vals] | |
(nth vals (rand-int (count vals)))) | |
(defn end-of-sentence? | |
[token] | |
(re-matches end-of-sentence token)) | |
(defn random-sentence | |
[chain max-tokens] | |
(let [tokens (keys chain)] | |
(loop [sentence [] | |
token (random-token tokens)] | |
(let [next-token (random-token (get chain token)) | |
next-sentence (conj sentence next-token)] | |
(if (or (end-of-sentence? next-token) | |
(<= max-tokens (count next-sentence))) | |
(s/join " " sentence) | |
(recur next-sentence next-token)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment