1 #!/usr/bin/env python 2 3 """ 4 Phrase iterators providing navigation over common positions for a number of 5 different terms. 6 7 Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT ANY 15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 16 PARTICULAR PURPOSE. See the GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License along 19 with this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from itermerge import itermerge 23 from bisect import insort_right 24 25 class PhraseIterator(itermerge): 26 27 "Iteration over many terms." 28 29 def __init__(self, sequences): 30 itermerge.__init__(self, sequences) 31 32 def _add_iter(self, iterator, i): 33 34 "Store the details of the given 'iterator' at position 'i'." 35 36 insort_right(self.iters, (len(iterator), i, iterator)) 37 38 def next(self): 39 if self.iters: 40 freq, i, it = self.iters[0] 41 while 1: 42 doc, positions = it.next() 43 values = [(i, positions)] 44 for freq, i, it in self.iters[1:]: 45 positions = it.from_document(doc) 46 if positions is None: 47 break 48 else: 49 values.append((i, positions)) 50 else: 51 values.sort() 52 return doc, [positions for (i, positions) in values] 53 else: 54 raise StopIteration 55 56 # vim: tabstop=4 expandtab shiftwidth=4