views:

176

answers:

3

I have a document hash which is a reference like this:

(def *document-hash* (ref (hash-map)))

It looks like this

 {"documentid" {:term-detail {"term1" count1 ,"term2" count2},  "doclen" 33}}}

How do I add to this hash table?Right now I have

(defn add-doc-hash [docid  term-number count]
  (dosync (alter *document-hash*
    (fn [a-hash]
      (assoc a-hash docid {:term-detail  
        (assoc ((a-hash docid)) :term-detail) term-number count), :doclen 33))))))
  • I want to update the term-details for the documents
  • Every time a new term comes, I want to get the term-details and update the terms and its count
  • initially the hash is empty

But this throws the null pointer exception because ther term-detail hash is not created when I try to add the term-number.

A: 

Another way of phrasing the question if i understand it correctly would be: "how do i write a function to add another [term, count] pair to a map."

a little helper function to get the current details for a map, if that map is not yet added, then clearly it will have no details so i represent this with an empty map
this solves your problem of where to add the first term-number:

(defn get-term-detail [a-hash docid]
  (let [entry (a-hash docid)]
    (if nil? entry)
       {}
       (:term-details entry))))

then soemthing like:

(assoc a-hash docid {:term-details (assoc (get-term-detail a-hash docid) term-number count)        :doclen 33)

to actuall add it to the hash

Arthur Ulfeldt
+1  A: 
user> (def x (ref {"documentid" {:term-detail {"term1" 1 ,"term2" 2},  "doclen" 33}}))
#'user/x
user> (dosync (alter x assoc-in ["documentid" :term-detail "term3"] 0))
{"documentid" {:term-detail {"term3" 0, "term1" 1, "term2" 2}, "doclen" 33}}
user> (dosync (alter x update-in ["documentid" :term-detail "term3"] inc))
{"documentid" {:term-detail {"term3" 1, "term1" 1, "term2" 2}, "doclen" 33}}
Brian Carper
You can also take advantage of the fact that update-in creates nodes:user=> (update-in {} [:hi :mum] #(if % (inc %) 0)) -> {:hi {:mum 0}}
Timothy Pratley
A: 

Here's a rewrite of your function that should work. It uses the assoc-in function

(defn add-doc-hash [docid  term-number count]
  (dosync (alter *document-hash* assoc-in [docid :term-detail term-number] count)))
Eric Normand