1.1 --- a/iixr.py Wed Aug 26 23:36:02 2009 +0200
1.2 +++ b/iixr.py Thu Aug 27 00:02:50 2009 +0200
1.3 @@ -413,7 +413,7 @@
1.4 self.max_offset = self.terms[-1][1]
1.5 self.max_info_offset = self.terms[-1][2]
1.6
1.7 - def find(self, term):
1.8 + def find_term(self, term):
1.9
1.10 "Find the position file offset of 'term' from the term dictionary."
1.11
1.12 @@ -453,7 +453,7 @@
1.13
1.14 "Return the documents and positions at which the given 'term' is found."
1.15
1.16 - offset = self.find(term)
1.17 + offset = self.find_term(term)
1.18 if offset is None:
1.19 return None
1.20 else:
1.21 @@ -464,4 +464,45 @@
1.22 self.index_reader.close()
1.23 self.position_reader.close()
1.24
1.25 +class IndexWriter:
1.26 +
1.27 + "Building term information and writing it to the term dictionary."
1.28 +
1.29 + def __init__(self, dict_writer):
1.30 + self.dict_writer = dict_writer
1.31 + self.terms = {}
1.32 +
1.33 + def add_position(self, term, docnum, position):
1.34 +
1.35 + """
1.36 + Add a position entry for the given 'term' in the document with the given
1.37 + 'docnum', indicating the given 'position'.
1.38 + """
1.39 +
1.40 + if not self.terms.has_key(term):
1.41 + doc_positions = self.terms[term] = {}
1.42 + else:
1.43 + doc_positions = self.terms[term]
1.44 +
1.45 + if not doc_positions.has_key(docnum):
1.46 + doc = doc_positions[docnum] = []
1.47 + else:
1.48 + doc = doc_positions[docnum]
1.49 +
1.50 + doc.append(position)
1.51 +
1.52 + def close(self):
1.53 +
1.54 + # Get the terms in order.
1.55 +
1.56 + terms = self.terms.items()
1.57 + terms.sort()
1.58 +
1.59 + for term, doc_positions in terms:
1.60 + doc_positions = doc_positions.items()
1.61 + doc_positions.sort()
1.62 + self.dict_writer.write_term_positions(term, doc_positions)
1.63 +
1.64 + self.dict_writer.close()
1.65 +
1.66 # vim: tabstop=4 expandtab shiftwidth=4