Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.98 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (ns freq.core
  2.   (:import (java.io BufferedReader FileReader))
  3.   (:require [clojure.string :as string]))
  4.  
  5. (defn read-file-lazy
  6.   "Opens a file and creates a lazy-sequence to read each line"
  7.   [file-name]
  8.   (with-open [reader  (BufferedReader. (FileReader. file-name))]
  9.     (line-seq reader)))
  10.  
  11. (defn read-file
  12.   "Reads a file using slurp"
  13.   [file-name]
  14.   (slurp file-name))
  15.  
  16. (defn read-words
  17.   "returns the words in a file"
  18.   [file-name]
  19.   (string/split (read-file file-name) #"[ \n]"))
  20.  
  21. (defn update-frequencies
  22.   "Update frequency of the word"
  23.   [frequency-map word]
  24.   (if (contains? frequency-map word)
  25.     (update-in frequency-map [word] inc)
  26.     (assoc frequency-map word 1)))
  27.  
  28. (defn calculate-word-frequencies
  29.   "Given a sequence of words spits out frequency map of each word"
  30.   [words]
  31.   (reduce update-frequencies (sorted-map) words))
  32.  
  33. (defn top-words
  34.   "Get top 10 frequent words from file"
  35.   [file-name]
  36.   (take 10 (reverse (sort-by last (calculate-word-frequencies (read-words file-name))))))