-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
68 lines (49 loc) · 2.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import argparse
import logging
from pcfg import PCFG
from oov import OovModule
from data import data
from evaluate import get_gold, get_predictions, evaluation
# Logging
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(logging.Formatter('[%(asctime)s %(levelname)-3s @%(name)s] %(message)s', datefmt='%H:%M:%S'))
logging.basicConfig(level=logging.DEBUG, handlers=[console])
logger = logging.getLogger("PCFG Parser")
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>")
def path(d):
try:
assert os.path.exists(d)
return d
except Exception as e:
raise argparse.ArgumentTypeError("Example {} cannot be located.".format(d))
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description='Run scripts for the NLP assignment')
argparser.add_argument('--inference', action='store_true', help='performs inference on the test set')
argparser.add_argument('--evaluation', action='store_true', help='performs POS evaluation')
argparser.add_argument('--parse', action='store_true', help='parses .txt file')
argparser.add_argument('txt_path', nargs="?", type=path, help='path for txt file to be parsed')
run(argparser.parse_args())