Esempio n. 1
0
def run(args):

    data = loader.load_treebanks(TREEBANK_PATH)
    train_data, dev_data, test_data = loader.train_test_split(
        data, 0.8, 0.1, 0.1)
    words, embeddings = loader.load_word_embeddings(EMBEDDING_PATH)

    pcfg = PCFG(train_data)
    pcfg.train(train_data)
    pcfg.set_oov(OOV, words, embeddings)

    if args.generate_output:
        output = pcfg.generate_output(test_data)

    if args.evaluation:
        accs, nb_no_parse = pcfg.predict(test_data[:2])

    if args.parse:
        corpus = []
        with open(args.txt_path, 'r') as f:
            corpus = f.read().split('\n')
        pcfg.parse_from_txt(corpus)
def run(args):

    has_effect = False

    if args:
        try:

            train_corpus, val_corpus, test_corpus = data.get_train_val_test()
            words, embeddings = data.get_polyglot_words_embeddings()

            parser = PCFG()
            parser.learn_probabilities_and_rules(train_corpus)
            parser.set_oov_module(OovModule, words, embeddings)

            if args.inference:

                get_gold(parser, test_corpus, filename='evaluation_data.gold')
                get_predictions(parser,
                                test_corpus,
                                filename='evaluation_data.parser_output')

            if args.evaluation:
                evaluation('evaluation_data.gold',
                           'evaluation_data.parser_output')

            if args.parse:
                parser.parse_from_txt(args.txt_path)

        except Exception as e:
            logger.exception(e)
            logger.error("Uhoh, the script halted with an error.")
    else:
        if not has_effect:
            logger.error(
                "Script halted without any effect. To run code, use command:\npython3 main.py <args>"
            )