Ejemplo n.º 1
0
def main_process(input_file):
	sentence_count = 0
	gen_questions = []
	final_questions = []

	with open(input_file, "r") as f:
		for line in f:
			sentences = ss.sentence_segment(line, tri_gram=False)
			sentence_count += 1

			for i in range(len(sentences)):
				questions = qg.generate(sentences[i].pos)
				for tp in questions:
					(question, answer, answer_index) = tp
					ans_eng, ans_index = wnth.get_general_info(answer)
					answer_item = _wi.word_item(answer, ans_eng, ans_index, None)
					question_item = _qui.question_item(sentences[i], sentence_count, question, answer_item, answer_index)
					gen_questions.append(question_item)
			
	ranked_questions, qrank_scores = qr.rank_question(gen_questions)
	for question in ranked_questions:
		choices = chg.generate_choices(question)
		if choices != []:
			question.add_choices(choices)
			final_questions.append(question)

	return final_questions
	def generate_choices(self, question_item):
		answer = question_item.answer
		if self.is_thai_word(answer.word):
			siblings, eng_siblings, index, hypernyms = self.wn.get_siblings(answer.eng_word, answer.index)
			
			choices_set = set()
			all_choices = []
			for i in range(len(siblings)):
				for j in range(len(siblings[i])):
					new_item = word_item.word_item(siblings[i][j], eng_siblings[i][j], index[i][j], hypernyms[i])

					sibling_thai = siblings[i][j]
					if sibling_thai not in choices_set and self.is_thai_word(sibling_thai):
						choices_set.add(sibling_thai)
						all_choices.append(new_item)
			
			if len(all_choices) >= 3:
				ranked_choices, choices_with_score = self.choice_ranker.rank_choices(question_item, all_choices)
				choices = [question_item.answer] + ranked_choices[0:3]
			else:
				return []
			# choices = self.rank_choices(all_choices, answer)
		else:
			return []

		# random.shuffle(choices)
		return choices
	def __init__(self, *args, **kwargs):
		if len(args) == 5:
			(sentence, sentence_no, question, answer, answer_index) = args
			self.sentence = sentence
			self.sentence_no = sentence_no
			self.question = question
			self.answer = answer
			self.answer_index = answer_index
			self.choices = None
			self.asked_choices = None
		elif "from_str" in kwargs:
			attributes = ast.literal_eval(kwargs["from_str"])
			for key in attributes:
				if key == "sentence":
					self.sentence = _sentence.sentence(from_str=attributes["sentence"])
				elif key == "choices":
					self.choices = [_word_item.word_item(from_str=choice_str) for choice_str in attributes["choices"]]
				elif key == "answer":
					self.answer = _word_item.word_item(from_str=attributes["answer"])
				else:
					setattr(self, key, attributes[key])

		self.evals = []
def get_question_item(question_file, pos_file):
	sentences = []
	sentence_with_pos = []
	with open(pos_file) as f:
		for line in f:
			read_list = ast.literal_eval(line.strip())
			for a_sentence in read_list:
				sentences.append("".join([word for (word, _) in a_sentence]))
				sentence_with_pos.append(a_sentence)

	sentence_count = 0
	all_question_items = []
	with open(question_file) as f:
		for line in f:
			read_list = ast.literal_eval(line.strip())
			for a_sentence in read_list:
				sentence_item = sentence.sentence(sentences[sentence_count], sentence_with_pos[sentence_count])
				for a_question in a_sentence:
					(question_sentence, answer, choices) = a_question
					answer_item = word_item.word_item(answer)
					answer_index = find_blank_index([tp[0] for tp in sentence_with_pos[sentence_count]], question_sentence)
					all_generated_choices = _cg.choice_generate(answer_item)
					choice_items = []
					for a_choice in choices:
						if str(a_choice) == answer:
							choice_items.append(answer_item)
						else:
							for gen_choice in all_generated_choices:
								if str(gen_choice) == a_choice:
									choice_items.append(gen_choice)
									break

					# print([str(choice) for choice in choice_items])
					question = question_item.question_item(sentence_item, sentence_count, question_sentence, answer_item, answer_index)
					question.add_choices(choice_items)
					all_question_items.append(question)

				sentence_count += 1

	return all_question_items