Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bennekrouf/similar-sled
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Jan 19, 2024
2 parents f64d0a4 + 7a9e8cd commit 8d006c3
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/domain/exercise/generate_one_exercise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rand::seq::SliceRandom;

use crate::domain::exercise::extract_and_shuffle_options::extract_and_shuffle_options;
use crate::domain::exercise::select_random_verse_index::select_random_verse_index;
use crate::domain::similar::sourate_from_verse::sourate_name_from_verse;
Expand All @@ -14,12 +13,20 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer
let exclude_verse = Some(exercise.verses[valid_verse_index].verse.clone());
let extracted_values = extract_and_shuffle_options(&mut exercise.verses, exercise_type, &exclude_verse);

<<<<<<< HEAD
// Extract and keep the correct answer separate
let correct_alternative = Alternative { verse: Some(exercise.verses[valid_verse_index].verse.clone()) };

// Prepare incorrect alternatives and shuffle them
let mut incorrect_alternatives: Vec<Alternative> = extracted_values.into_iter().map(|value| {
=======
// Ensure the correct answer is added first
let valid_verse = exercise.verses.get(valid_verse_index).unwrap();
let mut alternatives = vec![Alternative { verse: Some(valid_verse.verse.clone()) }];

// Add random alternatives
alternatives.extend(extracted_values.into_iter().map(|value| {
>>>>>>> c6a5876cabcf450c486e9315cd3f5973dbcb37a3
match exercise_type {
ExerciseType::FindDiscriminant => {
Alternative {
Expand Down Expand Up @@ -47,19 +54,26 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer
},
_ => unimplemented!(),
}
}).filter(|alt| alt.verse != Some(valid_verse.verse.clone())));
alternatives.truncate(4);
// Add the correct answer
// let valid_verse = exercise.verses.get(valid_verse_index).unwrap();
// alternatives.push(Alternative { verse: Some(valid_verse.verse.clone()) });
alternatives.shuffle(&mut rand::thread_rng());
}).filter(|alt| alt.verse != Some(exercise.verses[valid_verse_index].verse.clone()))
.collect();

incorrect_alternatives.shuffle(&mut rand::thread_rng());

// Combine correct and incorrect alternatives, correct answer at the end
incorrect_alternatives.push(correct_alternative);

// Truncate the alternatives to the desired length (e.g., 4)
incorrect_alternatives.truncate(4);

// Use the combined and truncated list as the final alternatives
let alternatives = incorrect_alternatives;

// Return None if there are not enough alternatives
if alternatives.len() <= 1 {
None
} else {
let mut generated_exercise = Exercise {
statement: valid_verse.clone(),
statement: exercise.verses[valid_verse_index].clone(),
alternatives,
exercise_type: exercise_type.clone(),
};
Expand Down

0 comments on commit 8d006c3

Please sign in to comment.