Files
Liber-public/crates/liber-lang/src/lib.rs
T

65 lines
2.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Liber Domi, Day 3: reading a loved book in another language.
//!
//! Pure: text and tables in, values out. No files, no network, no clock, no model. The app supplies chapter text
//! (the same text the reader shows, so offsets are the reader's UTF-16 offsets) and, once Mark has decided their
//! licences, lemma tables; this crate never ships outside data. The day page is `docs/03_languages.md` in the app repo.
//!
//! - [`token`]: words, numbers and punctuation by Unicode word boundaries, with UTF-16 offsets.
//! - [`lemma`]: the [`Lemmatizer`] trait, a lookup [`lemma::Table`] and hand-written [`lemma::Rules`] per language.
//! - [`profile`]: a book's lemma counts per chapter; coverage, "comes up N more times", the warm-up's words.
//! - [`level`]: the climb (levels by known lemmas) and the unlock rule.
//! - [`align`]: two editions of one book, aligned chapters → paragraphs → sentences (Gale–Church with anchors).
//! - [`concept`]: Ludus concept ids for vocabulary.
pub mod align;
pub mod concept;
pub mod lemma;
pub mod level;
pub mod profile;
pub mod token;
pub use lemma::Lemmatizer;
pub use profile::Profile;
pub use token::{Kind, Token, tokens};
use serde::{Deserialize, Serialize};
/// A language Liber can read in (Day 3: English, German, Latin).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Lang {
En,
De,
La,
}
impl Lang {
/// From a BCP 47 tag or a book's `dc:language` (`de`, `de-AT`, `ger`, `deu`, `lat`, `en-GB`, …).
pub fn from_tag(tag: &str) -> Option<Lang> {
let primary = tag.trim().split(['-', '_']).next()?.to_ascii_lowercase();
match primary.as_str() {
"en" | "eng" => Some(Lang::En),
"de" | "ger" | "deu" => Some(Lang::De),
"la" | "lat" => Some(Lang::La),
_ => None,
}
}
pub fn code(self) -> &'static str {
match self {
Lang::En => "en",
Lang::De => "de",
Lang::La => "la",
}
}
}
/// Lowercase and NFC: the one form lemmas are compared and stored in. Latin also folds `j` → `i` and `v` → `u`
/// (editions differ in both).
pub fn fold(word: &str, lang: Lang) -> String {
use unicode_normalization::UnicodeNormalization;
let lower: String = word.nfc().flat_map(char::to_lowercase).collect();
let lower = lower.replace(['\u{2019}', '\u{2018}'], "'");
if lang == Lang::La { lower.replace('j', "i").replace('v', "u") } else { lower }
}