120 lines
5.2 KiB
Rust
120 lines
5.2 KiB
Rust
//! Mandatory test 1: on fixture texts with known lemma sets, the coverage figure matches a hand-computed value;
|
|
//! proper nouns and numbers are excluded; a known word in any inflected form counts as known.
|
|
|
|
use liber_lang::lemma::{Chain, Headwords, Rules, Table, TableLimits};
|
|
use liber_lang::profile::{Profile, lemma_at};
|
|
use liber_lang::{Lang, level};
|
|
use std::collections::BTreeSet;
|
|
|
|
fn known(words: &[&str]) -> impl Fn(&str) -> bool {
|
|
let set: BTreeSet<String> = words.iter().map(|w| (*w).to_owned()).collect();
|
|
move |l: &str| set.contains(l)
|
|
}
|
|
|
|
#[test]
|
|
fn coverage_matches_the_hand_count() {
|
|
// Counted (lemma): The(the) cat sat(sit) on the mat | The(the) cats(cat) were(be) happy and saw(see) cats(cat) in in
|
|
// chapter. Not counted: Tom (a name: capitalised, not sentence-initial), 3 and 1999 (numbers), XII (a Roman
|
|
// numeral). 16 counted words; known = the, cat, sit, on, mat, be → 6 in the first sentence + 4 in the second.
|
|
let text = "The cat sat on the mat. The cats were happy, and Tom saw 3 cats in 1999, in chapter XII.";
|
|
let p = Profile::build(&[text], &Rules::new(Lang::En));
|
|
assert_eq!(p.words(), 16);
|
|
let cov = p.coverage(&known(&["the", "cat", "sit", "on", "mat", "be"]));
|
|
assert!((cov - 10.0 / 16.0).abs() < 1e-12, "{cov}");
|
|
assert!(!p.total().contains_key("tom"));
|
|
assert!(!p.total().keys().any(|k| k.chars().any(|c| c.is_ascii_digit()) || *k == "xii"));
|
|
}
|
|
|
|
#[test]
|
|
fn any_inflected_form_of_a_known_lemma_is_known() {
|
|
let p = Profile::build(&["Cats. The cat and the cats; a cat's tail."], &Rules::new(Lang::En));
|
|
let cov = p.coverage(&known(&["cat", "the", "and", "a", "tail"]));
|
|
assert_eq!(cov, 1.0);
|
|
// Latin: amat, amabat, amauit all count as `amo`.
|
|
let p = Profile::build(&["Puella amat. Puella amabat. Puella amauit."], &Rules::new(Lang::La));
|
|
assert_eq!(p.total().get("amo"), Some(&3));
|
|
assert_eq!(p.coverage(&known(&["amo", "puella"])), 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn german_names_need_a_lexicon_to_be_told_from_nouns() {
|
|
// With headwords, a capitalised word they do not know (Hänsel) is a name; a known noun (Haus) counts.
|
|
let chain = Chain {
|
|
table: None,
|
|
headwords: Some(Headwords::new(Lang::De, ["Haus", "der", "sein", "und", "groß", "gehen"])),
|
|
rules: Rules::new(Lang::De),
|
|
};
|
|
let p = Profile::build(&["Das Haus ist groß, und Hänsel geht."], &chain);
|
|
let total = p.total();
|
|
assert!(total.contains_key("haus"));
|
|
assert!(!total.contains_key("hänsel"));
|
|
// Without a lexicon every capitalised German word is counted (a name cannot be told from a noun): stated in the docs.
|
|
let p = Profile::build(&["Das Haus ist groß, und Hänsel geht."], &Rules::new(Lang::De));
|
|
assert!(p.total().contains_key("hänsel"));
|
|
}
|
|
|
|
#[test]
|
|
fn later_counts_and_the_warm_up() {
|
|
let chapters =
|
|
["Der Hund bellt. Der Hund schläft.", "Ein Hund und eine Katze. Die Katze schläft.", "Hunde und Katzen."];
|
|
// A table for the nouns, so the test is about counting, not about the German rules.
|
|
let table = Table::from_tsv(
|
|
Lang::De,
|
|
"Hund Hund
|
|
Hunde Hund
|
|
Katze Katze
|
|
Katzen Katze
|
|
",
|
|
TableLimits::default(),
|
|
)
|
|
.expect("table");
|
|
let chain = Chain { table: Some(table), headwords: None, rules: Rules::new(Lang::De) };
|
|
let p = Profile::build(&chapters, &chain);
|
|
// "comes up N more times" after chapter 0: Hund once in ch.1, Hunde (→ hund) once in ch.2.
|
|
assert_eq!(p.later("hund", 0), 2);
|
|
assert_eq!(p.later("hund", 2), 0);
|
|
// Warm-up for chapter 1: the most frequent unknown lemmas, ties by lemma.
|
|
let w = p.warmup(1, &known(&["und", "ein", "der"]), 5);
|
|
assert_eq!(w.first().map(|x| x.0.as_str()), Some("katze"));
|
|
assert_eq!(w.first().map(|x| x.1), Some(2));
|
|
assert!(w.len() <= 5);
|
|
}
|
|
|
|
#[test]
|
|
fn a_tapped_word_gives_its_lemma() {
|
|
let text = "Elizabeth was walking slowly.";
|
|
let rules = Rules::new(Lang::En);
|
|
// UTF-16 offset 15 is inside "walking".
|
|
let (tok, lemma) = lemma_at(text, 15, &rules).expect("a word");
|
|
assert_eq!(tok.text, "walking");
|
|
assert_eq!(lemma, "walk");
|
|
assert!(lemma_at(text, 2, &rules).is_some(), "sentence-initial capital is a word");
|
|
assert!(lemma_at("He met Darcy.", 8, &rules).is_none(), "a name is not glossed as unknown");
|
|
}
|
|
|
|
#[test]
|
|
fn levels_and_unlocking() {
|
|
assert_eq!(level::level(0, &level::LEVELS), (0, Some(500)));
|
|
assert_eq!(level::level(1_999, &level::LEVELS), (2, Some(1)));
|
|
assert_eq!(level::level(9_000, &level::LEVELS), (6, None));
|
|
assert!(level::unlocked(0.95, false));
|
|
assert!(!level::unlocked(0.949, false));
|
|
assert!(level::unlocked(0.91, true));
|
|
assert!(!level::unlocked(f64::NAN, true));
|
|
assert_eq!(level::percent(0.949), 94);
|
|
assert_eq!(level::percent(1.2), 100);
|
|
}
|
|
|
|
#[test]
|
|
fn concept_ids_follow_the_ludus_rule() {
|
|
use liber_lang::concept::concept_id;
|
|
let id = concept_id(Lang::De, "Haus");
|
|
assert!(id.starts_with("w-de-"));
|
|
assert_eq!(id.len(), 5 + 16);
|
|
assert!(id.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-'));
|
|
// Folded first: every form of one lemma, one concept; Latin j/v folded.
|
|
assert_eq!(concept_id(Lang::De, "haus"), id);
|
|
assert_eq!(concept_id(Lang::La, "Iulius"), concept_id(Lang::La, "julius"));
|
|
assert_ne!(concept_id(Lang::En, "haus"), id);
|
|
}
|