#!/usr/bin/env python3 # -*- coding: utf-8 -*- # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import argparse from sentence_generator import Sentence_generator def parse_file(f, sep=None, optional_sep=False): r = {} for lines in (block.split('\n') for block in f.read().split('\n\n') if block.strip() != ''): lines[0] = lines[0].strip() if lines[0][:1] == '#': continue lines_stripped = (line.lstrip() for line in lines[1:]) lines_checked = (line for line in lines_stripped if line[:1] not in ('', '#')) if sep: if optional_sep: value = [(sep in line) and line.split(sep) or line for line in lines_checked] else: value = [line.split(sep) for line in lines_checked] else: value = [line for line in lines_checked] if len(value) == 0: continue for key in lines[0].split(): if key in r: r[key].extend(value) else: r[key] = value return r class increment(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): namespace.__dict__[self.dest] += 1 parser = argparse.ArgumentParser() parser.add_argument('-i', default=1, type=int, help='number of sentences to generate') parser.add_argument('-n', '--non-terminals', default='non-terminals_fr', type=argparse.FileType('r')) parser.add_argument('-t', '--terminals', default='terminals_fr', type=argparse.FileType('r')) parser.add_argument('-u', '--unique', default=False, action='store_true', help='make sure the same sentence does not appear multiple times') parser.add_argument('-v', '--verbose', default=0, action=increment, nargs=0) args = parser.parse_args() generator = Sentence_generator( parse_file(args.non_terminals, sep=' '), parse_file(args.terminals, sep='\t', optional_sep=True), unique=args.unique, verbosity=args.verbose ) for i in range(args.i): print(generator.generate_sentence()) if args.verbose > 0: print()