[lang][library] Languages: liber-lang (words with UTF-16 offsets, lemmatizer trait with tables and hand-written rules, coverage, warm-up, levels, Ludus concept ids, Gale-Church alignment with anchors); known words, the known band, familiar books and editions as events
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! Events: what a device did, with a hybrid logical clock. The order of events is total and the same everywhere.
|
||||
|
||||
use crate::library::{BookFile, BookInfo, Colour, Field, GoalKind, HabitGoal, Mark, Outcome, Person};
|
||||
use crate::library::{BookFile, BookInfo, Colour, Field, GoalKind, HabitGoal, Mark, Outcome, Person, WordMark};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
@@ -168,4 +168,22 @@ pub enum Op {
|
||||
MedalSeen {
|
||||
medal: String,
|
||||
},
|
||||
/// Day 3: the reader marked a word (a folded lemma, see `liber_lang::fold`). Last writer wins.
|
||||
Word {
|
||||
lang: String,
|
||||
lemma: String,
|
||||
mark: WordMark,
|
||||
},
|
||||
/// Day 3: the band of a frequency list the reader knows.
|
||||
KnownBand {
|
||||
lang: String,
|
||||
list: String,
|
||||
upto: u32,
|
||||
},
|
||||
/// Day 3: a book file is an edition in another language (`None`: it is the book's own language again).
|
||||
Edition {
|
||||
book: String,
|
||||
sha256: String,
|
||||
language: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -129,6 +129,12 @@ pub fn ops_from(lib: &Library) -> Vec<Op> {
|
||||
if b.to_read {
|
||||
ops.push(set(Field::ToRead, json!(true)));
|
||||
}
|
||||
if b.familiar {
|
||||
ops.push(set(Field::Familiar, json!(true)));
|
||||
}
|
||||
for (sha, lang) in &b.editions {
|
||||
ops.push(Op::Edition { book: id.clone(), sha256: sha.clone(), language: Some(lang.clone()) });
|
||||
}
|
||||
let pos = |p: &crate::library::Position| Op::Position {
|
||||
book: id.clone(),
|
||||
cfi: p.cfi.clone(),
|
||||
@@ -189,6 +195,14 @@ pub fn ops_from(lib: &Library) -> Vec<Op> {
|
||||
for m in &lib.medals_seen {
|
||||
ops.push(Op::MedalSeen { medal: m.clone() });
|
||||
}
|
||||
for (lang, words) in &lib.words {
|
||||
for (lemma, mark) in words {
|
||||
ops.push(Op::Word { lang: lang.clone(), lemma: lemma.clone(), mark: *mark });
|
||||
}
|
||||
}
|
||||
for (lang, band) in &lib.bands {
|
||||
ops.push(Op::KnownBand { lang: lang.clone(), list: band.list.clone(), upto: band.upto });
|
||||
}
|
||||
ops
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +134,8 @@ pub enum Field {
|
||||
Tags,
|
||||
ToRead,
|
||||
Cover,
|
||||
/// Day 3: the reader has read this book in their own language (it opens at 90% coverage, not 95%).
|
||||
Familiar,
|
||||
}
|
||||
|
||||
impl Field {
|
||||
@@ -158,6 +160,7 @@ impl Field {
|
||||
Field::Tags => "tags",
|
||||
Field::ToRead => "to_read",
|
||||
Field::Cover => "cover",
|
||||
Field::Familiar => "familiar",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,6 +212,12 @@ pub struct Book {
|
||||
pub position: Option<Position>,
|
||||
/// The most recent position, when it is behind the furthest: "Jump back to page X?".
|
||||
pub last_position: Option<Position>,
|
||||
/// Day 3: read before in the reader's own language.
|
||||
#[serde(default)]
|
||||
pub familiar: bool,
|
||||
/// Day 3: the language of each file (sha256 → BCP 47 tag) when it differs from the book's: a second edition.
|
||||
#[serde(default)]
|
||||
pub editions: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
@@ -231,6 +240,8 @@ impl Book {
|
||||
cover: None,
|
||||
position: None,
|
||||
last_position: None,
|
||||
familiar: false,
|
||||
editions: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,6 +318,34 @@ impl Default for HabitGoal {
|
||||
}
|
||||
}
|
||||
|
||||
/// Day 3: how the reader knows a word (a lemma). Words not in the map are unknown.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum WordMark {
|
||||
/// Looked up and being practised.
|
||||
Learning,
|
||||
Known,
|
||||
/// Back to unknown (removes the word).
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// Day 3: the band of a frequency list the reader says they know ("the 2,000 most common words").
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Band {
|
||||
/// The list's id (its file name, as decided in docs/03_languages.md).
|
||||
pub list: String,
|
||||
/// The first `upto` lemmas of the list count as known.
|
||||
pub upto: u32,
|
||||
}
|
||||
|
||||
/// Caps for word events from sync (hostile input).
|
||||
pub const MAX_LEMMA: usize = 64;
|
||||
pub const MAX_WORDS: usize = 200_000;
|
||||
|
||||
fn lang_ok(l: &str) -> bool {
|
||||
!l.is_empty() && l.len() <= 8 && l.bytes().all(|b| b.is_ascii_lowercase())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Goal {
|
||||
pub year: i32,
|
||||
@@ -328,6 +367,12 @@ pub struct Library {
|
||||
/// Medals already shown (the medals are computed, see `liber_habit::medals`).
|
||||
#[serde(default)]
|
||||
pub medals_seen: BTreeSet<String>,
|
||||
/// Day 3: language (`en`, `de`, `la`) → lemma → known or learning.
|
||||
#[serde(default)]
|
||||
pub words: BTreeMap<String, BTreeMap<String, WordMark>>,
|
||||
/// Day 3: language → the frequency band marked as known.
|
||||
#[serde(default)]
|
||||
pub bands: BTreeMap<String, Band>,
|
||||
#[serde(skip)]
|
||||
aliases: BTreeMap<String, String>,
|
||||
#[serde(skip)]
|
||||
@@ -350,6 +395,8 @@ impl PartialEq for Library {
|
||||
&& self.goals == o.goals
|
||||
&& self.habit == o.habit
|
||||
&& self.medals_seen == o.medals_seen
|
||||
&& self.words == o.words
|
||||
&& self.bands == o.bands
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,6 +580,7 @@ impl Library {
|
||||
Field::Tags => set_of(value).map(|v| b.tags = v).is_some(),
|
||||
Field::ToRead => value.as_bool().map(|v| b.to_read = v).is_some(),
|
||||
Field::Cover => serde_json::from_value(value.clone()).map(|v| b.cover = v).is_ok(),
|
||||
Field::Familiar => value.as_bool().map(|v| b.familiar = v).is_some(),
|
||||
};
|
||||
if ok {
|
||||
self.versions.insert(key, e.id.clone());
|
||||
@@ -702,6 +750,44 @@ impl Library {
|
||||
self.medals_seen.insert(medal.clone());
|
||||
}
|
||||
}
|
||||
Op::Word { lang, lemma, mark } => {
|
||||
if !lang_ok(lang) || lemma.is_empty() || lemma.chars().count() > MAX_LEMMA {
|
||||
return;
|
||||
}
|
||||
let words = self.words.entry(lang.clone()).or_default();
|
||||
match mark {
|
||||
WordMark::Unknown => {
|
||||
words.remove(lemma);
|
||||
}
|
||||
m if words.len() < MAX_WORDS || words.contains_key(lemma) => {
|
||||
words.insert(lemma.clone(), *m);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if words.is_empty() {
|
||||
self.words.remove(lang);
|
||||
}
|
||||
}
|
||||
Op::KnownBand { lang, list, upto } => {
|
||||
if lang_ok(lang) && !list.is_empty() && list.len() <= 64 {
|
||||
self.bands.insert(lang.clone(), Band { list: list.clone(), upto: (*upto).min(1_000_000) });
|
||||
}
|
||||
}
|
||||
Op::Edition { book, sha256, language } => {
|
||||
let ok = sha256.len() == 64 && sha256.bytes().all(|b| b.is_ascii_hexdigit());
|
||||
if let Some(b) = self.book_mut(book)
|
||||
&& ok
|
||||
{
|
||||
match language {
|
||||
Some(l) if !l.is_empty() && l.len() <= 35 => {
|
||||
b.editions.insert(sha256.to_ascii_lowercase(), l.clone());
|
||||
}
|
||||
_ => {
|
||||
b.editions.remove(&sha256.to_ascii_lowercase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user