Misc more markov gen

main
Eric Ihli 4 years ago
parent b349e7af2c
commit b766f836d3

@ -562,8 +562,10 @@
(defn rhyme-choices (defn rhyme-choices
[{:keys [flex-rhyme-trie database] :as context} phrase] [{:keys [flex-rhyme-trie database] :as context} phrase]
(if (string? phrase)
(let [phones (phrase->flex-rhyme-phones phrase)] (let [phones (phrase->flex-rhyme-phones phrase)]
(get flex-rhyme-trie phones))) (get flex-rhyme-trie phones))
(get flex-rhyme-trie phrase)))
(defn exclude-non-rhymes-from-choices (defn exclude-non-rhymes-from-choices
"Removes any choice that includes the last "Removes any choice that includes the last
@ -571,11 +573,15 @@
Also removes beginning and end of sentence markers (1 and 38 in the database)." Also removes beginning and end of sentence markers (1 and 38 in the database)."
[{:keys [database]} phrase choices] [{:keys [database]} phrase choices]
(if (string? phrase)
(let [word-id (database (last (string/split phrase #" ")))] (let [word-id (database (last (string/split phrase #" ")))]
(remove (remove
(fn [child] (fn [child]
(or (= ((comp first second) child) word-id) (or (= ((comp first second) child) word-id)
(#{1 38} ((comp first first) child)))) (#{1 38} ((comp first first) child))))
choices))
(remove
(fn [child] (#{1 38} ((comp first first) child)))
choices))) choices)))
(defn exclude-non-english-phrases-from-choices (defn exclude-non-english-phrases-from-choices
@ -608,6 +614,7 @@
"Gets from a rhyme-trie a rhyming n-gram based on the "Gets from a rhyme-trie a rhyming n-gram based on the
weighted selection from their frequencies." weighted selection from their frequencies."
[{:keys [flex-rhyme-trie database] :as context} phrase] [{:keys [flex-rhyme-trie database] :as context} phrase]
(if (string? phrase)
(let [phones (phrase->flex-rhyme-phones phrase) (let [phones (phrase->flex-rhyme-phones phrase)
;; Exclude the last word. Don't rhyme kodak with kodak. ;; Exclude the last word. Don't rhyme kodak with kodak.
word-id (database (first (string/split phrase #" "))) word-id (database (first (string/split phrase #" ")))
@ -618,12 +625,20 @@
choice (math/weighted-selection choice (math/weighted-selection
(comp second second) (comp second second)
choices)] choices)]
(map database (first choice)))) (map database (first choice)))
(let [phones phrase
choices (get flex-rhyme-trie phones)
choice (math/weighted-selection
(comp second second)
choices)]
(map database (first choice)))))
(comment (comment
(get-flex-rhyme @context "bother me") (get-flex-rhyme @context "bother me")
(phrase->flex-rhyme-phones "bother me")
(get-flex-rhyme @context ["IY" "ER" "AA"])
) )
(defn get-next-markov (defn get-next-markov
[{:keys [trie database] :as context} seed] [{:keys [trie database] :as context} seed]
(let [seed (take-last 3 seed) (let [seed (take-last 3 seed)
@ -679,7 +694,7 @@
(recur (conj seed (get-next-markov context seed))) (recur (conj seed (get-next-markov context seed)))
(map database seed)))) (map database seed))))
(defn take-words-amounting-to-more-at-least-n-syllables (defn take-words-amounting-to-at-least-n-syllables
[phrase n] [phrase n]
(letfn [(phones [word] (letfn [(phones [word]
[word (first (owoga.phonetics/get-phones word))]) [word (first (owoga.phonetics/get-phones word))])
@ -698,6 +713,17 @@
(map first) (map first)
(string/join " ")))) (string/join " "))))
(defn take-n-syllables
[phrase n]
(if (string? phrase)
(->> phrase
(phrase->flex-rhyme-phones)
(take n)
(reverse))
(take-last n phrase)))
(take-n-syllables "bother me" 2)
(defn valid-english-sentence? (defn valid-english-sentence?
[phrase] [phrase]
(let [words (string/split #" " phrase)] (let [words (string/split #" " phrase)]
@ -728,17 +754,20 @@
(exclude-non-rhymes-from-choices context target-rhyme) (exclude-non-rhymes-from-choices context target-rhyme)
(exclude-non-english-phrases-from-choices context))] (exclude-non-english-phrases-from-choices context))]
(if (empty? choices) (if (empty? choices)
(recur (string/join " " (rest (string/split target-rhyme #" ")))) (recur (if (string? target-rhyme)
(rest (phrase->flex-rhyme-phones context target-rhyme))
(rest target-rhyme)))
choices)))) choices))))
(defn generate-n-syllable-sentence-rhyming-with (defn generate-n-syllable-sentence-rhyming-with
[context target-phrase n] [context target-phrase n-gram-rank target-rhyme-syllable-count target-sentence-syllable-count]
(if (string? target-phrase)
(let [target-phrase-words (string/split target-phrase #" ") (let [target-phrase-words (string/split target-phrase #" ")
reversed-target-phrase (string/join " " (reverse target-phrase-words)) reversed-target-phrase (string/join " " (reverse target-phrase-words))
target-rhyme target-rhyme
(->> (take-words-amounting-to-more-at-least-n-syllables (->> (take-words-amounting-to-at-least-n-syllables
reversed-target-phrase reversed-target-phrase
5) target-rhyme-syllable-count)
(#(string/split % #" ")) (#(string/split % #" "))
reverse reverse
(string/join " ")) (string/join " "))
@ -747,16 +776,73 @@
(choice->n-gram context) (choice->n-gram context)
(string/join " "))] (string/join " "))]
(loop [phrase rhyming-n-gram] (loop [phrase rhyming-n-gram]
(if (<= n (syllable-count-phrase phrase)) (if (<= target-sentence-syllable-count (syllable-count-phrase phrase))
phrase
(recur
(str (get-next-markov-from-phrase-backwards context phrase n-gram-rank)
" "
phrase)))))
(let [target-rhyme
(->> (take-n-syllables target-phrase target-rhyme-syllable-count))
rhyming-n-gram (->> (rhyming-n-gram-choices context target-rhyme)
(weighted-selection-from-choices)
(choice->n-gram context)
(string/join " "))]
(loop [phrase rhyming-n-gram]
(if (<= target-sentence-syllable-count (syllable-count-phrase phrase))
phrase phrase
(recur (recur
(str (get-next-markov-from-phrase-backwards context phrase 5) (str (get-next-markov-from-phrase-backwards context phrase n-gram-rank)
" " " "
phrase)))))) phrase)))))))
(defn generate-haiku
[seed]
(let [haiku (cons
seed
(map
#(generate-n-syllable-sentence-rhyming-with
@context
(take 3 (phrase->flex-rhyme-phones seed))
3 3 %)
[5 3]))]
(lazy-seq
(cons
haiku
(generate-haiku
(last haiku))))))
(comment
(defn valid-haiku [haiku]
(and
(or (every? nlp/valid-sentence? haiku)
(->> haiku
(mapcat #(string/split % #" "))
(every? dict/cmu-with-stress-map)))
(->> haiku
(map #(string/split % #" "))
(map last)
(apply distinct?))))
(->> (generate-haiku "technology")
(filter valid-haiku)
(map (partial string/join "\n"))
(map #(vector % (sha256 %)))
(map (fn [[haiku sha]]
(println haiku)
(println sha)
(println))))
)
(map
#(generate-n-syllable-sentence-rhyming-with
@context
(reverse (take 4 (phrase->flex-rhyme-phones "dawn of skynet")))
3 3 %)
[5 3])
(generate-n-syllable-sentence-rhyming-with @context "instead of war on poverty" 8)
((@context :database) "poverty")
(defn amul8 (defn amul8
([sentence] ([sentence]
(->> (amulate (string/split sentence #" ")) (->> (amulate (string/split sentence #" "))
@ -787,14 +873,12 @@
((fn [word-phones] ((fn [word-phones]
(loop [word-phones word-phones (loop [word-phones word-phones
seed []] seed []]
(println (mapcat second seed))
(if (< 2 (count (mapcat second seed))) (if (< 2 (count (mapcat second seed)))
(string/join (string/join
" " " "
(reverse (map first seed))) (reverse (map first seed)))
(recur (rest word-phones) (recur (rest word-phones)
(conj seed (first word-phones))))))))] (conj seed (first word-phones))))))))]
(println next-seed)
(lazy-seq (lazy-seq
(cons next-sentence (continuously-amulate next-seed))))) (cons next-sentence (continuously-amulate next-seed)))))
@ -810,8 +894,7 @@
(map #(vector % (sha256 %))) (map #(vector % (sha256 %)))
(map (map
(fn [[text sha]] (fn [[text sha]]
[text sha (re-matches #"8{4}" sha)])) [text sha (re-matches #"8{4}" sha)])))
(map println))
(dict/cmu-with-stress-map ) (dict/cmu-with-stress-map )
(repeatedly (repeatedly

Loading…
Cancel
Save