diff --git a/example/real_estate.clj b/example/real_estate.clj deleted file mode 100644 index 11590b2..0000000 --- a/example/real_estate.clj +++ /dev/null @@ -1,16 +0,0 @@ -(ns example.real-estate - (:require [com.owoga.frp.infrastructure :as frp])) - -(frp/defrelvar Offer - #(string? (:address %)) - #(number? (:offer-price %)) - #(inst? (:offer-date %)) - #(string? (:bidder-name %)) - #(string? (:bidder-address %))) - -(frp/defrelvar Property - #(string? (:address %)) - #(number? (:price %)) - #(string? (:photo %)) - #(string? (:agent-name %)) - #(inst? (:date-registered %))) diff --git a/src/com/owoga/frp/infrastructure.clj b/src/com/owoga/frp/infrastructure.clj deleted file mode 100644 index 7855aa6..0000000 --- a/src/com/owoga/frp/infrastructure.clj +++ /dev/null @@ -1,98 +0,0 @@ -(ns com.owoga.frp.infrastructure - (:require [clojure.set :as set]) - (:refer-clojure :exclude [extend])) - -(defprotocol PRelVar - (extend [this extensions & constraints]) - (restrict [this criteria & constraints]) - (project [this attributes & constraints]) - (product [this relvar & constraints]) - (union [this relvar & constraints]) - (intersection [this relvar & contstraints]) - (difference [this relvar & constraints]) - (join [this relvar & constraints]) - (divide [this relvar & constraints]) - (rename [this renames & constraints])) - -(defprotocol PRelations - (load! [this relations]) - (insert! - [this relation] - [this & relations]) - (delete! [this & relations]) - (update! [this old-relation new-relation]) - (clear! [this])) -(declare extend-) -(declare project-) -(declare restrict-) - -(deftype RelVar [relvar xf constraints] - PRelVar - (extend - [this extensions & constraints] - (extend- this extensions constraints)) - (project - [this attributes & constraints] - (project- this attributes constraints)) - (restrict - [this criteria & constraints] - (restrict- this criteria constraints)) - - clojure.lang.IDeref - (deref [_] (into #{} xf @relvar))) - -(deftype BaseRelVar [relvar-name store constraints] - PRelVar - (extend - [this extensions & constraints] - (extend- this extensions constraints)) - (project - [this attributes & constraints] - (project- this attributes constraints)) - (restrict - [this criteria & constraints] - (restrict- this criteria constraints)) - - PRelations - (load! [this relations] (reset! store relations)) - (insert! - [this relation] - (let [new-relation (conj @store relation)] - (run! - (fn [constraint] - (when (not (every? true? (constraint new-relation))) - (throw (ex-info "Constraint Exception" {})))) - constraints) - (reset! store new-relation))) - (insert! - [this & relations] - (let [new-relation (set/union @store (into #{} relations))] - (run! - (fn [constraint] - (when (not (every? true? (constraint new-relation))) - (throw (ex-info "Constraint Exception" {})))) - constraints) - (reset! store new-relation))) - - clojure.lang.IDeref - (deref [_] @store)) - -(defn extend- [relvar extensions constraints] - (let [xf (map (fn [element] - (map (fn [[k f]] - (assoc element k (f element))) - extensions)))] - (->RelVar relvar xf constraints))) - -(defn project- [relvar attributes constraints] - (->RelVar relvar (map #(select-keys % attributes)) constraints)) - -(defn restrict- [relvar criteria constraints] - (->RelVar relvar (filter criteria) constraints)) - -(def *constraints* (atom {})) - -(defmacro defrelvar - [relvar-name & constraints] - (swap! *constraints* assoc-in [relvar-name :constraints] constraints) - `(->BaseRelVar '~relvar-name (atom #{}) [~@constraints])) diff --git a/src/com/owoga/prhyme/frp.clj b/src/com/owoga/prhyme/frp.clj deleted file mode 100644 index 14c6859..0000000 --- a/src/com/owoga/prhyme/frp.clj +++ /dev/null @@ -1,360 +0,0 @@ -(ns com.owoga.prhyme.frp - (:require [clojure.java.io :as io] - [clojure.string :as string] - [clojure.set :as set] - [com.owoga.prhyme.data.thesaurus :refer [thesaurus]] - [com.owoga.prhyme.core :as p] - [com.owoga.prhyme.util :as u] - [com.owoga.prhyme.syllabify :as s])) - -(def dictionary - (line-seq (io/reader (io/resource "cmudict_SPHINX_40")))) - - - -(def words (->> dictionary - (map u/prepare-word) - (map p/cmu->prhyme))) - -(def popular-dict - (set (line-seq (io/reader (io/resource "popular.txt"))))) - -(def popular (filter #(get popular-dict (string/lower-case (:word %))) words)) - -(defn merge-phrase-words - "Given multiple `Word`, like the words for 'well off', create a single `Word` - that is syllabified as ('well' 'off') rather than as the combined ('weh' - 'loff'). Useful for finding single-word rhymes of multiple-word targets. - - An example: 'war on crime' -> 'turpentine'." - [phrase phrase-words] - (loop [merged (first phrase-words) - phrase-words (rest phrase-words)] - (cond - (and (empty? phrase-words) (empty? merged)) nil - (empty? phrase-words) (assoc merged :word phrase) - :else (recur (-> merged - (assoc :syllables (concat (:syllables merged) - (:syllables (first phrase-words)))) - (assoc :syllable-count (+ (:syllable-count merged) - (:syllable-count (first phrase-words)))) - (assoc :rimes (concat (:rimes merged) - (:rimes (first phrase-words)))) - (assoc :onsets (concat (:onsets merged) - (:onsets (first phrase-words)))) - (assoc :nuclei (concat (:nuclei merged) - (:nuclei (first phrase-words))))) - (rest phrase-words))))) - -(defn partition-word [word] - (->> word - (:syllables) - (u/partitions))) - -(defn rimes [words target] - (into #{} - (filter (fn [{:keys [rimes]}] - (= (last rimes) (last (:rimes target)))) - words))) - -(defn onsets [words target] - (into #{} - (filter (fn [{:keys [onsets]}] - (= (first onsets) (first (:onsets target)))) - words))) - -(defn nuclei [words target] - (into #{} - (filter (fn [{:keys [nuclei]}] - (= (last nuclei) (last (:nuclei target)))) - words))) - -(defn consecutive-matching - "Returns the consecutive matching rhymes of type. - - Given words: - (D EY Z IY) and (K R EY Z IY) - - the following would be returned for each type: - rimes: 2, (((IY) (IY)) ((EY) (EY))) - rimes are matched in reverse order - onsets: 0 - nuclei: 2, (((EY) (EY)) ((IY) (IY))) - nuclei and onsets are matched in order - " - [a b type] - (let [a (if (#{:rimes :nuclei} type) (reverse (type a)) (type a)) - b (if (#{:rimes :nuclei} type) (reverse (type b)) (type b))] - (take-while (fn [[x y]] (= x y)) (map list a b)))) - -(defn sort-rhymes - "Sorts by the number of consecutive matching rimes, onsets, and nuclei of each - word." - [rhymes word] - (sort (fn [a b] - (> (apply - + - (map #(count (consecutive-matching a word %)) - [:rimes :onsets :nuclei])) - (apply - + - (map #(count (consecutive-matching b word %)) - [:rimes :onsets :nuclei])))) - rhymes)) - -(defn prhyme - "Finds rhymes in dictionary `words` of `word` with options - to match on rimes, onsets, and/or nuclei." - [words word] - (let [r (if (:rimes? word) (rimes words word) #{}) - o (if (:onsets? word) (onsets words word) #{}) - n (if (:nuclei? word) (nuclei words word) #{}) - all (set/union r o n)] - all)) - -(defn matching-syllable-count [n words] - (filter #(= n (:syllable-count %)) words)) - -(defn matching-synonyms [thesaurus target words] - (let [synonyms (get thesaurus target)] - (filter (fn [word] (some #(re-matches (re-pattern (str "(?i)" %)) (:word word)) synonyms)) - words))) - -(comment - (->> (p/cmu->prhyme ["foobar" "F" "UW" "B" "AA" "R"]) - (#(assoc % :rimes? true)) - (prhyme words) - (filter #(= (:syllable-count %) 2)) - (sort-by #(count - (consecutive-matching - % - (p/cmu->prhyme ["foobar" "F" "UW" "B" "AA" "R"]) - :rimes)))) - - (as-> (p/cmu->prhyme ["magic beam" "M" "AE" "J" "IH" "K" "B" "IY" "M"]) word - (into word {:rimes? true}) - (prhyme popular word) - (mapcat #(matching-synonyms thesaurus % word) - ["death" "evil" "satan" "devil" "sin" "bad" "hell" - "guts" "gore" "blood" "demon" "fear" "nightmare" - "distress" "corpse" "necrotic" "zombie" - "coma" "monster"])) - - (as-> (p/cmu->prhyme ["please turn" "P" "L" "IH" "Z" "T" "ER" "N"]) word - (into word {:rimes? true}) - (prhyme popular word) - (mapcat #(matching-synonyms thesaurus % word) - ["death" "evil" "satan" "devil" "sin" "bad" "hell" - "guts" "gore" "blood" "demon" "fear" "nightmare" - "distress" "corpse" "necrotic" "zombie" - "coma" "monster"])) - ) - - -(defn find-synonyms - ([thesaurus dict word] - (find-synonyms thesaurus dict 1 #{word} #{})) - ([thesaurus dict word degree] - (find-synonyms thesaurus dict degree #{word} #{})) - ([thesaurus dict degree words synonyms] - (cond - (= degree 0) synonyms - - (nil? (first words)) - (recur thesaurus - dict - (dec degree) - (into #{} (map #(string/lower-case (:word %)) synonyms)) - synonyms) - - :else - (recur thesaurus - dict - degree - (rest words) - (set/union - synonyms - (let [synonyms (->> (get thesaurus (first words)) - (map string/lower-case) - (into #{}))] - (->> dict - (filter #(synonyms (string/lower-case (:word %)))) - (into #{})))))))) - -(defn pprint-phrase [phrase-words] - (let [phrase-words (map #(if (empty? %) '("_") %) phrase-words) - max-len (apply max (map count phrase-words)) - words-cycles (map cycle phrase-words)] - (->> (map (partial take max-len) words-cycles) - (apply map vector)))) - -(defn pprint-list [phrase-words] - (let [phrase-words (map #(if (empty? %) '("") %) phrase-words) - max-word-lens (->> phrase-words - (map #(map count %)) - (map #(apply max %))) - max-rhyme-count (count (apply max-key count phrase-words)) - fmt-strs (->> max-word-lens - (map #(+ 3 %)) - (map #(format "%%-%ds" %))) - phrase-words (->> phrase-words - (map #(concat % (repeat ""))) - (map #(take max-rhyme-count %)) - (apply map vector) - (map (fn [words] - (->> (map vector fmt-strs words) - (map #(apply format %))))))] - phrase-words)) - -(defn pprint-table [phrase-words] - (let [phrase-words (map #(if (empty? %) '("") %) phrase-words) - max-word-lens (->> phrase-words - (map #(map count %)) - (map #(apply max %))) - max-rhyme-count (count (apply max-key count phrase-words)) - fmt-str (->> max-word-lens - (map #(+ 3 %)) - (map #(format "%%-%ds" %)) - (apply str)) - phrase-words (->> phrase-words - (map #(concat % (repeat ""))) - (map #(take max-rhyme-count %)) - (apply map vector) - (map #(apply format fmt-str %)) - (#(string/join "\n" %)))] - phrase-words)) - - -(defn words->rhyme [dict words] - (->> words - (map #(into % {:rimes? true})) - (map (fn [target] - (->> (prhyme dict target) - (map #(assoc % :target target))))) - (map (fn [rhyming-words] - (filter #(= (:syllable-count %) (:syllable-count (:target %))) - rhyming-words))) - (map (fn [rhyming-words] - (let [target (:target (first rhyming-words))] - (sort-rhymes rhyming-words target)))) - (map (fn [rhyming-words] - (map :word rhyming-words))))) - -(defn prhyme-1 [dict targets] - (words->rhyme dict targets)) - -(defn prhyme-many [dict phrase] - (let [syllable-partitions - (->> phrase - (:syllables (p/phrase->word dict phrase)) - (u/partitions) - (map (fn [part] - (map (fn [syllables] - (p/cmu->prhyme - (into - [(string/join " " (flatten (apply concat syllables)))] - (flatten syllables)))) - part))))] - (map (partial prhyme-1 dict) syllable-partitions))) - -(comment - (->> (prhyme-many popular "give him two lips like roses in clover") - (map (fn [rhymes] - (map #(take 20 %) rhymes))) - (map pprint-table) - (string/join "\n") - (println)) - - (let [phrase "give him two lips like roses in clover" - targets (->> phrase - (:syllables (phrase->word words phrase)) - (u/partitions) - (first) - (map (fn [syllables] - (make-word - (into - [(string/join " " (flatten (apply concat syllables)))] - (flatten syllables))))))] - targets - (->> targets - (map #(into % {:rimes? true})) - (map (fn [target] - (->> (prhyme popular target) - (map #(assoc % :target target))))) - (map (fn [rhyming-words] - (filter #(= (:syllable-count %) (:syllable-count (:target %))) - rhyming-words))) - (map (fn [rhyming-words] - (let [target (:target (first rhyming-words))] - (sort-rhymes rhyming-words target)))) - (map (fn [rhyming-words] - (map :word rhyming-words))) - (pprint-table) - (spit "rhymes.txt"))) - - (->> (phrase->word words "give") - (#(assoc % :rimes? true)) - (prhyme popular)) - (let [targets (map (partial phrase->word words) - (string/split - "give him two lips like roses in clover" - #" "))] - (->> targets - (map #(into % {:rimes? true})) - (map (fn [target] - (->> (prhyme popular target) - (map #(assoc % :target target))))) - (map (fn [rhyming-words] - (filter #(= (:syllable-count %) (:syllable-count (:target %))) - rhyming-words))) - (map (fn [rhyming-words] - (let [target (:target (first rhyming-words))] - (sort-rhymes rhyming-words target)))) - (map (fn [rhyming-words] - (map :word rhyming-words))) - (pprint-table) - (spit "rhymes.txt"))) - - (let [targets (map (partial phrase->word words) - (string/split - "then tell him that his lonesome nights are over" - #" "))] - (->> targets - (map #(into % {:rimes? true})) - (map (fn [target] - (->> (prhyme popular target) - (map #(assoc % :target target))))) - (map (fn [rhyming-words] - (filter #(= (:syllable-count %) (:syllable-count (:target %))) - rhyming-words))) - (map (fn [rhyming-words] - (map :word rhyming-words))) - (pprint-table) - (spit "rhymes.txt"))) - - (let [targets (map (partial phrase->word words) - ["please" "turn" "on" "your" "magic" "beam"]) - synonyms (into #{} (->> (mapcat #(find-synonyms thesaurus words % 2) - ["evil" "war" "death" "corpse"]) - (map :word)))] - (->> targets - (map #(into % {:rimes? true})) - (map (fn [target] - (->> (prhyme popular target) - (map #(assoc % :target target))))) - (map (fn [rhyming-words] - (filter #(= (:syllable-count %) (:syllable-count (:target %))) - rhyming-words))) - (map (fn [rhyming-words] - (filter #(synonyms (:word %)) rhyming-words))) - (map (fn [rhyming-words] - (map :word rhyming-words))) - (pprint-table) - (spit "rhymes.txt"))) - (s/syllabify ["IH" "N" "V" "AA" "L" "V" "Z"]) - (s/syllabify ["D" "EH" "B" "Y" "AH"]) - - (s/syllabify ["R" "AW" "N" "D" "M" "IY" "HH" "AA" "R" "T"]) - ;; => (("R" "AW" "N" "D") ("M" "IY") ("HH" "AA" "R" "T")) - (s/syllabify ["P" "AE" "D" "M" "AY"]) - (set/union (rimes words (make-word ["boat" "B" "OW" "T"])) - (onsets words (make-word ["ballboy" "D" "AH" "L" "B" "OY"])))) - diff --git a/src/com/owoga/prhyme/logic/core.clj b/src/com/owoga/prhyme/logic/core.clj deleted file mode 100644 index e76ae1d..0000000 --- a/src/com/owoga/prhyme/logic/core.clj +++ /dev/null @@ -1,158 +0,0 @@ -(ns com.owoga.prhyme.logic.core - (:refer-clojure :exclude [==]) - (:require [clojure.core.logic.fd :as fd] - [clojure.string :as string] - [clojure.core.logic.pldb :as db] - [com.owoga.prhyme.data.dictionary :as dict] - [com.owoga.prhyme.syllabify :as syllabify]) - (:use [clojure.core.logic])) - -(defn productsumo [vars dens sum] - (fresh [vhead vtail dhead dtail product run-sum] - (conde - [(emptyo vars) (== sum 0)] - [(conso vhead vtail vars) - (conso dhead dtail dens) - (fd/* vhead dhead product) - (fd/+ product run-sum sum) - (productsumo vtail dtail run-sum)]))) - -(defn change [amount denoms] - (let [dens (sort > denoms) - vars (repeatedly (count dens) lvar)] - (run* [q] - (== q (zipmap dens vars)) - (everyg #(fd/in % (fd/interval 0 amount)) vars) - (productsumo vars dens amount)))) - -(change 14 #{1 2 5 10}) - -(run* [s p o] - (membero s [:mother :child]) - (membero o [:mother :child]) - (membero p [:loves :has]) - (!= s o)) - -(run* [s p o] - (everyg #(membero % [:mother :child]) - [s o]) - (membero p [:loves :has]) - (distincto [s o])) - -(def words-for-db dict/cmu-with-stress) - -(defn phonemes-for-rhyme [word-phonemes] - (->> word-phonemes - reverse - (split-with #(re-matches #".*[^1]" %)) - (#(concat (first %) (take 1 (second %)))))) - -(defn perfect-rhyme? [a b] - (= (phonemes-for-rhyme a) - (phonemes-for-rhyme b))) - -(defn remove-consonants [w] - (remove #(re-matches #".*[^\d]" %) w)) - -(defn perfect-vowel-rhyme? [a b] - (= (remove-consonants (phonemes-for-rhyme a)) - (remove-consonants (phonemes-for-rhyme b)))) - -(db/db-rel - word - ^:index w - ^:index syllable-count - ^:index perfect-rhyme-phonemes) - -(defn make-word [w] - [word - w - (->> w - rest - (map #(string/replace % #"\d" "")) - syllabify/syllabify - count) - (remove-consonants (phonemes-for-rhyme w))]) - -(remove-consonants (phonemes-for-rhyme ["po" "P" "AA1" "V" "ER0" "T" "IY0"])) -(def words - (apply - db/db - (map make-word words-for-db))) - -(db/with-db words - (run 5 [sentence] - (fresh [w s p - w1 s1 p1] - (== p '("IY0" "OW1")) - (== sentence `(~w ~w1)) - (fd/+ s s1 7) - (word w s p) - (word w1 s1 p1) - (== p p1) - (!= w w1)))) - -(db/db-rel test-word w) -(def test-db (db/db [test-word "hello"] - [test-word "world"])) -(db/with-db test-db - (run 5 [q] - (test-word q))) - -(db/db-rel test-word w ^:index pos) -(def test-db (db/db [test-word "hello" :greeting] - [test-word "world" :noun])) -(db/with-db test-db - (run 5 [e a v] - (test-word "hello" a))) - -(comment - (run* [q r] - (membero q ["linux" "windows" "mac" "android" ""]) - (conda - [fail] - [(membero q ["linux" "windows"]) (== r 1)] - [(== q "mac") (== r 2)] - [succeed (== q "") (== r 3)])) - ;; => (["linux" 1] ["windows" 1]) - (run* [q r] - (conde - [fail] - [(membero q ["linux" "windows"]) (== r 1)] - [(== q "mac") (== r 2)] - [succeed (== q "") (== r 3)])) - ;; => (["mac" 2] ["" 3] ["linux" 1] ["windows" 1]) - (run* [q r] - (membero q ["linux" "windows" "mac" "android" ""]) - (condu - [(membero q ["linux" "windows"]) (== r 1)] - [(== q "mac") (== r 2)] - [succeed (== q "") (== r 3)])) - ;; => (["linux" 1] ["windows" 1] ["mac" 2] ["" 3]) - - - (run* [q r] - (conde - [(membero q ["linux" "windows"]) (membero r ["iOS" "android"])] - [(== q "mac") (== r 2)] - [succeed (== q "") (== r 3)])) - ;; => (["linux" "iOS"] ["linux" "android"] ["windows" "iOS"] ["windows" "android"]) - (run* [q r] - (condu - [(== q "linux") (== r 1)] - [(membero q ["linux" "windows"]) (membero r ["iOS" "android"])] - [(== q "windows") (== r 2)] - [(== q "linux") (== r 3)])) - - ;; => (["linux" "iOS"] ["linux" "android"]) - ) - -(let [people (repeatedly 5 lvar) - magazines (repeatedly 5 lvar) - cheeses (repeatedly 5 lvar) - reservations (repeatedly 5 lvar) - answers (map list people magazines cheeses reservations)] - (run* [q] - (== q answers))) -(run* [q] - (fd/in q (apply fd/domain (take 10 (iterate #(* % 2) 1)))))