- Source http://tinyurl.com/alice-clj
Last active
March 9, 2017 08:12
-
-
Save Gonzih/37e7353bcf300b626c5afd9ec16e404b to your computer and use it in GitHub Desktop.
markov-chain-meetup-dojo
This file contains hidden or 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-chain.core | |
(:require [clojure.java.io :as io] | |
[clojure.string :as string])) | |
(defn read-input [] | |
(-> "input" | |
io/resource | |
slurp | |
(string/split #"[\n\r\s]+"))) | |
(defn generate-model [words] | |
(reduce (fn [m [f s]] (update m f conj s)) | |
{} | |
(partition 2 1 words))) | |
(defn generate-sentence [start-word model] | |
(loop [output "" | |
next-word start-word] | |
(if (= \. (last next-word)) | |
(str output " " next-word) | |
(recur (format "%s %s" output next-word) | |
(rand-nth (get model next-word)))))) | |
(->> (read-input) | |
(map #(string/replace % #"[()\"]*" "")) | |
(filter #(> (count %) 0 )) | |
generate-model | |
(generate-sentence "Alice") | |
string/triml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/little-arhat/dc8cc27c2eee11ace5e97aed60227212