def generate_questions(sentence): q = QuestionGenerator() question_list = q.generate_question(sentence, ['All']) for sent in question_list: qa = sent.split('\t') if (len(qa) >= 3): print("0 " + qa[0]) print("1 " + qa[1]) print("3 " + qa[3]) print(" ")
class Game: def __init__(self, game_engine, player_name, difficulty_level = GameDifficulty.Easy): self.player_name = player_name self.game_engine = game_engine self.question_generator = QuestionGenerator(difficulty=difficulty_level) self.curr_question = None def is_game_over(self): return not self.game_engine.current or self.game_engine.current.is_end_state() def get_question(self): self.curr_question = self.question_generator.generate_question() return self.curr_question def submit_answer(self, answer): is_correct = False if self.curr_question.get_answer() == int(answer): is_correct = True self.game_engine.execute_action(Action.Win) else: self.game_engine.execute_action(Action.Loose) return is_correct def get_current_state(self): return str(self.game_engine.current)
import sys import argparse from question_generator import QuestionGenerator def add_arguments(): parser = argparse.ArgumentParser() parser.add_argument( '-s', '--sentence', type=str, help="The sentence for which questions are to be generated.") parser.add_argument('-t', '--question_type', type=str, default=['Wh', 'Are', 'Who', 'Do'], choices=['Wh', 'Are', 'Who', 'Do', 'All'], help='The types of questions to be generated.') return parser.parse_args() if __name__ == '__main__': args = add_arguments() if not args.s: sys.stdout.write('No input given\n') sys.exit() q = QuestionGenerator() question_list = q.generate_question(args.s, args.t) print(question_list)
def article_to_paragraphs(file_name): with open('/Users/arul/Projects/pubmedbot/txt/' + file_name, 'r') as f: text = f.read() new_text = re.sub(r'\n\n.*et al\n', "", text) new_text = re.sub(r'\.\n\n', "\.\n", new_text) new_text = re.sub(r'Page [0-9]+ of [0-9]+\n', "", new_text) new_text = clean_unidentified_characters(new_text) new_text = re.sub(r'(\n )+', "\n", new_text).strip() new_text = re.sub(r'\n+', "\n", new_text).strip() paragraphs = re.split(r'\.( )*\n', new_text) return paragraphs qgen = QuestionGenerator() file_name = '12113563.txt' paras = article_to_paragraphs(file_name) qa_id = 1 doc_id = 1 data_vec = [] paragraphs_vec = [] for para in paras: if para is not None: sentences = sent_tokenize(para) qas_vec = [] for sent in sentences: if sent is not None: qa_pairs = qgen.generate_question(sent, ['Wh', 'Are', 'Who', 'Do'])
# requests.post('http://[::]:9000/?properties={"annotators":"tokenize,ssplit,pos","outputFormat":"json"}', # data = {'data': "tmp"}).text sp = spacy.load('en_core_web_lg') # Add pipes for SpaCy neuralcoref.add_to_pipe(sp) #sp.add_pipe(coref, name='neuralcoref') tr = TextRank() sp.add_pipe(tr.PipelineComponent, name="textrank", last=True) with open(args.input, 'r') as f: content = f.read() sp_file = sp(content) phrase_ranks = sp_file._.phrases coref_resolved = get_resolved(sp_file, sp_file._.coref_clusters) sentences = [sent.string.strip() for sent in sp(coref_resolved).sents] prune_sentences(sentences) parser = CoreNLPParser(url='http://localhost:9000') generator = QuestionGenerator(sentences, parser, sp) print("Generating Questions...") questions, _ = generator.get_questions() print("Done.") print("Ranking Questions...") ranked_questions = rank(questions, phrase_ranks) print("Done.") with open(args.output, "w+") as f: f.write("\n".join(ranked_questions))
def __init__(self, game_engine, player_name, difficulty_level = GameDifficulty.Easy): self.player_name = player_name self.game_engine = game_engine self.question_generator = QuestionGenerator(difficulty=difficulty_level) self.curr_question = None
from question_generator import QuestionGenerator q = QuestionGenerator() print(q.generate_question("According to Copernicus the earth is only a planet like the others, and not even the biggest one, while the sun is the most important body in the system, and the stars probably too far away for any motion of the earth to affect their apparent places."))
from config import ServerConfig from question_generator import QuestionGenerator from server import Server c = ServerConfig() qg = QuestionGenerator(c.QuestionFilepath) qg.PrintAll() s = Server(c, qg) s.Run()