lib/vsm: introduce an indexing process based on VSM
[nit.git] / lib / vsm / vsm.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Vector Space Model
16 #
17 # Vector Space Model (VSM) is an algebraic model for representing text documents
18 # (and any objects, in general) as vectors of identifiers, such as, for example,
19 # index terms.
20 #
21 # It is used in information filtering, information retrieval, indexing and
22 # relevancy rankings.
23 module vsm
24
25 import counter
26
27 # A n-dimensions vector
28 #
29 # *n-dimensions* vectors are used to represent a text document or an object.
30 class Vector
31 super HashMap[nullable Object, Float]
32
33 # Cosine similarity of `self` and `other`.
34 #
35 # Gives the proximity in the range `[0.0 .. 1.0]` where 0.0 means that the
36 # two vectors are orthogonal and 1.0 means that they are identical.
37 #
38 # ~~~
39 # var v1 = new Vector
40 # v1["x"] = 1.0
41 # v1["y"] = 2.0
42 # v1["z"] = 3.0
43 #
44 # var v2 = new Vector
45 # v2["x"] = 1.0
46 # v2["y"] = 2.0
47 # v2["z"] = 3.0
48 #
49 # var v3 = new Vector
50 # v3["a"] = 1.0
51 # v3["b"] = 2.0
52 # v3["c"] = 3.0
53 #
54 # print v1.cosine_similarity(v2)
55 # assert v1.cosine_similarity(v2) == 1.0
56 # print v1.cosine_similarity(v3)
57 # assert v1.cosine_similarity(v3) == 0.0
58 # ~~~
59 fun cosine_similarity(other: SELF): Float do
60 # Collect terms
61 var terms = new HashSet[nullable Object]
62 for k in self.keys do terms.add k
63 for k in other.keys do terms.add k
64
65 # Get dot product of two vectors
66 var dot = 0.0
67 for term in terms do
68 dot += self.get_or_default(term, 0.0) * other.get_or_default(term, 0.0)
69 end
70 var cos = dot.to_f / (self.norm * other.norm)
71 if cos.is_nan then return 0.0
72 return cos
73 end
74
75 # The norm of the vector.
76 #
77 # `||x|| = (x1 ** 2 ... + xn ** 2).sqrt`
78 #
79 # ~~~
80 # var v = new Vector
81 # v["x"] = 1.0
82 # v["y"] = 1.0
83 # v["z"] = 1.0
84 # v["t"] = 1.0
85 # assert v.norm.is_approx(2.0, 0.001)
86 #
87 # v["x"] = 1.0
88 # v["y"] = 2.0
89 # v["z"] = 3.0
90 # v["t"] = 0.0
91 # assert v.norm.is_approx(3.742, 0.001)
92 # ~~~
93 fun norm: Float do
94 var sum = 0.0
95 for v in self.values do sum += v.pow(2.0)
96 return sum.to_f.sqrt
97 end
98
99 redef fun to_s do
100 return "[{join(", ", ":")}]"
101 end
102 end
103
104 # A Document index based on VSM
105 #
106 # Using VSMIndex you can index documents associated with their vector.
107 # Documents can then be matched to query vectors.
108 class VSMIndex
109
110 # Documents index
111 #
112 # TODO use a more efficient representation.
113 var documents = new HashSet[Document]
114
115 # Count for all terms in all indexed documents
116 #
117 # Used to compute the `inverse_doc_frequency`.
118 var terms_doc_count = new Vector
119
120 # Inverse document frequency
121 #
122 # The inverse document frequency is a measure of how much information a term
123 # provides, that is, whether the term is common or rare across all documents.
124 var inverse_doc_frequency = new Vector
125
126 # Used to sort matches
127 #
128 # See `IndexMatch`.
129 var sorter = new IndexMatchSorter
130
131 # Match `query` vector to all index document vectors
132 #
133 # Returns an `IndexMatch` for each indexed document.
134 # Results are ordered by descending similarity.
135 fun match_vector(query: Vector): Array[IndexMatch] do
136 var matches = new Array[IndexMatch]
137 for doc in documents do
138 var sim = query.cosine_similarity(doc.tfidf)
139 if sim == 0.0 then continue
140 matches.add new IndexMatch(doc, sim)
141 end
142 sorter.sort(matches)
143 return matches
144 end
145
146 # Index a document
147 #
148 # With each new document, the `inverse_doc_frequency` must be updated.
149 # By default, the method `update_index` is called after each call to
150 # `index_document`.
151 #
152 # When processing batch documents, use `auto_update = false` to disable
153 # the auto update of the index.
154 fun index_document(doc: Document, auto_update: nullable Bool) do
155 for term, count in doc.terms_count do
156 if not terms_doc_count.has_key(term) then
157 terms_doc_count[term] = 1.0
158 else
159 terms_doc_count[term] += 1.0
160 end
161 end
162 documents.add doc
163 if auto_update == null or auto_update then update_index
164 end
165
166 # Update the index
167 #
168 # Recompute the `inverse_doc_frequency` values.
169 # Must be called manually after indexing new document with the option
170 # `auto_update = false`.
171 fun update_index do
172 for doc in documents do
173 for term, ccount in doc.terms_count do
174 inverse_doc_frequency[term] = (documents.length.to_f / terms_doc_count[term]).log
175 end
176 end
177 for doc in documents do
178 for term, freq in doc.terms_frequency do
179 doc.tfidf[term] = freq * inverse_doc_frequency[term]
180 end
181 end
182 end
183 end
184
185 # A VSM index to store strings
186 class StringIndex
187 super VSMIndex
188
189 # Index a new Document from `title`, `uri` and string `string`.
190 #
191 # Return the Document created.
192 #
193 # See `index_document`.
194 fun index_string(title, uri, string: String, auto_update: nullable Bool): Document do
195 var vector = parse_string(string)
196 var doc = new Document(title, uri, vector)
197 index_document(doc, auto_update)
198 return doc
199 end
200
201 # Match the `query` string against all indexed documents
202 #
203 # See `match_vector`.
204 fun match_string(query: String): Array[IndexMatch] do
205 var vector = parse_string(query)
206 return match_vector(vector)
207 end
208
209 # Parse the `string` as a Vector
210 #
211 # Returns a vector containing the terms of `string`.
212 fun parse_string(string: String): Vector do
213 var reader = new StringReader(string)
214 var vector = new Vector
215 loop
216 var token = reader.read_word
217 if token == "" then break
218
219 if not vector.has_key(token) then
220 vector[token] = 1.0
221 else
222 vector[token] += 1.0
223 end
224 end
225 return vector
226 end
227 end
228
229 # A VSMIndex to index files
230 class FileIndex
231 super StringIndex
232
233 # Index a file from its `path`.
234 #
235 # Return the created document or null if `path` is not accepted by `accept_file`.
236 #
237 # See `index_document`.
238 fun index_file(path: String, auto_update: nullable Bool): nullable Document do
239 if not accept_file(path) then return null
240 var vector = parse_file(path)
241 var doc = new Document(path, path, vector)
242 index_document(doc, auto_update)
243 return doc
244 end
245
246 # Index multiple files
247 #
248 # The recursive method `index_dir` will be called for each directory found
249 # in `paths`.
250 #
251 # See `index_file`
252 fun index_files(paths: Collection[String], auto_update: nullable Bool) do
253 for path in paths do
254 if path.to_path.is_dir then
255 index_dir(path, false)
256 else
257 index_file(path, false)
258 end
259 end
260 if auto_update != null and auto_update then update_index
261 end
262
263 # Index all files in `dir` recursively
264 #
265 # See `index_file`.
266 fun index_dir(dir: String, auto_update: nullable Bool) do
267 if not dir.to_path.is_dir then return
268 for file in dir.files do
269 var path = dir / file
270 if path.to_path.is_dir then
271 index_dir(path, false)
272 else
273 index_file(path, false)
274 end
275 end
276 if auto_update != null and auto_update then update_index
277 end
278
279 # Is `path` accepted depending on `whitelist_exts` and `blacklist_exts`?
280 fun accept_file(path: String): Bool do
281 var ext = path.file_extension
282 if ext != null then
283 ext = ext.to_lower
284 if blacklist_exts.has(ext) then return false
285 if whitelist_exts.not_empty and not whitelist_exts.has(ext) then return false
286 end
287 return whitelist_exts.is_empty
288 end
289
290 # Parse the `file` content as a Vector
291 #
292 # See `parse_string`.
293 fun parse_file(file: String): Vector do
294 return parse_string(file.to_path.read_all)
295 end
296
297 # File extensions white list
298 #
299 # If not empty, only files with these extensions will be indexed.
300 #
301 # If an extension is in both `whitelist_exts` and `blacklist_exts`, the
302 # blacklist will prevail and the file will be ignored.
303 var whitelist_exts = new Array[String] is writable
304
305 # File extensions black list
306 #
307 # Files with these extensions will not be indexed.
308 var blacklist_exts = new Array[String] is writable
309 end
310
311 # A Document to add in a VSMIndex
312 class Document
313
314 # Document title
315 var title: String
316
317 # Document URI
318 var uri: String
319
320 # Count of all terms found in the document
321 #
322 # Used to compute the document `terms_frequency`.
323 var terms_count: Vector
324
325 # Frequency of each term found in the document
326 #
327 # Used to match the document against the `VSMIndex::inverse_doc_frequency`.
328 var terms_frequency: Vector is lazy do
329 var all_terms = 0.0
330 for t, c in terms_count do all_terms += c
331
332 var vector = new Vector
333 for t, c in terms_count do
334 vector[t] = c / all_terms
335 end
336 return vector
337 end
338
339 # Term frequency–Inverse document frequency for each term
340 #
341 # A high weight in tf–idf is reached by a high term frequency
342 # (in the given document) and a low document frequency of the term in the
343 # whole collection of documents
344 var tfidf = new Vector
345
346 redef fun to_s do return "{title}"
347 end
348
349 # A match to a `request` in an `Index`
350 class IndexMatch
351 super Comparable
352
353 # Document matching the `request_vector`
354 var document: Document
355
356 # Similarity between the `request` and the `doc`.
357 #
358 # Result is in the range 0.0 .. 1.1 where 0.0 means no similarity and 1.0
359 # means perfect similarity.
360 var similarity: Float
361
362 redef fun to_s do return "{document} ({similarity})"
363 end
364
365 # Sort matches by similarity
366 class IndexMatchSorter
367 super DefaultComparator
368
369 redef type COMPARED: IndexMatch
370
371 redef fun compare(a, b) do
372 return b.similarity <=> a.similarity
373 end
374 end