|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This script is for illustration purposes only, it ignores quite a number |
| 4 | +of natural language features, so it should be considered an example only |
| 5 | +in a technical sense. |
| 6 | +""" |
| 7 | + |
| 8 | +from argparse import ArgumentParser |
| 9 | +import os.path |
| 10 | +import sys |
| 11 | +from mpi4py import MPI |
| 12 | + |
| 13 | +terminators = set(['.', '?', '!']) |
| 14 | + |
| 15 | + |
| 16 | +def extract_prefix(file_name, start_pos, end_pos): |
| 17 | + global terminators |
| 18 | + prefix_str = '' |
| 19 | + index = 0 |
| 20 | + with open(file_name, 'r') as file: |
| 21 | + file.seek(start_pos) |
| 22 | + if start_pos < end_pos: |
| 23 | + c = file.read(1) |
| 24 | + if not c.isupper(): |
| 25 | + file.seek(start_pos) |
| 26 | + while index + start_pos < end_pos: |
| 27 | + c = file.read(1) |
| 28 | + prefix_str += c |
| 29 | + index += 1 |
| 30 | + if c in terminators: |
| 31 | + break |
| 32 | + return (prefix_str, start_pos + index) |
| 33 | + |
| 34 | + |
| 35 | +def extract_suffix(file_name, start_pos, end_pos): |
| 36 | + global terminators |
| 37 | + suffix_str = '' |
| 38 | + with open(file_name, 'r') as file: |
| 39 | + file.seek(end_pos) |
| 40 | + if end_pos >= start_pos: |
| 41 | + c = file.read(1) |
| 42 | + if c not in terminators: |
| 43 | + file.seek(end_pos) |
| 44 | + while end_pos >= start_pos: |
| 45 | + c = file.read(1) |
| 46 | + if c in terminators: |
| 47 | + break |
| 48 | + else: |
| 49 | + suffix_str = c + suffix_str |
| 50 | + current_pos = file.tell() |
| 51 | + file.seek(current_pos - 2) |
| 52 | + end_pos -= 1 |
| 53 | + return (suffix_str, end_pos) |
| 54 | + |
| 55 | + |
| 56 | +def count_sentences(file_name, start_pos, end_pos): |
| 57 | + """Parse a given string, returning the number of sentences, as well |
| 58 | + as the prefix to the first sentence, as well as the suffix to the |
| 59 | + last.""" |
| 60 | + global terminators |
| 61 | + with open(file_name, 'r') as file: |
| 62 | + file.seek(start_pos) |
| 63 | + count = 0 |
| 64 | + index = start_pos |
| 65 | + while index <= end_pos: |
| 66 | + c = file.read(1) |
| 67 | + if c in terminators: |
| 68 | + count += 1 |
| 69 | + index += 1 |
| 70 | + return count |
| 71 | + |
| 72 | + |
| 73 | +def main(): |
| 74 | + comm = MPI.COMM_WORLD |
| 75 | + size = comm.size |
| 76 | + rank = comm.rank |
| 77 | + arg_parser = ArgumentParser(description='count sentences in a text file') |
| 78 | + arg_parser.add_argument('-v', dest='is_verbose', action='store_true', |
| 79 | + help='verbose output for debugging') |
| 80 | + arg_parser.add_argument('file', metavar='FILE', help='file to parse') |
| 81 | + options = arg_parser.parse_args() |
| 82 | + file_size = os.path.getsize(options.file) |
| 83 | + chunck_size = file_size//size |
| 84 | + start_pos = chunck_size*rank |
| 85 | + if rank + 1 < size: |
| 86 | + end_pos = start_pos + chunck_size - 1 |
| 87 | + else: |
| 88 | + end_pos = file_size - 1 |
| 89 | + if options.is_verbose: |
| 90 | + msg = "rank {0} reading '{1}' from {2} to {3}\n" |
| 91 | + sys.stderr.write(msg.format(rank, options.file, start_pos, end_pos)) |
| 92 | + |
| 93 | + prefix, new_start_pos = extract_prefix(options.file, start_pos, end_pos) |
| 94 | +# send new_start_pos - 1 to previous, to use as new_end_pos |
| 95 | + if rank > 0: |
| 96 | + comm.isend(new_start_pos - 1, dest=rank - 1) |
| 97 | +# receive new_new_end_pos from next |
| 98 | + if rank < size - 1: |
| 99 | + end_pos = comm.recv(source=rank + 1) |
| 100 | + if options.is_verbose: |
| 101 | + msg = "rank {0} reading '{1}' from {2} to {3}\n" |
| 102 | + sys.stderr.write(msg.format(rank, options.file, new_start_pos, |
| 103 | + end_pos)) |
| 104 | + count = count_sentences(options.file, new_start_pos, end_pos) |
| 105 | + if options.is_verbose: |
| 106 | + sys.stderr.write('rank {0} counted {1}\n'.format(rank, count)) |
| 107 | + total = comm.reduce(count, op=MPI.SUM, root=0) |
| 108 | + if rank == 0: |
| 109 | + print('sentences: {0}'.format(total)) |
| 110 | + return 0 |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + status = main() |
| 114 | + sys.exit(status) |
0 commit comments