#!/usr/bin/env python3

from argparse import ArgumentParser, FileType
import sys

import fictionary
import fictionary.model


def create_model(files):
    """
    Read through one or more wordlist files and make_model a Markov object
    representing the words and a set of all the words fed into the Markov object.

    ``files`` should be a sequence of file paths. Each file will be opened,
    and each line should contain a single word.  Words beginning with a
    capital letter or containing an apostrophe will be rejected. Each word
    is fed into a Markov object as a sequence of characters.
    """
    model = fictionary.model.Model()
    for path in files:
        for line in open(path, encoding='utf-8'):
            word = line.strip()
            if not line[0].isupper() and "'" not in word:
                model.feed(word)
    return model


def main(argv=sys.argv[1:]):
    ap = ArgumentParser()
    ap.add_argument('-o', '--output', type=FileType(mode='w', encoding='utf-8'), required=True)
    ap.add_argument('input', nargs='+')

    args = ap.parse_args(argv)

    model = fictionary.model.Model()
    for path in args.input:
        with open(path, encoding='utf-8') as fin:
            for line in fin:
                word = line.strip()
                if not word[0].isupper() and "'" not in word:
                    model.feed(line.strip())
    model.write(args.output)


if __name__ == '__main__':
    main()