data, table = chains.process_corpus("train.csv") table.write("all.json") print("Successfully built the model") sys.exit(0) else: if not path.exists("all.json"): print("Load the model first using --load") # load the pre-built model with open("all.json") as fp: table = chains.ProbabilityTable(json.load(fp)) total, correct = 0, 0 # load testing data test = chains.load_data("test.csv") with open("answers.txt", "w") as f: # header for the csv f.write("InputStoryid,AnswerRightEnding\n") test_tqdm = tqdm.tqdm(test) for t in test_tqdm: one, two = parse_test_instance(t) _, one_deps = chains.extract_dependency_pairs(one) _, two_deps = chains.extract_dependency_pairs(two) # logic to choose between one and two prog_one = chains.protagonist(one) prog_two = chains.protagonist(two)
return [ chains.ParsedStory(id, id, chains.nlp(" ".join(story[2:6] + [a])), *(sentences + [chains.nlp(a)])) for a in alternatives ] def story_answer(story): """Tells you the correct answer. Return (storyid, index). 1 for the first ending, 2 for the second ending""" #obviously you can't use this information until you've chosen your answer! return story.InputStoryid, story.AnswerRightEnding # Load training data and build the model #data, table = chains.process_corpus("train.csv", 100) #print(table.pmi("move", "nsubj", "move", "nsubj")) # load the pre-built model with open("all.json") as fp: table = chains.ProbabilityTable(json.load(fp)) # load testing data test = chains.load_data("val.csv") for t in test: one, two = parse_test_instance(t) one_deps = chains.extract_dependency_pairs(one) pprint(one[2:]) pprint(two[2:]) # logic to choose between one and two pprint("answer:" + str(story_answer(t)))