Add paragraphMatch.

This commit is contained in:
Cristiano Ferreira
2024-03-19 23:05:44 +00:00
parent bed07ca819
commit 6e65732e94
3 changed files with 77 additions and 6 deletions

View File

@@ -1,4 +1,6 @@
import queue
import string
import nltk
import random
import re
@@ -309,6 +311,10 @@ def generate_reading_exercises(passage: str, req_exercises: list, number_of_exer
question = gen_write_blanks_exercise(passage, number_of_exercises, start_id, difficulty)
exercises.append(question)
print("Added write blanks: " + str(question))
elif req_exercise == "paragraphMatch":
question = gen_paragraph_match_exercise(passage, number_of_exercises, start_id)
exercises.append(question)
print("Added paragraph match: " + str(question))
start_id = start_id + number_of_exercises
@@ -483,6 +489,53 @@ def gen_write_blanks_exercise(text: str, quantity: int, start_id, difficulty):
}
def gen_paragraph_match_exercise(text: str, quantity: int, start_id):
paragraphs = assign_letters_to_paragraphs(text)
heading_prompt = (
'For every paragraph of the list generate a minimum 5 word heading for it. Provide your answer in this JSON format: '
'{"headings": [ {"heading": "first paragraph heading"}, {"heading": "second paragraph heading"}]}\n'
'The paragraphs are these: ' + str(paragraphs))
token_count = count_tokens(heading_prompt)["n_tokens"]
headings = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, heading_prompt, token_count,
["headings"],
GEN_QUESTION_TEMPERATURE)["headings"]
options = []
for i, paragraph in enumerate(paragraphs, start=0):
paragraph["heading"] = headings[i]
options.append({
"id": paragraph["letter"],
"sentence": paragraph["paragraph"]
})
random.shuffle(paragraphs)
sentences = []
for i, paragraph in enumerate(paragraphs, start=start_id):
sentences.append({
"id": i,
"sentence": paragraph["heading"],
"solution": paragraph["letter"]
})
return {
"id": str(uuid.uuid4()),
"allowRepetition": False,
"options": options,
"prompt": "Choose the correct heading for paragraphs from the list of headings below.",
"sentences": sentences[:quantity],
"type": "matchSentences"
}
def assign_letters_to_paragraphs(paragraphs):
result = []
letters = iter(string.ascii_uppercase)
for paragraph in paragraphs.split("\n"):
result.append({'paragraph': paragraph.strip(), 'letter': next(letters)})
return result
def gen_multiple_choice_exercise_listening_conversation(text: str, quantity: int, start_id, difficulty):
gen_multiple_choice_for_text = "Generate " + str(
quantity) + " " + difficulty + " difficulty multiple choice questions of 4 options of for this conversation: " \