added comments; some refactoring

master
Abhinav Sarkar 2010-11-04 00:25:54 +05:30
parent 0bb5911351
commit 63635873ca
2 changed files with 50 additions and 20 deletions

View File

@ -4,7 +4,7 @@
[org.apache.http HttpException]
[org.apache.http.auth AuthScope UsernamePasswordCredentials]
[org.apache.http.client.methods HttpGet]
[org.apache.http.client ResponseHandler HttpClient]
[org.apache.http.client HttpClient]
[org.apache.http.impl.client DefaultHttpClient]
[org.apache.http.params BasicHttpParams HttpParams])
(:use [clojure.java.io :only (reader resource as-file)]
@ -18,9 +18,12 @@
(defmethod trace :l [msg _ arg] (do (println msg ":" arg) arg))
(defmethod trace :f [arg _ msg] (do (println msg ":" arg) arg))
;names of adjective files
(def adjective-files ["negative" "neutral" "positive"])
(defn adjectives []
(defn adjectives
"Loads the adjective files and returns a map of adjective to its type."
[]
(->> adjective-files
(map #(str "clj_twitter_feelings/adjectives/" % ".txt"))
(map resource)
@ -29,31 +32,39 @@
(let [adjective-type
(-> url .toString (.split "/") last (.split "\\.") first)]
(reduce
(fn [acc word]
(assoc! acc word adjective-type))
(fn [acc word] (assoc! acc word adjective-type))
acc
(read-lines url))))
(transient {}))
(persistent!)))
(defn safe-divide [n d] (if (zero? d) 0 (float (/ n d))))
(defn safe-divide
"If denominator is 0 returns 0 else returns numerator divided by denominator."
[n d] (if (zero? d) 0 (float (/ n d))))
(def split-pattern (re-pattern "[\\p{Z}\\p{C}\\p{P}]+"))
(defn tokenize-line [line]
(defn tokenize-line
"Tokenizes the line on split-pattern and returns a lazy seq of non-empty
tokens."
[line]
(->> line (split split-pattern) (filter (complement empty?))))
(defprotocol Processor
(process [this tweet]))
(defn twitter-stream-client [username password]
(defn twitter-stream-client
"Creates an HttpClient for stream.twitter.com using the credentials provided."
[username password]
(doto (DefaultHttpClient.)
(.. getCredentialsProvider
(setCredentials
(AuthScope. "stream.twitter.com" 80)
(UsernamePasswordCredentials. username password)))))
(defn tweet-stream [^HttpClient client method & params]
(defn tweet-stream
"Creates a lazy stream of live tweets."
[^HttpClient client method & params]
(let [read-line (fn this [^BufferedReader rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
@ -74,7 +85,9 @@
(throw (HttpException.
(str "Invalid Status code: " status-code))))))
(defn process-tweet-stream [stream processors]
(defn process-tweet-stream
"Processes the tweet stream with the provided processors."
[stream processors]
(doseq [tweet stream]
(future (doseq [p processors] (process p tweet)))))
@ -93,10 +106,10 @@
(def *tweet-window-size* 25)
(defn adjective-processor [adjective-map]
(let [states (atom (PersistentQueue/EMPTY))]
(let [state-window (atom (PersistentQueue/EMPTY))]
(reify Processor
(process [this tweet]
(let [adj-typs
(let [adj-typs ;list of types of adjective in the tweet
(->> tweet :text
tokenize-line
(map lower-case)
@ -110,11 +123,17 @@
(reduce #(assoc %1 %2 (inc (get %1 %2 0))) {} adj-typs)]
(swap! adjective-type-count
(fn [state]
(if (<= (count @states) *tweet-window-size*)
(do (swap! states conj current-state)
;If the count of states in the state-window is less than
;*tweet-window-size*, push the current-state in the
;state-window and return merge result: (state + current-state).
;Else, pop the oldest state from the state-window, push the
;current-state in the state-window and return merge result:
;(state + current-state - popped out oldest state).
(if (<= (count @state-window) *tweet-window-size*)
(do (swap! state-window conj current-state)
(merge-with + state current-state))
(let [old-state (peek @states)]
(swap! states #(conj (pop %) current-state))
(let [old-state (peek @state-window)]
(swap! state-window #(conj (pop %) current-state))
(merge-with #(max 0 (- %1 %2))
(merge-with + state current-state)
old-state))))))))))))

View File

@ -119,7 +119,11 @@
(.setSize dialog-width dialog-height))))
(defn init-gui [adjective-map]
(let [frame (JFrame. "Twitter Feelings")
(let [frame (doto (JFrame. "Twitter Feelings")
(.setIconImage
(ImageIO/read (resource "clj_twitter_feelings/favicon.jpg")))
(.setDefaultCloseOperation WindowConstants/EXIT_ON_CLOSE)
(.setResizable false))
adjective-types (sort (keys @adjective-type-count))
^DefaultPieDataset pie-dataset
@ -164,9 +168,11 @@
"Credentials" "Input your Twitter credentials" 220 150
"Screen Name" "Password" 20
"OK" "Cancel"
;validation-fn
(fn [uname pass _]
(when (or (empty? uname) (empty? pass))
(str "Please input Screen Name and Password")))
;ok-fn
(fn [uname pass ^JDialog dialog]
(future
(try
@ -181,13 +187,18 @@
"Error" :error)
(.setVisible dialog true)))))
(.start timer))
;cancel-fn
(fn [_] (exit-app frame)))]
;add watches
(add-watch adjective-seen :adjective-lbl
(fn [_ _ _ n]
(do-swing
(.setText adjective-lbl (str "<html><h2>" n "</h2></html>")))))
(add-watch status-seen :status-lbl
(fn [_ _ _ n] (do-swing (.setText status-lbl n))))
;do some configuration of the plots
(doto ^PiePlot (.getPlot pie-chart)
(.setNoDataMessage "No data available")
(.setLabelGenerator (StandardPieSectionLabelGenerator. "{0} {2}"))
@ -199,11 +210,9 @@
(.setFixedAutoRange 120000.0))
(doto (.. time-series-chart getXYPlot getRangeAxis)
(.setRange 0.0 100.0))
;layout the content in the frame and make it visible
(doto frame
(.setIconImage
(ImageIO/read (resource "clj_twitter_feelings/favicon.jpg")))
(.setDefaultCloseOperation WindowConstants/EXIT_ON_CLOSE)
(.setResizable false)
(.setContentPane
(miglayout (JPanel.)
:layout [:wrap 1]
@ -217,6 +226,8 @@
(.pack)
(.setVisible true)
(RefineryUtilities/centerFrameOnScreen))
;show the auth-input-dialog
(doto auth-input-dialog
(RefineryUtilities/centerFrameOnScreen)
(.setVisible true))))