def shortest_term_span(positions): """ Given a list of positions in a corpus, returns the shortest span of words that contain all query terms. """ # Initialize our list of lists where each list corresponds # to the locations within the document for a term indices = [0] * len(positions) min_window = window = get_window(positions, indices) # Iteratively moving the minimum index forward finds us our # minimum span while True: min_index = get_min_index(positions, window) if min_index is None: return sorted(min_window) indices[min_index] += 1 window = get_window(positions, indices) if list_range(min_window) > list_range(window): min_window = window if list_range(min_window) == len(positions): return sorted(min_window)
def test_get_window(self): self.assertEquals( get_window([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [0, 1, 2]), [1, 5, 9])