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@8 | 24 | while exchange.active(): |
paulb@8 | 25 | print "Waiting for %d channels..." % exchange.active() |
paulb@6 | 26 | for channel in exchange.ready(): |
paulb@6 | 27 | index = channel.receive() |
paulb@6 | 28 | update(master_index, index) |
paulb@8 | 29 | exchange.remove(channel) |
paulb@8 | 30 | channel.close() |
paulb@4 | 31 | |
paulb@4 | 32 | # Provide a search interface. |
paulb@4 | 33 | |
paulb@4 | 34 | return Searcher(master_index) |
paulb@4 | 35 | |
paulb@4 | 36 | if __name__ == "__main__": |
paulb@8 | 37 | import sys, os |
paulb@8 | 38 | window_size, directory = int(sys.argv[1]), sys.argv[2] |
paulb@8 | 39 | filenames = [os.path.join(directory, filename) for filename in os.listdir(directory)] |
paulb@8 | 40 | |
paulb@6 | 41 | searcher = get_searcher(filenames, window_size) |
paulb@4 | 42 | while 1: |
paulb@4 | 43 | print "Pattern:", |
paulb@4 | 44 | pattern = raw_input() |
paulb@4 | 45 | print searcher.find(pattern) |
paulb@4 | 46 | |
paulb@4 | 47 | # vim: tabstop=4 expandtab shiftwidth=4 |