aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-07-29 08:13:18 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-07-29 08:13:18 +0200
commite012aa80006a0de8135929243e1311958b791048 (patch)
tree8bd6878cc47d75633c150fa6f413577fc670d0f9
parent87a7b528482a1850731001ee8ccaccfd75c64824 (diff)
downloadmihi-e012aa80006a0de8135929243e1311958b791048.tar.gz
mihi-e012aa80006a0de8135929243e1311958b791048.zip
Add weight to words
This allows for the user to give more importance to some words than others, as that will be taken into account when running `practice`. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--crates/cli/src/words.rs16
-rw-r--r--lib/mihi/src/lib.rs47
-rw-r--r--lib/mihi/src/migrate.rs8
3 files changed, 57 insertions, 14 deletions
diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs
index 4743001..72748ae 100644
--- a/crates/cli/src/words.rs
+++ b/crates/cli/src/words.rs
@@ -260,6 +260,21 @@ fn ask_for_word_based_on(enunciated: String, word: Word) -> Result<Word, String>
return Err("abort!".to_string());
};
+ let Ok(raw_weight) = Text::new("Weight:")
+ .with_initial_value(word.weight.to_string().as_str())
+ .prompt()
+ else {
+ return Err("abort!".to_string());
+ };
+ let Ok(weight) = raw_weight.parse::<usize>() else {
+ return Err(format!("bad value for inflection ID '{inflection}'"));
+ };
+ if weight > 10 {
+ return Err(format!(
+ "weight has to be an integer between 0 and 10, but {weight} was given"
+ ));
+ }
+
let raw_flags = serde_json::to_string(&word.flags).unwrap();
let Ok(flags) = Editor::new("Flags:")
@@ -316,6 +331,7 @@ fn ask_for_word_based_on(enunciated: String, word: Word) -> Result<Word, String>
flags: serde_json::from_str(&trimmed_flags).unwrap(),
succeeded: 0,
steps: 0,
+ weight,
})
}
diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs
index 95cb6e6..7a5bfc9 100644
--- a/lib/mihi/src/lib.rs
+++ b/lib/mihi/src/lib.rs
@@ -204,6 +204,7 @@ pub struct Word {
pub flags: Value,
pub succeeded: usize,
pub steps: usize,
+ pub weight: usize,
}
impl Word {
@@ -232,6 +233,7 @@ impl Word {
flags: serde_json::from_str("{}").unwrap(),
succeeded: 0,
steps: 0,
+ weight: 5,
}
}
@@ -324,12 +326,29 @@ pub fn create_word(word: Word) -> Result<(), String> {
let conn = get_connection()?;
match conn.execute(
- "INSERT INTO words (enunciated, particle, language_id, declension_id, conjugation_id, kind, category, regular, locative, gender, suffix, flags, translation, updated_at, created_at) \
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, datetime('now'), datetime('now'))",
- params![word.enunciated, word.particle, word.language as usize,
- word.declension_id, word.conjugation_id, word.kind, word.category as usize,
- word.regular, word.locative, word.gender as usize, word.suffix,
- serde_json::to_string(&word.flags).unwrap(), serde_json::to_string(&word.translation).unwrap()]) {
+ "INSERT INTO words (enunciated, particle, language_id, declension_id, \
+ conjugation_id, kind, category, regular, locative, \
+ gender, suffix, flags, translation, weight, \
+ updated_at, created_at) \
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, \
+ datetime('now'), datetime('now'))",
+ params![
+ word.enunciated,
+ word.particle,
+ word.language as usize,
+ word.declension_id,
+ word.conjugation_id,
+ word.kind,
+ word.category as usize,
+ word.regular,
+ word.locative,
+ word.gender as usize,
+ word.suffix,
+ serde_json::to_string(&word.flags).unwrap(),
+ serde_json::to_string(&word.translation).unwrap(),
+ word.weight
+ ],
+ ) {
Ok(_) => Ok(()),
Err(e) => Err(format!("could not create '{}': {}", word.enunciated, e)),
}
@@ -346,7 +365,8 @@ pub fn update_word(word: Word) -> Result<(), String> {
"UPDATE words \
SET enunciated = ?2, particle = ?3, declension_id = ?4, conjugation_id = ?5, \
kind = ?6, category = ?7, regular = ?8, locative = ?9, gender = ?10, \
- suffix = ?11, flags = ?12, translation = ?13, updated_at = datetime('now') \
+ suffix = ?11, flags = ?12, translation = ?13, weight = ?14, \
+ updated_at = datetime('now') \
WHERE id = ?1",
params![
word.id,
@@ -361,7 +381,8 @@ pub fn update_word(word: Word) -> Result<(), String> {
word.gender as usize,
word.suffix,
serde_json::to_string(&word.flags).unwrap(),
- serde_json::to_string(&word.translation).unwrap()
+ serde_json::to_string(&word.translation).unwrap(),
+ word.weight
],
) {
Ok(_) => Ok(()),
@@ -403,7 +424,7 @@ pub fn find_by(enunciated: &str) -> Result<Word, String> {
.prepare(
"SELECT id, enunciated, particle, language_id, declension_id, conjugation_id, \
kind, category, regular, locative, gender, suffix, translation, \
- succeeded, steps, flags \
+ succeeded, steps, flags, weight \
FROM words \
WHERE enunciated = ?1",
)
@@ -430,6 +451,7 @@ pub fn find_by(enunciated: &str) -> Result<Word, String> {
succeeded: row.get(13).unwrap(),
steps: row.get(14).unwrap(),
flags: serde_json::from_str(&row.get::<usize, String>(15).unwrap()).unwrap(),
+ weight: row.get(16).unwrap(),
}),
None => Err("no words were found with this enunciate".to_string()),
},
@@ -442,10 +464,10 @@ pub fn select_random_words(category: Category, number: usize) -> Result<Vec<Word
.prepare(
"SELECT id, enunciated, particle, language_id, declension_id, conjugation_id, \
kind, category, regular, locative, gender, suffix, translation, \
- succeeded, steps \
+ succeeded, steps, flags, weight \
FROM words \
WHERE category = ?1 AND translation != '{}' \
- ORDER BY succeeded ASC, updated_at DESC
+ ORDER BY weight DESC, succeeded ASC, updated_at DESC
LIMIT ?2",
)
.unwrap();
@@ -469,7 +491,8 @@ pub fn select_random_words(category: Category, number: usize) -> Result<Vec<Word
translation: serde_json::from_str(&row.get::<usize, String>(12).unwrap()).unwrap(),
succeeded: row.get(13).unwrap(),
steps: row.get(14).unwrap(),
- flags: serde_json::from_str("{}").unwrap(),
+ flags: serde_json::from_str(&row.get::<usize, String>(15).unwrap()).unwrap(),
+ weight: row.get(16).unwrap(),
});
}
Ok(res)
diff --git a/lib/mihi/src/migrate.rs b/lib/mihi/src/migrate.rs
index 48c078e..9b1e831 100644
--- a/lib/mihi/src/migrate.rs
+++ b/lib/mihi/src/migrate.rs
@@ -17,12 +17,16 @@ CREATE TABLE IF NOT EXISTS "words" (
"gender" integer,
"created_at" datetime(6) NOT NULL,
"updated_at" datetime(6) NOT NULL,
- "last_asked_at" datetime(6) NOT NULL,
"suffix" varchar,
"language_id" integer,
- "translation" json,
+ "succeeded" integer,
+ "steps" integer DEFAULT 0 NOT NULL,
+ "translation" jsonb DEFAULT '{}',
"pending" boolean DEFAULT 0,
"flags" jsonb DEFAULT '{}',
+ "weight" integer DEFAULT 0 NOT NULL,
+
+ CHECK (weight >= 0 AND weight <= 10),
FOREIGN KEY ("conjugation_id") REFERENCES "conjugations" ("id"),
FOREIGN KEY ("declension_id") REFERENCES "declensions" ("id")