Friday, August 19, 2011

Visualize Google search results with Clojure

One question that I always get asked when I introduce Clojure to my programmer friends is about its strength. While there are so much about Clojure that a programmer can benefit from, I always tend to single out one thing about it - powerful sequence processing. It shines when you are faced with solving any problem related to data processing. Rather than just talking how great it is to manipulate and analyze data, I actually created a project that I called gcount that shows Google search results in a bar chart. Here is the source:
(ns gcount.core
"generates charts out of the result from google search term(s)."
(:use (incanter core charts))
(:require [clj-http.client :as http-client]))
(def *search-provider* "http://www.google.com/search?hl=en&q=")
(def *search-pattern* #"About.*?([\d,]+).*?")
(defn search-for-term [term]
(let [encoded-term (.replaceAll (apply str term) " " "+")
qstring (str *search-provider* encoded-term)
page (:body (http-client/get qstring))
hits (second (re-find *search-pattern* page))
nhits (Integer/parseInt (.replaceAll hits "," ""))]
(hash-map term nhits)))
(defn view-bar-chart [mcoll]
(view (bar-chart (keys mcoll) (vals mcoll)
:x-label "Search terms"
:y-label "Search results")))
(defn search-view-terms [terms]
(->> (reduce merge (map search-for-term terms))
(view-bar-chart)))
view raw gcount.clj hosted with ❤ by GitHub
You can just clone it like this: git clone https://oneness@github.com/oneness/gcount.git Then you can run lein deps in gcount directory to pull all dependencies. When it is done, just evaluate core.clj file from the REPL and run: gcount.core> (search-view-terms ["Functional programming" "Object oriented programming" "Functional Object oriented programming"]) You will see following chart:
Next time when you need to show your friend how popular a particular term is, just show him/her this chart. Then you will have curious questions about how it is done and more about Clojure. Enjoy!

No comments:

Post a Comment