paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | A simple (and sane) text indexing library. |
paul@1 | 5 | |
paul@1 | 6 | Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> |
paul@1 | 7 | |
paul@1 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1 | 11 | version. |
paul@1 | 12 | |
paul@1 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@1 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@1 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@1 | 16 | |
paul@1 | 17 | You should have received a copy of the GNU General Public License along |
paul@1 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 19 | """ |
paul@0 | 20 | |
paul@2 | 21 | from os.path import commonprefix # to find common string prefixes |
paul@3 | 22 | from bisect import bisect_right # to find terms in the dictionary index |
paul@2 | 23 | |
paul@0 | 24 | # Foundation classes. |
paul@0 | 25 | |
paul@0 | 26 | class File: |
paul@0 | 27 | |
paul@0 | 28 | "A basic file abstraction." |
paul@0 | 29 | |
paul@0 | 30 | def __init__(self, f): |
paul@0 | 31 | self.f = f |
paul@0 | 32 | self.reset() |
paul@0 | 33 | |
paul@0 | 34 | def reset(self): |
paul@0 | 35 | pass |
paul@0 | 36 | |
paul@0 | 37 | def close(self): |
paul@0 | 38 | self.f.close() |
paul@0 | 39 | |
paul@0 | 40 | class FileWriter(File): |
paul@0 | 41 | |
paul@0 | 42 | "Writing basic data types to files." |
paul@0 | 43 | |
paul@0 | 44 | def write_number(self, number): |
paul@0 | 45 | |
paul@0 | 46 | "Write 'number' to the file using a variable length encoding." |
paul@0 | 47 | |
paul@0 | 48 | # Negative numbers are not supported. |
paul@0 | 49 | |
paul@0 | 50 | if number < 0: |
paul@0 | 51 | raise ValueError, "Number %r is negative." % number |
paul@0 | 52 | |
paul@0 | 53 | # Special case: one byte containing zero. |
paul@0 | 54 | |
paul@0 | 55 | elif number == 0: |
paul@4 | 56 | self.f.write(chr(0)) |
paul@0 | 57 | return |
paul@0 | 58 | |
paul@0 | 59 | # Write the number from least to most significant digits. |
paul@0 | 60 | |
paul@0 | 61 | bytes = [] |
paul@0 | 62 | |
paul@0 | 63 | while number != 0: |
paul@4 | 64 | lsd = number & 127 |
paul@4 | 65 | number = number >> 7 |
paul@4 | 66 | if number != 0: |
paul@4 | 67 | lsd |= 128 |
paul@0 | 68 | bytes.append(chr(lsd)) |
paul@0 | 69 | |
paul@0 | 70 | record = "".join(bytes) |
paul@0 | 71 | self.f.write(record) |
paul@0 | 72 | |
paul@2 | 73 | def write_string(self, s): |
paul@2 | 74 | |
paul@2 | 75 | "Write 's' to the file, recording its length." |
paul@2 | 76 | |
paul@2 | 77 | length = len(s) |
paul@2 | 78 | |
paul@2 | 79 | if not (0 <= length <= 255): |
paul@2 | 80 | raise ValueError, "String %r is too long." % s |
paul@2 | 81 | |
paul@4 | 82 | self.write_number(length) |
paul@2 | 83 | self.f.write(s) |
paul@2 | 84 | |
paul@0 | 85 | class FileReader(File): |
paul@0 | 86 | |
paul@0 | 87 | "Reading basic data types from files." |
paul@0 | 88 | |
paul@0 | 89 | def read_number(self): |
paul@0 | 90 | |
paul@0 | 91 | "Read a number from the file." |
paul@0 | 92 | |
paul@0 | 93 | # Read each byte, adding it to the number. |
paul@0 | 94 | |
paul@0 | 95 | shift = 0 |
paul@0 | 96 | number = 0 |
paul@4 | 97 | more = 1 |
paul@0 | 98 | |
paul@4 | 99 | while more: |
paul@4 | 100 | byte = self.f.read(1) |
paul@4 | 101 | if not byte: |
paul@4 | 102 | raise EOFError |
paul@4 | 103 | |
paul@4 | 104 | csd = ord(byte) |
paul@4 | 105 | more = csd & 128 != 0 |
paul@4 | 106 | if more: |
paul@4 | 107 | csd &= 127 |
paul@0 | 108 | number += (csd << shift) |
paul@4 | 109 | shift += 7 |
paul@0 | 110 | |
paul@0 | 111 | return number |
paul@0 | 112 | |
paul@2 | 113 | def read_string(self): |
paul@2 | 114 | |
paul@2 | 115 | "Read a string from the file." |
paul@2 | 116 | |
paul@4 | 117 | length = self.read_number() |
paul@2 | 118 | return self.f.read(length) |
paul@2 | 119 | |
paul@0 | 120 | # Specific classes. |
paul@0 | 121 | |
paul@0 | 122 | class PositionWriter(FileWriter): |
paul@0 | 123 | |
paul@0 | 124 | "Writing position information to files." |
paul@0 | 125 | |
paul@0 | 126 | def reset(self): |
paul@0 | 127 | self.last_docnum = 0 |
paul@0 | 128 | |
paul@0 | 129 | def write_positions(self, docnum, positions): |
paul@0 | 130 | |
paul@0 | 131 | "Write for the document 'docnum' the given 'positions'." |
paul@0 | 132 | |
paul@0 | 133 | if docnum < self.last_docnum: |
paul@0 | 134 | raise ValueError, "Document number %r is less than previous number %r." % (docnum, self.last_docnum) |
paul@0 | 135 | |
paul@0 | 136 | # Write the document number delta. |
paul@0 | 137 | |
paul@0 | 138 | self.write_number(docnum - self.last_docnum) |
paul@0 | 139 | |
paul@0 | 140 | # Write the number of positions. |
paul@0 | 141 | |
paul@0 | 142 | self.write_number(len(positions)) |
paul@0 | 143 | |
paul@0 | 144 | # Write the position deltas. |
paul@0 | 145 | |
paul@0 | 146 | last = 0 |
paul@0 | 147 | for position in positions: |
paul@0 | 148 | pos = position - last |
paul@0 | 149 | self.write_number(pos) |
paul@0 | 150 | last = position |
paul@0 | 151 | |
paul@0 | 152 | self.last_docnum = docnum |
paul@0 | 153 | |
paul@0 | 154 | def write_all_positions(self, doc_positions): |
paul@0 | 155 | |
paul@0 | 156 | """ |
paul@0 | 157 | Write all 'doc_positions' - a collection of tuples of the form (document |
paul@0 | 158 | number, position list) - to the file, returning the offset at which they |
paul@0 | 159 | were stored. |
paul@0 | 160 | """ |
paul@0 | 161 | |
paul@0 | 162 | # Reset the writer and record the current file offset. |
paul@0 | 163 | |
paul@0 | 164 | self.reset() |
paul@0 | 165 | offset = self.f.tell() |
paul@0 | 166 | |
paul@0 | 167 | # Write the number of documents. |
paul@0 | 168 | |
paul@0 | 169 | self.write_number(len(doc_positions)) |
paul@0 | 170 | |
paul@0 | 171 | # Write the positions. |
paul@0 | 172 | |
paul@0 | 173 | for docnum, positions in doc_positions: |
paul@0 | 174 | self.write_positions(docnum, positions) |
paul@0 | 175 | |
paul@0 | 176 | return offset |
paul@0 | 177 | |
paul@0 | 178 | class PositionReader(FileReader): |
paul@0 | 179 | |
paul@0 | 180 | "Reading position information from files." |
paul@0 | 181 | |
paul@0 | 182 | def reset(self): |
paul@0 | 183 | self.last_docnum = 0 |
paul@0 | 184 | |
paul@0 | 185 | def read_positions(self): |
paul@0 | 186 | |
paul@0 | 187 | "Read positions, returning a document number and a list of positions." |
paul@0 | 188 | |
paul@0 | 189 | # Read the document number delta and add it to the last number. |
paul@0 | 190 | |
paul@0 | 191 | self.last_docnum += self.read_number() |
paul@0 | 192 | |
paul@0 | 193 | # Read the number of positions. |
paul@0 | 194 | |
paul@0 | 195 | npositions = self.read_number() |
paul@0 | 196 | |
paul@0 | 197 | # Read the position deltas, adding each previous position to get the |
paul@0 | 198 | # appropriate collection of absolute positions. |
paul@0 | 199 | |
paul@0 | 200 | i = 0 |
paul@0 | 201 | last = 0 |
paul@0 | 202 | positions = [] |
paul@0 | 203 | |
paul@0 | 204 | while i < npositions: |
paul@0 | 205 | last += self.read_number() |
paul@0 | 206 | positions.append(last) |
paul@0 | 207 | i += 1 |
paul@0 | 208 | |
paul@0 | 209 | return self.last_docnum, positions |
paul@0 | 210 | |
paul@0 | 211 | def read_all_positions(self, offset): |
paul@0 | 212 | |
paul@0 | 213 | """ |
paul@0 | 214 | Read all positions from 'offset', seeking to that position in the file |
paul@0 | 215 | before reading. |
paul@0 | 216 | """ |
paul@0 | 217 | |
paul@0 | 218 | self.reset() |
paul@0 | 219 | self.f.seek(offset) |
paul@0 | 220 | |
paul@0 | 221 | # Read the number of documents. |
paul@0 | 222 | |
paul@0 | 223 | ndocuments = self.read_number() |
paul@0 | 224 | |
paul@0 | 225 | # Read all records. |
paul@0 | 226 | |
paul@0 | 227 | i = 0 |
paul@0 | 228 | doc_positions = [] |
paul@0 | 229 | |
paul@0 | 230 | while i < ndocuments: |
paul@0 | 231 | doc_positions.append(self.read_positions()) |
paul@0 | 232 | i += 1 |
paul@0 | 233 | |
paul@0 | 234 | return doc_positions |
paul@0 | 235 | |
paul@2 | 236 | class TermWriter(FileWriter): |
paul@2 | 237 | |
paul@2 | 238 | "Writing term information to files." |
paul@2 | 239 | |
paul@2 | 240 | def reset(self): |
paul@2 | 241 | self.last_term = "" |
paul@2 | 242 | self.last_offset = 0 |
paul@2 | 243 | |
paul@2 | 244 | def write_term(self, term, offset): |
paul@2 | 245 | |
paul@2 | 246 | """ |
paul@2 | 247 | Write the given 'term' and its position file 'offset' to the term |
paul@3 | 248 | information file. Return the offset after the term information was |
paul@3 | 249 | written to the file. |
paul@2 | 250 | """ |
paul@2 | 251 | |
paul@2 | 252 | # Too long terms are not currently supported. |
paul@2 | 253 | |
paul@2 | 254 | if len(term) > 255: |
paul@2 | 255 | raise ValueError, "Term %r is too long." % term |
paul@2 | 256 | |
paul@2 | 257 | # Write the prefix length and term suffix. |
paul@2 | 258 | |
paul@2 | 259 | common = len(commonprefix([self.last_term, term])) |
paul@2 | 260 | suffix = term[common:] |
paul@2 | 261 | |
paul@4 | 262 | self.write_number(common) |
paul@2 | 263 | self.write_string(suffix) |
paul@2 | 264 | |
paul@2 | 265 | # Write the offset delta. |
paul@2 | 266 | |
paul@2 | 267 | self.write_number(offset - self.last_offset) |
paul@2 | 268 | |
paul@2 | 269 | self.last_term = term |
paul@2 | 270 | self.last_offset = offset |
paul@2 | 271 | |
paul@3 | 272 | return self.f.tell() |
paul@3 | 273 | |
paul@2 | 274 | class TermReader(FileReader): |
paul@2 | 275 | |
paul@2 | 276 | "Reading term information from files." |
paul@2 | 277 | |
paul@2 | 278 | def reset(self): |
paul@2 | 279 | self.last_term = "" |
paul@2 | 280 | self.last_offset = 0 |
paul@2 | 281 | |
paul@2 | 282 | def read_term(self): |
paul@2 | 283 | |
paul@2 | 284 | """ |
paul@2 | 285 | Read a term and its position file offset from the term information file. |
paul@2 | 286 | """ |
paul@2 | 287 | |
paul@2 | 288 | # Read the prefix length and term suffix. |
paul@2 | 289 | |
paul@4 | 290 | common = self.read_number() |
paul@2 | 291 | suffix = self.read_string() |
paul@2 | 292 | |
paul@2 | 293 | self.last_term = self.last_term[:common] + suffix |
paul@2 | 294 | |
paul@2 | 295 | # Read the offset delta. |
paul@2 | 296 | |
paul@2 | 297 | self.last_offset += self.read_number() |
paul@2 | 298 | |
paul@2 | 299 | return self.last_term, self.last_offset |
paul@2 | 300 | |
paul@3 | 301 | def go_to_term(self, term, offset, info_offset): |
paul@3 | 302 | |
paul@3 | 303 | "Seek past the entry for 'term' having 'offset' to 'info_offset'." |
paul@3 | 304 | |
paul@3 | 305 | self.f.seek(info_offset) |
paul@3 | 306 | self.last_term = term |
paul@3 | 307 | self.last_offset = offset |
paul@3 | 308 | |
paul@3 | 309 | class TermIndexWriter(TermWriter): |
paul@3 | 310 | |
paul@3 | 311 | "Writing term dictionary index details to files." |
paul@3 | 312 | |
paul@3 | 313 | def reset(self): |
paul@3 | 314 | TermWriter.reset(self) |
paul@3 | 315 | self.last_info_offset = 0 |
paul@3 | 316 | |
paul@3 | 317 | def write_term(self, term, offset, info_offset): |
paul@3 | 318 | |
paul@3 | 319 | """ |
paul@3 | 320 | Write the given 'term' and its position file 'offset' to the term |
paul@3 | 321 | dictionary index file, along with the 'info_offset' in the term |
paul@3 | 322 | information file. |
paul@3 | 323 | """ |
paul@3 | 324 | |
paul@3 | 325 | TermWriter.write_term(self, term, offset) |
paul@3 | 326 | |
paul@3 | 327 | # Write the information file offset delta. |
paul@3 | 328 | |
paul@3 | 329 | self.write_number(info_offset - self.last_info_offset) |
paul@3 | 330 | self.last_info_offset = info_offset |
paul@3 | 331 | |
paul@3 | 332 | class TermIndexReader(TermReader): |
paul@3 | 333 | |
paul@3 | 334 | "Reading term dictionary index details from files." |
paul@3 | 335 | |
paul@3 | 336 | def reset(self): |
paul@3 | 337 | TermReader.reset(self) |
paul@3 | 338 | self.last_info_offset = 0 |
paul@3 | 339 | |
paul@3 | 340 | def read_term(self): |
paul@3 | 341 | |
paul@3 | 342 | """ |
paul@3 | 343 | Read a term, its position file offset, and its term information file |
paul@3 | 344 | offset from the term dictionary index file. |
paul@3 | 345 | """ |
paul@3 | 346 | |
paul@3 | 347 | term, offset = TermReader.read_term(self) |
paul@3 | 348 | |
paul@3 | 349 | # Read the offset delta. |
paul@3 | 350 | |
paul@3 | 351 | self.last_info_offset += self.read_number() |
paul@3 | 352 | |
paul@3 | 353 | return term, offset, self.last_info_offset |
paul@3 | 354 | |
paul@3 | 355 | class TermDictionaryWriter: |
paul@3 | 356 | |
paul@3 | 357 | "Writing term dictionaries." |
paul@3 | 358 | |
paul@3 | 359 | def __init__(self, info_writer, index_writer, interval): |
paul@3 | 360 | self.info_writer = info_writer |
paul@3 | 361 | self.index_writer = index_writer |
paul@3 | 362 | self.interval = interval |
paul@3 | 363 | self.entry = 0 |
paul@3 | 364 | |
paul@3 | 365 | def write_term(self, term, offset): |
paul@3 | 366 | |
paul@3 | 367 | """ |
paul@3 | 368 | Write the given 'term' and its position file 'offset' to the term |
paul@3 | 369 | information file and optionally to the index, making a dictionary entry. |
paul@3 | 370 | """ |
paul@3 | 371 | |
paul@3 | 372 | info_offset = self.info_writer.write_term(term, offset) |
paul@3 | 373 | |
paul@3 | 374 | if self.entry % self.interval == 0: |
paul@3 | 375 | self.index_writer.write_term(term, offset, info_offset) |
paul@3 | 376 | |
paul@3 | 377 | self.entry += 1 |
paul@3 | 378 | |
paul@3 | 379 | def close(self): |
paul@3 | 380 | self.info_writer.close() |
paul@3 | 381 | self.index_writer.close() |
paul@3 | 382 | |
paul@3 | 383 | class TermDictionaryReader: |
paul@3 | 384 | |
paul@3 | 385 | "Reading term dictionaries." |
paul@3 | 386 | |
paul@3 | 387 | def __init__(self, info_reader, index_reader): |
paul@3 | 388 | self.info_reader = info_reader |
paul@3 | 389 | self.index_reader = index_reader |
paul@3 | 390 | |
paul@3 | 391 | self.terms = [] |
paul@3 | 392 | try: |
paul@3 | 393 | while 1: |
paul@3 | 394 | self.terms.append(self.index_reader.read_term()) |
paul@3 | 395 | except EOFError: |
paul@3 | 396 | pass |
paul@3 | 397 | |
paul@3 | 398 | # Large numbers for ordering purposes. |
paul@3 | 399 | |
paul@3 | 400 | self.max_offset = self.terms[-1][1] |
paul@3 | 401 | self.max_info_offset = self.terms[-1][2] |
paul@3 | 402 | |
paul@3 | 403 | def find(self, term): |
paul@3 | 404 | |
paul@3 | 405 | "Find the position file offset of 'term' from the term dictionary." |
paul@3 | 406 | |
paul@3 | 407 | i = bisect_right(self.terms, (term, self.max_offset, self.max_info_offset)) - 1 |
paul@3 | 408 | |
paul@3 | 409 | # Get the entry position providing the term or one preceding it. |
paul@3 | 410 | |
paul@3 | 411 | if i == -1: |
paul@3 | 412 | return None |
paul@3 | 413 | |
paul@3 | 414 | found_term, offset, info_offset = self.terms[i] |
paul@3 | 415 | |
paul@3 | 416 | # Where the term is found immediately, return the offset. |
paul@3 | 417 | |
paul@3 | 418 | if term == found_term: |
paul@3 | 419 | return offset |
paul@3 | 420 | |
paul@3 | 421 | # Otherwise, seek past the index term's entry in the information file |
paul@3 | 422 | # and scan for the desired term. |
paul@3 | 423 | |
paul@3 | 424 | else: |
paul@3 | 425 | self.info_reader.go_to_term(found_term, offset, info_offset) |
paul@3 | 426 | try: |
paul@3 | 427 | while term > found_term: |
paul@3 | 428 | found_term, offset = self.info_reader.read_term() |
paul@3 | 429 | except EOFError: |
paul@3 | 430 | pass |
paul@3 | 431 | |
paul@3 | 432 | # If the term is found, return the offset. |
paul@3 | 433 | |
paul@3 | 434 | if term == found_term: |
paul@3 | 435 | return offset |
paul@3 | 436 | else: |
paul@3 | 437 | return None |
paul@3 | 438 | |
paul@3 | 439 | def close(self): |
paul@3 | 440 | self.info_reader.close() |
paul@3 | 441 | self.index_reader.close() |
paul@3 | 442 | |
paul@0 | 443 | # vim: tabstop=4 expandtab shiftwidth=4 |