@@ -189,31 +189,53 @@ impl Model for SqliteModel {
189189 Map of term with its frequency of occurence single document. */
190190pub type FreqTable = HashMap :: < String , usize > ;
191191
192- /* Map of a document with a pair containing (frequency table, total terms in that table (i.e sum of values)). */
193- pub type FreqTableIndex = HashMap :: < PathBuf , ( usize , FreqTable ) > ;
192+ /* PREVIOUS: Map of a document with a pair containing (frequency table, total terms in that table (i.e sum of values)). */
193+ // pub type FreqTableIndex = HashMap::<PathBuf, (usize, FreqTable)>;
194194
195195/* Answers how frequently a term occurs in all documents.
196196 Map of term with frequency of occurence in all corpus of documents.*/
197197pub type GlobalTermFreq = HashMap :: < String , usize > ;
198198
199+ #[ derive( Default , Serialize , Deserialize ) ]
200+ pub struct Doc {
201+ count : usize ,
202+ ft : FreqTable
203+ }
204+
205+ type Docs = HashMap :: < PathBuf , Doc > ;
206+
199207#[ derive( Default , Deserialize , Serialize ) ]
200208pub struct InMemoryModel {
201209 pub gtf : GlobalTermFreq ,
202- pub tf_index : FreqTableIndex
210+ pub docs : Docs
211+ }
212+
213+ fn compute_tf ( term : & str , doc : & Doc ) -> f32 {
214+ let n = doc. ft . get ( term) . cloned ( ) . unwrap_or ( 0 ) as f32 ;
215+ let d = doc. count as f32 ;
216+ n / d
217+ }
218+
219+ fn compute_idf ( term : & str , model : & InMemoryModel ) -> f32 {
220+ let n = model. docs . len ( ) as f32 ;
221+ // NOTE: Can lead to division by 0 if term is not in Document Corpus
222+ // Set Denominator to 1 if turns to 0
223+ let d = model. gtf . get ( term) . cloned ( ) . unwrap_or ( 1 ) as f32 ;
224+ f32:: log10 ( n / d)
203225}
204226
205227impl Model for InMemoryModel {
206228 fn search_query ( & self , query : & [ char ] ) -> Result < Vec < ( PathBuf , f32 ) > , ( ) > {
207229 let mut results = Vec :: new ( ) ;
208230 // Cache all the tokens and don't retokenize on each query
209231 let tokens = Lexer :: new ( & query) . collect :: < Vec < _ > > ( ) ;
210- for ( doc , ( count , ft ) ) in & self . tf_index {
232+ for ( path , doc ) in & self . docs {
211233 let mut rank = 0f32 ;
212234 for token in & tokens {
213235 // Rank is value of tf-idf => tf * idf
214- rank += compute_tf ( & token, & ft , * count ) * compute_idf ( & token, & self ) ;
236+ rank += compute_tf ( & token, & doc ) * compute_idf ( & token, & self ) ;
215237 }
216- results. push ( ( doc . clone ( ) , rank) ) ;
238+ results. push ( ( path . to_owned ( ) , rank) ) ;
217239 }
218240
219241 // Rank the files in desc order
@@ -237,21 +259,7 @@ impl Model for InMemoryModel {
237259 for term in ft. keys ( ) {
238260 self . gtf . entry ( term. to_owned ( ) ) . and_modify ( |x| * x += 1 ) . or_insert ( 1 ) ;
239261 }
240- self . tf_index . insert ( path, ( term_count, ft) ) ;
262+ self . docs . insert ( path, Doc { count : term_count, ft : ft } ) ;
241263 Ok ( ( ) )
242264 }
243- }
244-
245- pub fn compute_tf ( term : & str , freq_table : & FreqTable , term_count : usize ) -> f32 {
246- let n = * freq_table. get ( term) . unwrap_or ( & 0 ) as f32 ;
247- let d = term_count. max ( 1 ) as f32 ;
248- n / d
249- }
250-
251- pub fn compute_idf ( term : & str , model : & InMemoryModel ) -> f32 {
252- let n = model. tf_index . len ( ) as f32 ;
253- // NOTE: Can lead to division by 0 if term is not in Document Corpus
254- // Set Denominator to 1 if turns to 0
255- let d = * model. gtf . get ( term) . unwrap_or ( & 1 ) as f32 ;
256- f32:: log10 ( n / d)
257265}
0 commit comments