Last active
July 11, 2017 16:13
-
-
Save boogie666/e9e7589c16acaa139d5ddfb6f5132dc7 to your computer and use it in GitHub Desktop.
a awesomely cool sentence generator
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-text.core | |
(:require [clojure.string :as str])) | |
(defn spliter [str] | |
(mapv str/lower-case (str/split str #"\s"))) | |
(defn markov-chain [n-gram-size words] | |
(loop [m-chain {} | |
words words] | |
(let [n-gram (take n-gram-size words) | |
next (nth words n-gram-size nil) | |
chain (or (m-chain n-gram) [])] | |
(if next | |
(recur (assoc m-chain n-gram (dedupe (conj chain next))) | |
(rest words)) | |
m-chain)))) | |
(defn markov-walker [starter chain] | |
(loop [sentence [(str/join " " starter)] | |
current starter] | |
(let [next (rand-nth (chain current))] | |
(if (or (str/includes? next ".")) | |
(str (str/join " " sentence) " " next) | |
(recur (conj sentence next) (list (last current) next)))))) | |
; (def corpus "The cat isn't sleeping, but the cat is definetly not listening.") | |
(->> (slurp "corpus.txt") | |
spliter | |
(markov-chain 2) | |
;make sure the "some" and "word" are actuall bits in your markov chain otherwise it will npe :) | |
(markov-walker (list "some" "word"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment