Skip to content

Commit

Permalink
Implement Winston Markov
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzleutgeb committed Feb 5, 2017
1 parent f27059f commit ae4a3eb
Show file tree
Hide file tree
Showing 6 changed files with 10,300 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pom.properties
# nrepl specific
.nrepl-port
target/

.lein-repl-history
10,276 changes: 10,276 additions & 0 deletions resources/1984.txt

Large diffs are not rendered by default.

56 changes: 22 additions & 34 deletions src/twitter_example/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
(defn chain->text [chain]
(apply str (interpose " " chain)))

(defn walk-chain [prefix chain result]
(defn end-at-last-punctuation [text]
(let [trimmed-to-last-punct (apply str (re-seq #"[\s\w]+[^.!?,]*[.!?,]" text))
trimmed-to-last-word (apply str (re-seq #".*[^a-zA-Z]+" text))
result-text (if (empty? trimmed-to-last-punct)
trimmed-to-last-word
trimmed-to-last-punct)
cleaned-text (clojure.string/replace result-text #"[,| ]$" ".")]
(clojure.string/replace cleaned-text #"\"" "'")))

(defn walk-chain [prefix chain result maximum]
(let [suffixes (get chain prefix)]
(if (empty? suffixes)
result
Expand All @@ -33,68 +42,47 @@
result-char-count (count result-with-spaces)
suffix-char-count (+ 1 (count suffix))
new-result-char-count (+ result-char-count suffix-char-count)]
(if (>= new-result-char-count 140)
(if (>= new-result-char-count maximum)
result
(recur new-prefix chain (conj result suffix)))))))
(recur new-prefix chain (conj result suffix) maximum))))))

(defn generate-text
[start-phrase word-chain]
[start-phrase word-chain hashtag]
(let [prefix (clojure.string/split start-phrase #" ")
result-chain (walk-chain prefix word-chain prefix)
result-chain (walk-chain prefix word-chain prefix (- 140 (+ 1 (count hashtag))))
result-text (chain->text result-chain)]
result-text))
(str (end-at-last-punctuation result-text) " " hashtag)))

(defn process-file [fname]
(text->word-chain
(slurp (clojure.java.io/resource fname))))

(def files ["fluffyrocketship2.txt" "fluffyrocketship.txt" "astronomy.txt" "theguide.txt"])
(def files ["1984.txt"])
(def functional-leary (apply merge-with clojure.set/union (map process-file files)))

(def prefix-list ["I will" "But if" "So I" "I was" "I am" "We also"
"We have" "I can" "I never" "For you"
"Fuck you" "We need" "We will" "Now this"
"You might" "This is" "Let me" "You better"])
(def hashtags ["#dystopia" "#orwellian" "#future" "#surveillance" "#bigbrother" "#oneparty" "#warispeace" "#freedomisslavery" "#ignoranceisstrength" "#newspeak" "#oceania"])

(defn end-at-last-punctuation [text]
(let [trimmed-to-last-punct (apply str (re-seq #"[\s\w]+[^.!?,]*[.!?,]" text))
trimmed-to-last-word (apply str (re-seq #".*[^a-zA-Z]+" text))
result-text (if (empty? trimmed-to-last-punct)
trimmed-to-last-word
trimmed-to-last-punct)
cleaned-text (clojure.string/replace result-text #"[,| ]$" ".")]
(clojure.string/replace cleaned-text #"\"" "'")))
(def prefix-list ["I would" "Big Brother" "It was" "There was" "They talked" "She was" "Newspeak is" "The Party" "Winston turned" "What seemed" "The door" "The man" "He paused" "The word" "Abruptly he" "He laid" "At this" "And he" "He raised"])

(defn tweet-text []
(let [text (generate-text (-> prefix-list shuffle first) functional-leary)]
(end-at-last-punctuation text)))

(defn easy-tweet-text []
(let [text (random-line "astronomy.txt")]
(end-at-last-punctuation text)))
(defn generate-tweet-text []
(generate-text (-> prefix-list shuffle first) functional-leary (-> hashtags shuffle first)))

(def my-creds (twitter-oauth/make-oauth-creds (env :app-consumer-key)
(env :app-consumer-secret)
(env :user-access-token)
(env :user-access-secret)))

(defn status-update []
(let [tweet (easy-tweet-text)]
(let [tweet (generate-tweet-text)]
(println "generate tweet is :" tweet)
(println "char count is:" (count tweet))
(when (not-empty tweet)
(try (twitter/statuses-update :oauth-creds my-creds
:params {:status tweet})
(catch Exception e (println "Oh no! " (.getMessage e)))))))

(defn random-line [filename]
(with-open [reader (io/reader (clojure.java.io/resource filename))]
(rand-nth (line-seq reader))))

(def my-pool (overtone/mk-pool))

(defn -main [& args]
;; every 2 hours
(println "Started up")
(println (tweet-text))
(overtone/every (* 1000 60 60 2) #(println (status-update)) my-pool))
(overtone/every (* 1000 30) #(println (status-update)) my-pool))

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit ae4a3eb

Please sign in to comment.