Fix reading exercise with more than 3 words.
This commit is contained in:
59
app.py
59
app.py
@@ -231,7 +231,7 @@ def grade_writing_task_1():
|
||||
token_count = count_tokens(message)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, message, token_count,
|
||||
["comment"],
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
GRADING_TEMPERATURE)
|
||||
response['fixed_text'] = get_fixed_text(answer)
|
||||
return response
|
||||
else:
|
||||
@@ -707,24 +707,7 @@ def get_reading_passage_1_question():
|
||||
topic = request.args.get('topic', default=random.choice(topics))
|
||||
req_exercises = request.args.getlist('exercises')
|
||||
difficulty = request.args.get("difficulty", default=random.choice(difficulties))
|
||||
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_1_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic)
|
||||
app.logger.info("Generated passage: " + str(passage))
|
||||
start_id = 1
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
return gen_reading_passage_1(topic, req_exercises, difficulty)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
@@ -737,24 +720,7 @@ def get_reading_passage_2_question():
|
||||
topic = request.args.get('topic', default=random.choice(topics))
|
||||
req_exercises = request.args.getlist('exercises')
|
||||
difficulty = request.args.get("difficulty", default=random.choice(difficulties))
|
||||
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_2_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic)
|
||||
app.logger.info("Generated passage: " + str(passage))
|
||||
start_id = 14
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
return gen_reading_passage_2(topic, req_exercises, difficulty)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
@@ -767,24 +733,7 @@ def get_reading_passage_3_question():
|
||||
topic = request.args.get('topic', default=random.choice(topics))
|
||||
req_exercises = request.args.getlist('exercises')
|
||||
difficulty = request.args.get("difficulty", default=random.choice(difficulties))
|
||||
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_3_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic)
|
||||
app.logger.info("Generated passage: " + str(passage))
|
||||
start_id = 27
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
return gen_reading_passage_3(topic, req_exercises, difficulty)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@@ -16,6 +16,68 @@ from wonderwords import RandomWord
|
||||
nltk.download('words')
|
||||
|
||||
|
||||
def gen_reading_passage_1(topic, req_exercises, difficulty):
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_1_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic)
|
||||
start_id = 1
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
if contains_empty_dict(exercises):
|
||||
return gen_reading_passage_1(topic, req_exercises, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
|
||||
|
||||
def gen_reading_passage_2(topic, req_exercises, difficulty):
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_2_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic)
|
||||
start_id = 14
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
if contains_empty_dict(exercises):
|
||||
return gen_reading_passage_1(topic, req_exercises, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
|
||||
|
||||
def gen_reading_passage_3(topic, req_exercises, difficulty):
|
||||
if (len(req_exercises) == 0):
|
||||
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
|
||||
|
||||
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_3_EXERCISES, len(req_exercises))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic)
|
||||
start_id = 27
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id, difficulty)
|
||||
if contains_empty_dict(exercises):
|
||||
return gen_reading_passage_1(topic, req_exercises, difficulty)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
"difficulty": difficulty
|
||||
}
|
||||
|
||||
def divide_number_into_parts(number, parts):
|
||||
if number < parts:
|
||||
return None
|
||||
@@ -309,8 +371,12 @@ def generate_reading_exercises(passage: str, req_exercises: list, number_of_exer
|
||||
print("Added trueFalse: " + str(question))
|
||||
elif req_exercise == "writeBlanks":
|
||||
question = gen_write_blanks_exercise(passage, number_of_exercises, start_id, difficulty)
|
||||
if answer_word_limit_ok(question):
|
||||
exercises.append(question)
|
||||
print("Added write blanks: " + str(question))
|
||||
else:
|
||||
exercises.append({})
|
||||
print("Did not add write blanks because it did not respect word limit")
|
||||
elif req_exercise == "paragraphMatch":
|
||||
question = gen_paragraph_match_exercise(passage, number_of_exercises, start_id)
|
||||
exercises.append(question)
|
||||
@@ -321,13 +387,26 @@ def generate_reading_exercises(passage: str, req_exercises: list, number_of_exer
|
||||
return exercises
|
||||
|
||||
|
||||
def generate_listening_conversation_exercises(conversation: str, req_exercises: list, number_of_exercises_q, start_id, difficulty):
|
||||
def answer_word_limit_ok(question):
|
||||
# Check if any option in any solution has more than three words
|
||||
return not any(len(option.split()) > 3
|
||||
for solution in question["solutions"]
|
||||
for option in solution["solution"])
|
||||
|
||||
|
||||
def contains_empty_dict(arr):
|
||||
return any(elem == {} for elem in arr)
|
||||
|
||||
|
||||
def generate_listening_conversation_exercises(conversation: str, req_exercises: list, number_of_exercises_q, start_id,
|
||||
difficulty):
|
||||
exercises = []
|
||||
for req_exercise in req_exercises:
|
||||
number_of_exercises = number_of_exercises_q.get()
|
||||
|
||||
if req_exercise == "multipleChoice":
|
||||
question = gen_multiple_choice_exercise_listening_conversation(conversation, number_of_exercises, start_id, difficulty)
|
||||
question = gen_multiple_choice_exercise_listening_conversation(conversation, number_of_exercises, start_id,
|
||||
difficulty)
|
||||
exercises.append(question)
|
||||
print("Added multiple choice: " + str(question))
|
||||
elif req_exercise == "writeBlanksQuestions":
|
||||
@@ -351,25 +430,30 @@ def generate_listening_conversation_exercises(conversation: str, req_exercises:
|
||||
return exercises
|
||||
|
||||
|
||||
def generate_listening_monologue_exercises(monologue: str, req_exercises: list, number_of_exercises_q, start_id, difficulty):
|
||||
def generate_listening_monologue_exercises(monologue: str, req_exercises: list, number_of_exercises_q, start_id,
|
||||
difficulty):
|
||||
exercises = []
|
||||
for req_exercise in req_exercises:
|
||||
number_of_exercises = number_of_exercises_q.get()
|
||||
|
||||
if req_exercise == "multipleChoice":
|
||||
question = gen_multiple_choice_exercise_listening_monologue(monologue, number_of_exercises, start_id, difficulty)
|
||||
question = gen_multiple_choice_exercise_listening_monologue(monologue, number_of_exercises, start_id,
|
||||
difficulty)
|
||||
exercises.append(question)
|
||||
print("Added multiple choice: " + str(question))
|
||||
elif req_exercise == "writeBlanksQuestions":
|
||||
question = gen_write_blanks_questions_exercise_listening_monologue(monologue, number_of_exercises, start_id, difficulty)
|
||||
question = gen_write_blanks_questions_exercise_listening_monologue(monologue, number_of_exercises, start_id,
|
||||
difficulty)
|
||||
exercises.append(question)
|
||||
print("Added write blanks questions: " + str(question))
|
||||
elif req_exercise == "writeBlanksFill":
|
||||
question = gen_write_blanks_notes_exercise_listening_monologue(monologue, number_of_exercises, start_id, difficulty)
|
||||
question = gen_write_blanks_notes_exercise_listening_monologue(monologue, number_of_exercises, start_id,
|
||||
difficulty)
|
||||
exercises.append(question)
|
||||
print("Added write blanks notes: " + str(question))
|
||||
elif req_exercise == "writeBlanksForm":
|
||||
question = gen_write_blanks_form_exercise_listening_monologue(monologue, number_of_exercises, start_id, difficulty)
|
||||
question = gen_write_blanks_form_exercise_listening_monologue(monologue, number_of_exercises, start_id,
|
||||
difficulty)
|
||||
exercises.append(question)
|
||||
print("Added write blanks form: " + str(question))
|
||||
|
||||
@@ -379,7 +463,8 @@ def generate_listening_monologue_exercises(monologue: str, req_exercises: list,
|
||||
|
||||
|
||||
def gen_multiple_choice_exercise(text: str, quantity: int, start_id, difficulty):
|
||||
gen_multiple_choice_for_text = "Generate " + str(quantity) + " " + difficulty + " difficulty multiple choice questions for this text: " \
|
||||
gen_multiple_choice_for_text = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty multiple choice questions for this text: " \
|
||||
"'" + text + "'\n" \
|
||||
"Use this format: \"questions\": [{\"id\": \"9\", \"options\": [{\"id\": \"A\", \"text\": " \
|
||||
"\"Economic benefits\"}, {\"id\": \"B\", \"text\": \"Government regulations\"}, {\"id\": \"C\", \"text\": " \
|
||||
@@ -469,8 +554,9 @@ def gen_true_false_not_given_exercise(text: str, quantity: int, start_id, diffic
|
||||
|
||||
|
||||
def gen_write_blanks_exercise(text: str, quantity: int, start_id, difficulty):
|
||||
gen_short_answer_questions = "Generate " + str(quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers " \
|
||||
"(max 3 words per answer), about this text: '" + text + "'. " \
|
||||
gen_short_answer_questions = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers, " \
|
||||
"must have maximum 3 words per answer, about this text: '" + text + "'. " \
|
||||
"Provide your answer in this JSON format: {\"questions\": [{\"question\": question, " \
|
||||
"\"possible_answers\": [\"answer_1\", \"answer_2\"]}]}"
|
||||
|
||||
@@ -562,7 +648,8 @@ def gen_multiple_choice_exercise_listening_conversation(text: str, quantity: int
|
||||
|
||||
|
||||
def gen_multiple_choice_exercise_listening_monologue(text: str, quantity: int, start_id, difficulty):
|
||||
gen_multiple_choice_for_text = "Generate " + str(quantity) + " " + difficulty + " difficulty multiple choice questions for this monologue: " \
|
||||
gen_multiple_choice_for_text = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty multiple choice questions for this monologue: " \
|
||||
"'" + text + "'"
|
||||
token_count = count_tokens(gen_multiple_choice_for_text)["n_tokens"]
|
||||
mc_questions = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_multiple_choice_for_text, token_count,
|
||||
@@ -586,7 +673,8 @@ def gen_multiple_choice_exercise_listening_monologue(text: str, quantity: int, s
|
||||
|
||||
|
||||
def gen_write_blanks_questions_exercise_listening_conversation(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_questions = "Generate " + str(quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers " \
|
||||
gen_write_blanks_questions = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers " \
|
||||
"(max 3 words per answer), about a monologue and" \
|
||||
"respond in this JSON format: {\"questions\": [{\"question\": question, " \
|
||||
"\"possible_answers\": [\"answer_1\", \"answer_2\"]}]}." \
|
||||
@@ -608,7 +696,8 @@ def gen_write_blanks_questions_exercise_listening_conversation(text: str, quanti
|
||||
|
||||
|
||||
def gen_write_blanks_questions_exercise_listening_monologue(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_questions = "Generate " + str(quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers " \
|
||||
gen_write_blanks_questions = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty short answer questions, and the possible answers " \
|
||||
"(max 3 words per answer), about a monologue and" \
|
||||
"respond in this JSON format: {\"questions\": [{\"question\": question, " \
|
||||
"\"possible_answers\": [\"answer_1\", \"answer_2\"]}]}." \
|
||||
@@ -630,7 +719,8 @@ def gen_write_blanks_questions_exercise_listening_monologue(text: str, quantity:
|
||||
|
||||
|
||||
def gen_write_blanks_notes_exercise_listening_conversation(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_notes = "Generate " + str(quantity) + " " + difficulty + " difficulty notes taken from the conversation and and respond in this " \
|
||||
gen_write_blanks_notes = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty notes taken from the conversation and and respond in this " \
|
||||
"JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'"
|
||||
|
||||
token_count = count_tokens(gen_write_blanks_notes)["n_tokens"]
|
||||
@@ -655,7 +745,8 @@ def gen_write_blanks_notes_exercise_listening_conversation(text: str, quantity:
|
||||
|
||||
|
||||
def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_notes = "Generate " + str(quantity) + " " + difficulty + " difficulty notes taken from the monologue and respond in this " \
|
||||
gen_write_blanks_notes = "Generate " + str(
|
||||
quantity) + " " + difficulty + " difficulty notes taken from the monologue and respond in this " \
|
||||
"JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'"
|
||||
|
||||
token_count = count_tokens(gen_write_blanks_notes)["n_tokens"]
|
||||
@@ -680,7 +771,8 @@ def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int
|
||||
|
||||
|
||||
def gen_write_blanks_form_exercise_listening_conversation(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_form = "Generate a form with " + str(quantity) + " " + difficulty + " difficulty key-value pairs about the conversation. " \
|
||||
gen_write_blanks_form = "Generate a form with " + str(
|
||||
quantity) + " " + difficulty + " difficulty key-value pairs about the conversation. " \
|
||||
"The conversation is this: '" + text + "'"
|
||||
token_count = count_tokens(gen_write_blanks_form)["n_tokens"]
|
||||
form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count,
|
||||
@@ -703,7 +795,8 @@ def gen_write_blanks_form_exercise_listening_conversation(text: str, quantity: i
|
||||
|
||||
|
||||
def gen_write_blanks_form_exercise_listening_monologue(text: str, quantity: int, start_id, difficulty):
|
||||
gen_write_blanks_form = "Generate a form with " + str(quantity) + " " + difficulty + " difficulty key-value pairs about the monologue. " \
|
||||
gen_write_blanks_form = "Generate a form with " + str(
|
||||
quantity) + " " + difficulty + " difficulty key-value pairs about the monologue. " \
|
||||
"The monologue is this: '" + text + "'"
|
||||
token_count = count_tokens(gen_write_blanks_form)["n_tokens"]
|
||||
form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count,
|
||||
@@ -775,7 +868,8 @@ def gen_multiple_choice_level(quantity: int, start_id=1):
|
||||
all_exams = get_all("level")
|
||||
seen_keys = set()
|
||||
for i in range(len(question["questions"])):
|
||||
question["questions"][i], seen_keys = replace_exercise_if_exists(all_exams, question["questions"][i], question,
|
||||
question["questions"][i], seen_keys = replace_exercise_if_exists(all_exams, question["questions"][i],
|
||||
question,
|
||||
seen_keys)
|
||||
return {
|
||||
"id": str(uuid.uuid4()),
|
||||
|
||||
Reference in New Issue
Block a user