1 #!/usr/bin/env python 2 3 """ 4 Specific classes for storing term information. 5 6 Copyright (C) 2009 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 os.path import commonprefix # to find common string prefixes 23 from bisect import bisect_right # to find terms in the dictionary index 24 25 class TermWriter(FileWriter): 26 27 "Writing term information to files." 28 29 def reset(self): 30 self.last_term = "" 31 self.last_offset = 0 32 33 def write_term(self, term, offset, frequency, doc_frequency): 34 35 """ 36 Write the given 'term', its position file 'offset', its 'frequency' and 37 its 'doc_frequency' (number of documents in which it appears) to the 38 term information file. Return the offset after the term information was 39 written to the file. 40 """ 41 42 # Write the prefix length and term suffix. 43 44 common = len(commonprefix([self.last_term, term])) 45 suffix = term[common:] 46 47 self.write_number(common) 48 self.write_string(suffix) 49 50 # Write the offset delta. 51 52 self.write_number(offset - self.last_offset) 53 54 # Write the frequency. 55 56 self.write_number(frequency) 57 58 # Write the document frequency. 59 60 self.write_number(doc_frequency) 61 62 self.last_term = term 63 self.last_offset = offset 64 65 return self.tell() 66 67 class TermReader(FileReader): 68 69 "Reading term information from files." 70 71 def reset(self): 72 self.last_term = "" 73 self.last_offset = 0 74 75 def read_term(self): 76 77 """ 78 Read a term, its position file offset, its frequency and its document 79 frequency from the term information file. 80 """ 81 82 # Read the prefix length and term suffix. 83 84 common = self.read_number() 85 suffix = self.read_string() 86 87 self.last_term = self.last_term[:common] + suffix 88 89 # Read the offset delta. 90 91 self.last_offset += self.read_number() 92 93 # Read the frequency. 94 95 frequency = self.read_number() 96 97 # Read the document frequency. 98 99 doc_frequency = self.read_number() 100 101 return self.last_term, self.last_offset, frequency, doc_frequency 102 103 def go_to_term(self, term, offset, info_offset): 104 105 """ 106 Seek past the entry for 'term' having 'offset' to 'info_offset'. This 107 permits the scanning for later terms from the specified term. 108 """ 109 110 self.seek(info_offset) 111 self.last_term = term 112 self.last_offset = offset 113 114 class TermIndexWriter(TermWriter): 115 116 "Writing term dictionary index details to files." 117 118 def reset(self): 119 TermWriter.reset(self) 120 self.last_info_offset = 0 121 122 def write_term(self, term, offset, frequency, doc_frequency, info_offset): 123 124 """ 125 Write the given 'term', its position file 'offset', its 'frequency' and 126 its 'doc_frequency' to the term dictionary index file, along with the 127 'info_offset' in the term information file. 128 """ 129 130 TermWriter.write_term(self, term, offset, frequency, doc_frequency) 131 132 # Write the information file offset delta. 133 134 self.write_number(info_offset - self.last_info_offset) 135 self.last_info_offset = info_offset 136 137 class TermIndexReader(TermReader): 138 139 "Reading term dictionary index details from files." 140 141 def reset(self): 142 TermReader.reset(self) 143 self.last_info_offset = 0 144 145 def read_term(self): 146 147 """ 148 Read a term, its position file offset, its frequency, its document 149 frequency and a term information file offset from the term dictionary 150 index file. 151 """ 152 153 term, offset, frequency, doc_frequency = TermReader.read_term(self) 154 155 # Read the offset delta. 156 157 self.last_info_offset += self.read_number() 158 159 return term, offset, frequency, doc_frequency, self.last_info_offset 160 161 class TermDictionaryWriter: 162 163 "Writing term dictionaries." 164 165 def __init__(self, info_writer, index_writer, position_dict_writer, interval): 166 self.info_writer = info_writer 167 self.index_writer = index_writer 168 self.position_dict_writer = position_dict_writer 169 self.interval = interval 170 self.entry = 0 171 172 def _write_term(self, term, offset, frequency, doc_frequency): 173 174 """ 175 Write the given 'term', its position file 'offset', its 'frequency' and 176 its 'doc_frequency' (number of documents in which it appears) to the 177 term information file. Return the offset after the term information was 178 written to the file. 179 """ 180 181 info_offset = self.info_writer.write_term(term, offset, frequency, doc_frequency) 182 183 if self.entry % self.interval == 0: 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 return self.position_dict_reader.read_term_positions(offset, doc_frequency) 299 300 # Iterator convenience methods. 301 302 def __iter__(self): 303 self.rewind() 304 return self 305 306 def next(self): 307 try: 308 return self.read_term() 309 except EOFError: 310 raise StopIteration 311 312 # Sequential access methods. 313 314 def rewind(self): 315 self.info_reader.rewind() 316 317 def read_term(self): 318 319 """ 320 Return the next term, its frequency, its document frequency, and the 321 documents and positions at which the term is found. 322 """ 323 324 term, offset, frequency, doc_frequency = self.info_reader.read_term() 325 positions = self._get_positions(offset, doc_frequency) 326 return term, frequency, doc_frequency, positions 327 328 # Query methods. 329 330 def find_terms(self, term): 331 332 "Return all terms whose values start with the value of 'term'." 333 334 terms = [] 335 336 found_term, offset, frequency, doc_frequency, info_offset = self._find_closest_term(term) 337 338 # Position the reader, if necessary. 339 340 if info_offset is not None: 341 self.info_reader.go_to_term(found_term, offset, info_offset) 342 343 # Read and record terms. 344 345 try: 346 # Add the found term if it starts with the specified term. 347 348 while found_term.startswith(term): 349 terms.append(found_term) 350 found_term, offset, frequency, doc_frequency = self.info_reader.read_term() 351 352 except EOFError: 353 pass 354 355 return terms 356 357 def find_positions(self, term): 358 359 "Return the documents and positions at which the given 'term' is found." 360 361 t = self._find_term(term) 362 if t is None: 363 return None 364 else: 365 offset, frequency, doc_frequency = t 366 return self._get_positions(offset, doc_frequency) 367 368 def get_frequency(self, term): 369 370 "Return the frequency of the given 'term'." 371 372 t = self._find_term(term) 373 if t is None: 374 return None 375 else: 376 offset, frequency, doc_frequency = t 377 return frequency 378 379 def get_document_frequency(self, term): 380 381 "Return the document frequency of the given 'term'." 382 383 t = self._find_term(term) 384 if t is None: 385 return None 386 else: 387 offset, frequency, doc_frequency = t 388 return doc_frequency 389 390 def close(self): 391 self.info_reader.close() 392 self.index_reader.close() 393 self.position_dict_reader.close() 394 395 # vim: tabstop=4 expandtab shiftwidth=4