More WGU code

main
Eric Ihli 3 years ago
parent 5dbc532506
commit 52ecbe1bb6

@ -10,7 +10,8 @@
[com.owoga.phonetics.stress-manip :as stress-manip] [com.owoga.phonetics.stress-manip :as stress-manip]
[clojure.math.combinatorics :as combinatorics] [clojure.math.combinatorics :as combinatorics]
[com.owoga.prhyme.nlp.core :as nlp] [com.owoga.prhyme.nlp.core :as nlp]
[com.owoga.prhyme.data-transform :as data-transform])) [com.owoga.prhyme.data-transform :as data-transform]
[com.owoga.trie :as trie]))
(defn gen-artist [] (defn gen-artist []
(->> [(rand-nth (seq dict/adjectives)) (->> [(rand-nth (seq dict/adjectives))
@ -322,6 +323,7 @@
) )
(defn rhymes-by-quality (defn rhymes-by-quality
"Returns rhyming word, pronunciation, rhyme quality, and n-gram frequency."
[seed-phrase] [seed-phrase]
(->> seed-phrase (->> seed-phrase
(prhyme/phrase->all-phones) (prhyme/phrase->all-phones)
@ -345,16 +347,64 @@
[phones1 (sort-by (fn [[_ _ quality]] [phones1 (sort-by (fn [[_ _ quality]]
(- quality)) (- quality))
words)])) words)]))
(mapcat second) (reduce
(fn convert-to-hashmap [acc [pronunciation rhyming-words]]
(reduce (fn [acc [phones word rhyme-quality]]
(assoc-in acc [pronunciation phones] {:word word :rhyme-quality rhyme-quality}))
acc
rhyming-words))
{})
#_#_#_#_(mapcat second)
(sort-by #(- (nth % 2))) (sort-by #(- (nth % 2)))
(take 20) (take 20)
(map second))) (map second)))
(comment (comment
(rhymes-by-quality "bother me") (rhymes-by-quality "boss hog")
) )
(defn add-frequency-to-rhymes
[rhymes trie database]
(reduce
(fn [acc [pronunciation rhyming-words]]
(reduce
(fn [acc [phones word]]
(assoc-in acc [pronunciation phones :freq] (second (get trie [(database (:word word))]))))
acc
rhyming-words))
rhymes
rhymes))
(defn rhymes-with-quality-and-frequency
[phrase]
(-> (rhymes-by-quality phrase)
(add-frequency-to-rhymes models/markov-trie models/database)))
(comment
(-> (rhymes-by-quality "boss hog")
(add-frequency-to-rhymes models/markov-trie models/database))
{("B" "AA1" "S" "HH" "AA1" "G")
{("D" "EH1" "M" "AH0" "G" "AA2" "G")
{:word "demagogue", :rhyme-quality 1, :freq 20},
("B" "AE1" "K" "L" "AA2" "G") {:word "backlog", :rhyme-quality 2, :freq 1},
("HH" "EH1" "JH" "HH" "AA2" "G")
{:word "hedgehog", :rhyme-quality 2, :freq 2},
,,,
("AA1" "G") {:word "og", :rhyme-quality 4, :freq 134},
("P" "R" "OW1" "L" "AA0" "G")
{:word "prologue", :rhyme-quality 2, :freq 25}},
("B" "AO1" "S" "HH" "AA1" "G")
{("D" "EH1" "M" "AH0" "G" "AA2" "G")
{:word "demagogue", :rhyme-quality 1, :freq 20},
("B" "AE1" "K" "L" "AA2" "G") {:word "backlog", :rhyme-quality 2, :freq 1},
("HH" "EH1" "JH" "HH" "AA2" "G")
{:word "hedgehog", :rhyme-quality 2, :freq 2},
,,,
("P" "R" "OW1" "L" "AA0" "G") {:word "prologue", :rhyme-quality 2, :freq 25}}}
)
(defn open-nlp-perplexity (defn open-nlp-perplexity
"Returns the perplexity of the parse tree using OpenNLP. "Returns the perplexity of the parse tree using OpenNLP.
This is an alternative to the perplexity of the Markov model. This is an alternative to the perplexity of the Markov model.

@ -23,7 +23,9 @@
(defmethod ig/init-key ::handler [_ {:keys [router]}] (defmethod ig/init-key ::handler [_ {:keys [router]}]
(http/ring-handler (http/ring-handler
router router
(ring/create-default-handler) (ring/routes
(ring/redirect-trailing-slash-handler)
(ring/create-default-handler))
{:executor sieppari/executor})) {:executor sieppari/executor}))
(defn home-handler (defn home-handler
@ -298,3 +300,16 @@
(views/show-rhyme-suggestion (views/show-rhyme-suggestion
request request
suggestions))}))) suggestions))})))
(defn rhymes-with-quality-and-frequency [db cache]
(fn [request]
(let [target (-> request :params :rhyme-target)]
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/wrap-with-js
{:db db
:request request
:opts {}}
(views/rhymes-with-quality-and-frequency
request
(linguistics/rhymes-with-quality-and-frequency target)))})))

