#NEWLINE# def score_docs( self , query_vec , postings_lists , N , get_doc_freq , get_doc_len , **extra ) : #NEWLINE# #INDENT# "\n        Scores documents' similarities to query using cosine similarity\n        in a vector space model.  Uses tf.idf weighting.\n        \n        An individual term hit is scored as::\n        \n            idf * self.tf_weight(q_tf) * self.tf_weight(d_tf)\n        \n        The overall score for a doc is given by the sum of the term-hit scores\n        " #NEWLINE# if ( N == 0 ) : #NEWLINE# #INDENT# return ( ) #NEWLINE# #UNINDENT# doc_hit_map = defaultdict ( int ) #NEWLINE# for ( term , postings_list ) in postings_lists : #NEWLINE# #INDENT# idf = self . idf_weight ( N , get_doc_freq ( term ) ) #NEWLINE# query_term_wt = ( self . tf_weight ( query_vec [ term ] ) * idf ) #NEWLINE# for ( docid , freq ) in postings_list : #NEWLINE# #INDENT# doc_hit_map [ docid ] += ( self . tf_weight ( freq ) * query_term_wt ) #NEWLINE# #UNINDENT# #UNINDENT# for ( docid , weight ) in doc_hit_map . iteritems ( ) : #NEWLINE# #INDENT# doc_len = get_doc_len ( docid ) #NEWLINE# doc_hit_map [ docid ] = ( weight / doc_len ) #NEWLINE# #UNINDENT# return sorted ( doc_hit_map . iteritems ( ) , key = operator . itemgetter ( 1 ) , reverse = True )