1 #!/usr/bin/env python 2 3 """ 4 Specific classes for storing term information. 5 6 Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 from iixr.files import * 22 from iixr.positions import * 23 from iixr.phrases import PhraseIterator 24 from os.path import commonprefix # to find common string prefixes 25 from bisect import bisect_right # to find terms in the dictionary index 26 27 class TermWriter(FileWriter): 28 29 "Writing term information to files." 30 31 def reset(self): 32 self.last_term = "" 33 self.last_offset = 0 34 35 def write_term(self, term, offset, frequency, doc_frequency): 36 37 """ 38 Write the given 'term', its position file 'offset', its 'frequency' and 39 its 'doc_frequency' (number of documents in which it appears) to the 40 term information file. 41 """ 42 43 # Write the prefix length and term suffix. 44 45 common = len(commonprefix([self.last_term, term])) 46 suffix = term[common:] 47 48 self.write_number(common) 49 self.write_string(suffix) 50 51 # Write the offset delta. 52 53 self.write_number(offset - self.last_offset) 54 55 # Write the frequency. 56 57 self.write_number(frequency) 58 59 # Write the document frequency. 60 61 self.write_number(doc_frequency) 62 63 self.last_term = term 64 self.last_offset = offset 65 66 class TermReader(FileReader): 67 68 "Reading term information from files." 69 70 def reset(self): 71 self.last_term = "" 72 self.last_offset = 0 73 74 def read_term(self): 75 76 """ 77 Read a term, its position file offset, its frequency and its document 78 frequency from the term information file. 79 """ 80 81 # Read the prefix length and term suffix. 82 83 common = self.read_number() 84 suffix = self.read_string() 85 86 self.last_term = self.last_term[:common] + suffix 87 88 # Read the offset delta. 89 90 self.last_offset += self.read_number() 91 92 # Read the frequency. 93 94 frequency = self.read_number() 95 96 # Read the document frequency. 97 98 doc_frequency = self.read_number() 99 100 return self.last_term, self.last_offset, frequency, doc_frequency 101 102 def go_to_term(self, term, offset, info_offset): 103 104 """ 105 Seek past the entry for 'term' having 'offset' to 'info_offset'. This 106 permits the scanning for later terms from the specified term. 107 """ 108 109 self.seek(info_offset) 110 self.last_term = term 111 self.last_offset = offset 112 113 class TermIndexWriter(TermWriter): 114 115 "Writing term dictionary index details to files." 116 117 def reset(self): 118 TermWriter.reset(self) 119 self.last_info_offset = 0 120 121 def write_term(self, term, offset, frequency, doc_frequency, info_offset): 122 123 """ 124 Write the given 'term', its position file 'offset', its 'frequency' and 125 its 'doc_frequency' to the term dictionary index file, along with the 126 'info_offset' in the term information file. 127 """ 128 129 TermWriter.write_term(self, term, offset, frequency, doc_frequency) 130 131 # Write the information file offset delta. 132 133 self.write_number(info_offset - self.last_info_offset) 134 self.last_info_offset = info_offset 135 136 class TermIndexReader(TermReader): 137 138 "Reading term dictionary index details from files." 139 140 def reset(self): 141 TermReader.reset(self) 142 self.last_info_offset = 0 143 144 def read_term(self): 145 146 """ 147 Read a term, its position file offset, its frequency, its document 148 frequency and a term information file offset from the term dictionary 149 index file. 150 """ 151 152 term, offset, frequency, doc_frequency = TermReader.read_term(self) 153 154 # Read the offset delta. 155 156 self.last_info_offset += self.read_number() 157 158 return term, offset, frequency, doc_frequency, self.last_info_offset 159 160 class TermDictionaryWriter: 161 162 "Writing term dictionaries." 163 164 def __init__(self, info_writer, index_writer, position_dict_writer, interval): 165 self.info_writer = info_writer 166 self.index_writer = index_writer 167 self.position_dict_writer = position_dict_writer 168 self.interval = interval 169 self.entry = 0 170 171 def _write_term(self, term, offset, frequency, doc_frequency): 172 173 """ 174 Write the given 'term', its position file 'offset', its 'frequency' and 175 its 'doc_frequency' (number of documents in which it appears) to the 176 term information file. Return the offset after the term information was 177 written to the file. 178 """ 179 180 self.info_writer.write_term(term, offset, frequency, doc_frequency) 181 182 if self.entry % self.interval == 0: 183 info_offset = self.info_writer.f.tell() 184 self.index_writer.write_term(term, offset, frequency, doc_frequency, info_offset) 185 186 self.entry += 1 187 188 def write_term_positions(self, term, doc_positions): 189 190 """ 191 Write the given 'term' and the 'doc_positions' recording the documents 192 and positions at which the term is found. 193 """ 194 195 offset, frequency, doc_frequency = self.position_dict_writer.write_term_positions(doc_positions) 196 self._write_term(term, offset, frequency, doc_frequency) 197 198 def close(self): 199 self.info_writer.close() 200 self.index_writer.close() 201 self.position_dict_writer.close() 202 203 class TermDictionaryReader: 204 205 "Reading term dictionaries." 206 207 def __init__(self, info_reader, index_reader, position_dict_reader): 208 self.info_reader = info_reader 209 self.index_reader = index_reader 210 self.position_dict_reader = position_dict_reader 211 212 self.terms = [] 213 try: 214 while 1: 215 self.terms.append(self.index_reader.read_term()) 216 except EOFError: 217 pass 218 219 # Large numbers for ordering purposes. 220 221 if self.terms: 222 self.max_offset = self.terms[-1][1] + 1 223 else: 224 self.max_offset = None 225 226 def _find_closest_entry(self, term): 227 228 """ 229 Find the offsets and frequencies of 'term' from the term dictionary or 230 the closest term starting with the value of 'term'. 231 232 Return the closest index entry consisting of a term, the position file 233 offset, the term frequency, the document frequency, and the term details 234 file offset. 235 """ 236 237 i = bisect_right(self.terms, (term, self.max_offset, 0, 0)) - 1 238 239 # Get the entry position providing the term or one preceding it. 240 # If no entry precedes the requested term, return the very first entry 241 # as the closest. 242 243 if i == -1: 244 return self.terms[0] 245 else: 246 return self.terms[i] 247 248 def _find_closest_term(self, term): 249 250 """ 251 Find the offsets and frequencies of 'term' from the term dictionary or 252 the closest term starting with the value of 'term'. 253 254 Return the closest term (or the term itself), the position file offset, 255 the term frequency, the document frequency, and the term details file 256 offset (or None if the reader is already positioned). 257 """ 258 259 found_term, offset, frequency, doc_frequency, info_offset = self._find_closest_entry(term) 260 261 # Where the term is found immediately, return the offset and 262 # frequencies. If the term does not appear, return the details of the 263 # closest entry. 264 265 if term <= found_term: 266 return found_term, offset, frequency, doc_frequency, info_offset 267 268 # Otherwise, seek past the index term's entry in the information file 269 # and scan for the desired term. 270 271 else: 272 self.info_reader.go_to_term(found_term, offset, info_offset) 273 try: 274 while term > found_term: 275 found_term, offset, frequency, doc_frequency = self.info_reader.read_term() 276 except EOFError: 277 pass 278 279 return found_term, offset, frequency, doc_frequency, None 280 281 def _find_term(self, term): 282 283 """ 284 Find the position file offset and frequency of 'term' from the term 285 dictionary. 286 """ 287 288 found_term, offset, frequency, doc_frequency, info_offset = self._find_closest_term(term) 289 290 # If the term is found, return the offset and frequencies. 291 292 if term == found_term: 293 return offset, frequency, doc_frequency 294 else: 295 return None 296 297 def _get_positions(self, offset, doc_frequency): 298 299 """ 300 Obtain positions from the position index 'offset' expecting a number of 301 documents equal to the given 'doc_frequency'. 302 """ 303 304 return self.position_dict_reader.read_term_positions(offset, doc_frequency) 305 306 # Iterator convenience methods. 307 308 def __iter__(self): 309 self.rewind() 310 return self 311 312 def next(self): 313 try: 314 return self.read_term() 315 except EOFError: 316 raise StopIteration 317 318 # Sequential access methods. 319 320 def rewind(self): 321 self.info_reader.rewind() 322 323 def read_term(self): 324 325 """ 326 Return the next term, its frequency, its document frequency, and the 327 documents and positions at which the term is found. 328 """ 329 330 term, offset, frequency, doc_frequency = self.info_reader.read_term() 331 return term, frequency, doc_frequency, self._get_positions(offset, doc_frequency) 332 333 # Query methods. 334 335 def find_terms(self, term): 336 337 "Return all terms whose values start with the value of 'term'." 338 339 terms = [] 340 341 found_term, offset, frequency, doc_frequency, info_offset = self._find_closest_term(term) 342 343 # Position the reader, if necessary. 344 345 if info_offset is not None: 346 self.info_reader.go_to_term(found_term, offset, info_offset) 347 348 # Read and record terms. 349 350 try: 351 # Add the found term if it starts with the specified term. 352 353 while found_term.startswith(term): 354 terms.append(found_term) 355 found_term, offset, frequency, doc_frequency = self.info_reader.read_term() 356 357 except EOFError: 358 pass 359 360 return terms 361 362 def find_positions(self, term): 363 364 "Return the documents and positions at which the given 'term' is found." 365 366 t = self._find_term(term) 367 if t is None: 368 return [] 369 else: 370 offset, frequency, doc_frequency = t 371 return self._get_positions(offset, doc_frequency) 372 373 def find_common_positions(self, terms): 374 375 """ 376 Return the documents and positions at which all the given 'terms' are 377 found, where only common documents are returned. 378 """ 379 380 return PhraseIterator([self.find_positions(term) for term in terms]) 381 382 def get_frequency(self, term): 383 384 "Return the frequency of the given 'term'." 385 386 t = self._find_term(term) 387 if t is None: 388 return None 389 else: 390 offset, frequency, doc_frequency = t 391 return frequency 392 393 def get_document_frequency(self, term): 394 395 "Return the document frequency of the given 'term'." 396 397 t = self._find_term(term) 398 if t is None: 399 return None 400 else: 401 offset, frequency, doc_frequency = t 402 return doc_frequency 403 404 def close(self): 405 self.info_reader.close() 406 self.index_reader.close() 407 self.position_dict_reader.close() 408 409 # vim: tabstop=4 expandtab shiftwidth=4