On Thu, Jan 6, 2011 at 1:49 PM, new2clojure <miguel.arre...@gmail.com> wrote:
>
> Hi,
>
> I am trying to store into a map the frequency of each [a-z]+ word in a
> file. When I run this code in the repl the resulting dictionary is
> empty. How should I adapt my code to get this functionality right?.
>
> Thank you in advance
>
> (import (java.io BufferedReader FileReader))
>
> (def dictionary {})
>
> (defn process-file [file-name]
>  (with-open
>    [rdr (BufferedReader. (FileReader. file-name))]
>    (doseq
>      [line (line-seq rdr)]
>      (reduce #(assoc %1 %2 (inc (get %1 %2 1))) dictionary (re-seq
> #"[a-z]+" (.toLowerCase line))))))

The main problem is (doseq ...) should be (loop [words {}] ...) or
(apply merge ...). But there's another bug: the third argument to get
should be 0 rather than 1. You could also use (merge-with + %1 {%2
1}). Or frequencies:

(frequencies (re-seq ...))

Try

(defn process-file [file-name]
  (with-open [rdr (BufferedReader. (FileReader. file-name))]
    (apply merge-with +
      (for [line (line-seq rdr)]
        (frequencies (re-seq #"[a-z]+" (.toLowerCase line)))))))

(untested, but should be closer).

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to