Created
January 26, 2014 22:42
-
-
Save jackrusher/8640437 to your computer and use it in GitHub Desktop.
Finding rhymes with clojure and CMU's pronunciation dictionary. Tested with this file, minus comments, as 'cmudict.txt': http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict.0.7a
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
(def rhyme-txt | |
(map #(string/split % #"[ ]+") (string/split-lines (slurp "cmudict.txt")))) | |
(def word-to-rhyme | |
(reduce (fn [m [word & rhyme]] | |
(assoc m | |
(string/lower-case word) | |
(mapv #(keyword (string/replace %1 #"[0-9]" "")) (reverse rhyme)))) | |
{} rhyme-txt)) | |
(def rhyme-to-word | |
(reduce (fn [m [word rhyme]] (assoc-in m rhyme { :terminate word })) {} word-to-rhyme)) | |
(defn get-deepest-values [k] | |
(if (string? k) [k] (mapcat get-deepest-values (vals k)))) | |
(get-deepest-values (get-in rhyme-to-word (take 5 (word-to-rhyme "connection")))) | |
;; => ("erection" "direction" "correction" "collection" "inflection" | |
;; "preelection" "circumspection" "introspection" "imperfection" | |
;; "perfection" "midsection" "transection" "connection" "protection") | |
(def byron-bigrams [["soul" "wears"] ["light" "of"] ["moon" "be"] | |
["outwears" "its"] ["the" "soul"] ["night" "though"] ["heart" "must"] | |
["out" "the"] ["be" "still"] ["love" "itself"] ["the" "light"] | |
["pause" "to"] ["yet" "we"] ["for" "loving"] ["day" "returns"] | |
["the" "night"] ["too" "soon"] ["for" "the"] ["a" "roving"] | |
["will" "go"] ["loving" "and"] ["sword" "outwears"] ["to" "breathe"] | |
["breast" "and"] ["still" "as"] ["so" "late"] ["the" "day"] | |
["was" "made"] ["the" "sword"] ["more" "a"] ["soon" "yet"] | |
["must" "pause"] ["roving" "by"] ["the" "moon"] ["returns" "too"] | |
["itself" "have"] ["night" "was"] ["into" "the"] ["we" "will"] | |
["breathe" "and"] ["sheath" "and"] ["so" "we"] ["roving" "so"] | |
["no" "more"] ["made" "for"] ["the" "heart"] ["though" "the"] | |
["go" "no"] ["late" "into"] ["wears" "out"] ["and" "the"] ["of" "the"] | |
["by" "the"] ["and" "love"] ["the" "breast"] ["heart" "be"] | |
["as" "loving"] ["its" "sheath"]]) | |
(group-by (comp (partial take 2) word-to-rhyme last) byron-bigrams) | |
;; {(:L :IH) [["be" "still"] ["we" "will"]], | |
;; () [["sword" "outwears"]], | |
;; (:N :UW) [["too" "soon"] ["the" "moon"]], | |
;; (:NG :IH) [["for" "loving"] ["a" "roving"] ["as" "loving"]], | |
;; (:OW :S) [["roving" "so"]], | |
;; (:OW :DH) [["night" "though"]], | |
;; (:IY :W) [["yet" "we"] ["so" "we"]], | |
;; (:T :S) [["heart" "must"] ["the" "breast"]], | |
;; (:D :EY) [["was" "made"]], | |
;; (:T :AY) [["the" "light"] ["the" "night"]], | |
;; (:T :R) [["the" "heart"]], | |
;; (:OW :N) [["go" "no"]], | |
;; (:T :EH) [["soon" "yet"]], | |
;; (:EY) [["more" "a"]], | |
;; (:Z :R) [["soul" "wears"]], | |
;; (:R :AO) [["no" "more"] ["made" "for"]], | |
;; (:Z :AA) [["night" "was"]], | |
;; (:AY :B) [["roving" "by"]], | |
;; (:V :AH) [["light" "of"] ["and" "love"]], | |
;; (:Z :N) [["day" "returns"]], | |
;; (:V :AE) [["itself" "have"]], | |
;; (:D :R) [["the" "sword"]], | |
;; (:Z :AE) [["still" "as"]], | |
;; (:OW :G) [["will" "go"]], | |
;; (:TH :IY) [["its" "sheath"]], | |
;; (:F :L) [["love" "itself"]], | |
;; (:D :N) | |
;; [["loving" "and"] | |
;; ["breast" "and"] | |
;; ["breathe" "and"] | |
;; ["sheath" "and"]], | |
;; (:Z :AO) [["must" "pause"]], | |
;; (:T :AW) [["wears" "out"]], | |
;; (:IY :B) [["moon" "be"] ["heart" "be"]], | |
;; (:DH :IY) [["to" "breathe"]], | |
;; (:UW :T) [["pause" "to"] ["returns" "too"] ["late" "into"]], | |
;; (:T :EY) [["so" "late"]], | |
;; (:AH :DH) | |
;; [["out" "the"] | |
;; ["for" "the"] | |
;; ["into" "the"] | |
;; ["though" "the"] | |
;; ["and" "the"] | |
;; ["of" "the"] | |
;; ["by" "the"]], | |
;; (:L :OW) [["the" "soul"]], | |
;; (:S :T) [["outwears" "its"]], | |
;; (:EY :D) [["the" "day"]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment