82 lines
3.5 KiB
Rust
82 lines
3.5 KiB
Rust
//! Mandatory test 2: per language, a fixture of inflected forms maps to the expected lemmas at or above a stated
|
|
//! accuracy (recorded in the app repo's `docs/03_languages.md`). Rules only: the tables wait for Mark's data decision.
|
|
|
|
use liber_lang::lemma::{Chain, Headwords, Lemmatizer, Rules, Table, TableError, TableLimits};
|
|
use liber_lang::{Lang, fold};
|
|
|
|
fn fixture(lang: Lang) -> Vec<(String, String)> {
|
|
let text = match lang {
|
|
Lang::En => include_str!("fixtures/lemma_en.tsv"),
|
|
Lang::De => include_str!("fixtures/lemma_de.tsv"),
|
|
Lang::La => include_str!("fixtures/lemma_la.tsv"),
|
|
};
|
|
text.lines()
|
|
.filter(|l| !l.is_empty() && !l.starts_with('#'))
|
|
.map(|l| {
|
|
let (f, le) = l.split_once('\t').expect("form<TAB>lemma");
|
|
(f.to_owned(), le.to_owned())
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn accuracy(lang: Lang, lem: &dyn Lemmatizer) -> (usize, usize, Vec<String>) {
|
|
let cases = fixture(lang);
|
|
let mut wrong = Vec::new();
|
|
let ok = cases
|
|
.iter()
|
|
.filter(|(form, want)| {
|
|
let got = lem.lemma(form);
|
|
let hit = got.as_deref() == Some(fold(want, lang).as_str());
|
|
if !hit {
|
|
wrong.push(format!("{form} → {got:?} (want {want})"));
|
|
}
|
|
hit
|
|
})
|
|
.count();
|
|
(ok, cases.len(), wrong)
|
|
}
|
|
|
|
/// Floors: the measured rules-only accuracy, rounded down to the next 5%. A change that lowers one fails here.
|
|
const FLOOR: [(Lang, f64); 3] = [(Lang::En, 0.85), (Lang::De, 0.75), (Lang::La, 0.75)];
|
|
|
|
#[test]
|
|
fn rules_reach_the_stated_accuracy() {
|
|
for (lang, floor) in FLOOR {
|
|
let (ok, all, wrong) = accuracy(lang, &Rules::new(lang));
|
|
let acc = ok as f64 / all as f64;
|
|
println!("{lang:?}: {ok}/{all} = {:.1}% misses: {}", acc * 100.0, wrong.join("; "));
|
|
assert!(acc >= floor, "{lang:?} rules: {:.1}% < {:.0}%", acc * 100.0, floor * 100.0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn headwords_choose_among_the_rules_candidates() {
|
|
// German: `schönen` is a verb reading first (schönen → schönen? no: schön+en) unless the headwords say otherwise.
|
|
let rules = Rules::new(Lang::De);
|
|
assert!(rules.candidates("schönen").contains(&"schön".to_owned()));
|
|
let chain = Chain { table: None, headwords: Some(Headwords::new(Lang::De, ["schön", "Haus"])), rules };
|
|
assert_eq!(chain.lemma("schönen").as_deref(), Some("schön"));
|
|
// Latin: `regem` is third declension; with `rex` absent the rules cannot know, with a table they do.
|
|
let table = Table::from_tsv(Lang::La, "regem\trex\nregis\trex\n", TableLimits::default()).expect("table");
|
|
let chain = Chain { table: Some(table), headwords: None, rules: Rules::new(Lang::La) };
|
|
assert_eq!(chain.lemma("Regem").as_deref(), Some("rex"));
|
|
assert!(chain.knows("regis"));
|
|
assert!(!chain.knows("amat"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_table_is_hostile_input() {
|
|
let limits = TableLimits { max_bytes: 100, max_entries: 2, max_word: 8 };
|
|
assert_eq!(Table::from_tsv(Lang::En, &"a\tb\n".repeat(40), limits).err(), Some(TableError::TooLarge));
|
|
assert_eq!(Table::from_tsv(Lang::En, "a\tb\nc\td\ne\tf\n", limits).err(), Some(TableError::TooManyEntries));
|
|
// Malformed lines, comments, over-long words and empty fields are skipped, not fatal.
|
|
let t = Table::from_tsv(
|
|
Lang::En,
|
|
"# c\nnope\n\tx\nverylongword\tx\nwent\tgo\r\n",
|
|
TableLimits { max_word: 8, ..limits },
|
|
)
|
|
.expect("ok");
|
|
assert_eq!(t.len(), 1);
|
|
assert_eq!(t.lemma("Went").as_deref(), Some("go"));
|
|
}
|