@ -35,9 +35,13 @@
:get {:handler (handlers/artist-get-handler db)}}]] :get {:handler (handlers/artist-get-handler db)}}]]
["/assets/*" handlers/file-handler] ["/assets/*" handlers/file-handler]
["/wgu" ["/wgu"
{:name ::wgu [""
:get {:handler (handlers/wgu db cache)} {:name ::wgu
:post {:handler (handlers/lyric-suggestions db cache)}}] :get {:handler (handlers/wgu db cache)}
:post {:handler (handlers/lyric-suggestions db cache)}}]
["/rhyme"
{:name ::rhyme
:get {:handler (handlers/lyric-suggestions db cache)}}]]
["/.well-known/*" (ring/create-file-handler ["/.well-known/*" (ring/create-file-handler
{:root "resources/public/.well-known"})]]] {:root "resources/public/.well-known"})]]]
(timbre/info "Starting router.") (timbre/info "Starting router.")

@ -277,23 +277,59 @@
[:div line])]]))]) [:div line])]]))])
(defn wgu (defn wgu
[request] [request {:keys [rhymes rhyming-lyrics lyrics]}]
[:div [:div
[:h1 "WGU Capstone"] [:h1 "WGU Capstone"]
(form/form-to [:div
[:post (util/route-name->path [:h2 "Generate Rhyme"]
request (form/form-to
:com.darklimericks.server.router/wgu)] [:post (util/route-name->path
(form/label request
"rhyme-target" :com.darklimericks.server.router/wgu)]
"Target word or phrase for which to find rhyme suggestions") (form/label
" " "rhyme-target"
(form/text-field "Target word or phrase for which to find rhyme suggestions")
{:placeholder "instead of war on poverty"} " "
"rhyme-target") (form/text-field
(form/submit-button {:placeholder "instead of war on poverty"}
{:class "ml2"} "rhyme-target")
"Show rhyme suggestions")) (form/submit-button
{:class "ml2"}
"Show rhyme suggestions"))
(when rhymes
rhymes)]
[:div
[:h2 "Generate Rhyming Lyric"]
(form/form-to
[:post (util/route-name->path
request
:com.darklimericks.server.router/wgu)]
(form/label
"rhyming-lyric-target"
"Target word or phrase for which to find a rhyming lyric")
" "
(form/text-field
{:placeholder "instead of war on poverty"}
"rhyming-lyric-target")
(form/submit-button
{:class "ml2"}
"Show rhyming lyrics suggestions"))]
[:div
[:h2 "Generate Lyrics"]
(form/form-to
[:post (util/route-name->path
request
:com.darklimericks.server.router/wgu)]
(form/label
"lyric-target"
"Seed word or phrase from which to generate lyric")
" "
(form/text-field
{:placeholder "instead of war on poverty"}
"lyric-target")
(form/submit-button
{:class "ml2"}
"Show lyrics suggestions"))]
[:div#myChart] [:div#myChart]
[:iframe {:src "/assets/README_WGU.htm" [:iframe {:src "/assets/README_WGU.htm"
:style "background-color: white; width: 100%; height: 760px;"}]]) :style "background-color: white; width: 100%; height: 760px;"}]])
@ -301,13 +337,17 @@
(defn lyric-suggestions (defn lyric-suggestions
[request suggestions] [request suggestions]
[:div [:div
(wgu request) (wgu request {:rhymes (for [suggestion suggestions]
(for [suggestion suggestions] [:div suggestion])})])
[:div suggestion])])
(defn show-rhyme-suggestion (defn show-rhyme-suggestion
[request suggestions] [request suggestions]
[:div [:div
(wgu request) (wgu
(for [[suggestion p1 freq _ p2 quality] suggestions] request
[:div (string/join " - " [suggestion freq p1 p2])])]) {:rhymes
(for [[suggestion p1 freq _ p2 quality] suggestions]
[:div (string/join " - " [suggestion freq p1 p2])])})])
(defn rhymes-with-quality-and-frequency
[])

Loading…
Cancel
Save