paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | from parallel import start, Exchange |
paulb@4 | 4 | from Dict import Indexer, Searcher, update |
paulb@4 | 5 | |
paulb@4 | 6 | def apply_indexer(channel, filename): |
paulb@4 | 7 | print "Indexing", filename |
paulb@4 | 8 | indexer = Indexer(filename) |
paulb@4 | 9 | channel.send(indexer.get_index()) |
paulb@4 | 10 | |
paulb@6 | 11 | def get_searcher(filenames, window_size): |
paulb@4 | 12 | master_index = {} |
paulb@4 | 13 | |
paulb@4 | 14 | # Start indexing. |
paulb@4 | 15 | |
paulb@6 | 16 | for i in range(0, len(filenames), window_size): |
paulb@6 | 17 | channels = [] |
paulb@6 | 18 | for filename in filenames[i:i + window_size]: |
paulb@6 | 19 | channels.append(start(apply_indexer, filename)) |
paulb@4 | 20 | |
paulb@6 | 21 | # Start listening for responses. |
paulb@4 | 22 | |
paulb@6 | 23 | exchange = Exchange(channels) |
paulb@6 | 24 | while len(channels) > 0: |
paulb@6 | 25 | print "Waiting for %d channels..." % len(channels) |
paulb@6 | 26 | for channel in exchange.ready(): |
paulb@6 | 27 | index = channel.receive() |
paulb@6 | 28 | update(master_index, index) |
paulb@6 | 29 | channels.remove(channel) |
paulb@4 | 30 | |
paulb@4 | 31 | # Provide a search interface. |
paulb@4 | 32 | |
paulb@4 | 33 | return Searcher(master_index) |
paulb@4 | 34 | |
paulb@4 | 35 | if __name__ == "__main__": |
paulb@4 | 36 | import sys |
paulb@6 | 37 | window_size, filenames = int(sys.argv[1]), sys.argv[2:] |
paulb@6 | 38 | searcher = get_searcher(filenames, window_size) |
paulb@4 | 39 | while 1: |
paulb@4 | 40 | print "Pattern:", |
paulb@4 | 41 | pattern = raw_input() |
paulb@4 | 42 | print searcher.find(pattern) |
paulb@4 | 43 | |
paulb@4 | 44 | # vim: tabstop=4 expandtab shiftwidth=4 |