[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:
@@ -0,0 +1,128 @@
|
||||
//! Mandatory test 3: on a fixture pair of editions with a hand-made gold alignment, the baseline aligner reaches a
|
||||
//! stated F1; removed or merged paragraphs do not shift every later sentence.
|
||||
//!
|
||||
//! The fixture (`fixtures/align_de.txt`, `align_en.txt`) is a short story written for this repository in German and
|
||||
//! English and dedicated to the public domain (CC0). The English edition merges paragraphs 4 and 5, leaves out
|
||||
//! paragraph 8 and splits the last paragraph's second sentence in two — the three things real editions do.
|
||||
|
||||
use liber_lang::align::{Bead, align, score, sentence_pairs};
|
||||
|
||||
fn paragraphs(text: &str) -> Vec<&str> {
|
||||
text.split("\n\n").map(str::trim).filter(|p| !p.is_empty()).collect()
|
||||
}
|
||||
|
||||
fn bead(a: &[usize], b: &[usize]) -> Bead {
|
||||
Bead { a: a.to_vec(), b: b.to_vec() }
|
||||
}
|
||||
|
||||
/// Stated F1 for paragraphs (docs/03_languages.md).
|
||||
const PARAGRAPH_F1: f64 = 0.9;
|
||||
|
||||
#[test]
|
||||
fn paragraphs_align_to_the_gold() {
|
||||
let de = paragraphs(include_str!("fixtures/align_de.txt"));
|
||||
let en = paragraphs(include_str!("fixtures/align_en.txt"));
|
||||
assert_eq!((de.len(), en.len()), (11, 9));
|
||||
let gold = vec![
|
||||
bead(&[0], &[0]),
|
||||
bead(&[1], &[1]),
|
||||
bead(&[2], &[2]),
|
||||
bead(&[3], &[3]),
|
||||
bead(&[4, 5], &[4]),
|
||||
bead(&[6], &[5]),
|
||||
bead(&[7], &[6]),
|
||||
bead(&[8], &[]),
|
||||
bead(&[9], &[7]),
|
||||
bead(&[10], &[8]),
|
||||
];
|
||||
let found = align(&de, &en);
|
||||
let (p, r, f1) = score(&found, &gold);
|
||||
println!("paragraphs: P {p:.2} R {r:.2} F1 {f1:.2}\n{found:?}");
|
||||
assert!(f1 >= PARAGRAPH_F1, "F1 {f1:.2}");
|
||||
// Nothing after the merge and the omission is shifted: the last two paragraphs pair exactly.
|
||||
assert!(found.contains(&bead(&[9], &[7])), "{found:?}");
|
||||
assert!(found.contains(&bead(&[10], &[8])), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sentences_of_an_aligned_paragraph() {
|
||||
let de = paragraphs(include_str!("fixtures/align_de.txt"));
|
||||
let en = paragraphs(include_str!("fixtures/align_en.txt"));
|
||||
// The last paragraph: two German sentences, three English (the second split in two).
|
||||
let pairs = sentence_pairs(de[10], en[8]);
|
||||
let shape: Vec<(usize, usize)> = pairs.iter().map(|(a, b)| (a.len(), b.len())).collect();
|
||||
assert_eq!(shape, vec![(1, 1), (1, 2)], "{pairs:?}");
|
||||
// A one-to-one paragraph: three and three.
|
||||
let pairs = sentence_pairs(de[2], en[2]);
|
||||
assert_eq!(pairs.iter().map(|(a, b)| (a.len(), b.len())).collect::<Vec<_>>(), vec![(1, 1), (1, 1), (1, 1)]);
|
||||
// Ranges are UTF-16 offsets into the paragraph: the first German sentence is the reader's text.
|
||||
let (a, _) = &pairs[0];
|
||||
let units: Vec<u16> = de[2].encode_utf16().collect();
|
||||
let first = String::from_utf16_lossy(&units[a[0].0..a[0].1]);
|
||||
assert!(first.starts_with("Im Jahr 1847"), "{first}");
|
||||
}
|
||||
|
||||
/// A deterministic generator, so the test needs no `rand`.
|
||||
struct Lcg(u64);
|
||||
impl Lcg {
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 = self.0.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
|
||||
self.0 >> 33
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_book_length_alignment_with_omissions_stays_on_track() {
|
||||
// 1,500 paragraphs; the other edition is ~15% longer with noise, drops every 97th paragraph and merges every
|
||||
// 131st with the next. Every 10th paragraph carries a number on both sides (a date, a chapter): the anchors.
|
||||
let mut g = Lcg(7);
|
||||
let mut a = Vec::new();
|
||||
let mut b = Vec::new();
|
||||
let mut gold = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < 1500 {
|
||||
let words = 8 + (g.next() % 120) as usize;
|
||||
let mk = |n: usize, tag: Option<usize>| {
|
||||
let mut s = "lorem ".repeat(n);
|
||||
if let Some(t) = tag {
|
||||
s.push_str(&format!(" {t}"));
|
||||
}
|
||||
s
|
||||
};
|
||||
let tag = (i % 10 == 0).then_some(i);
|
||||
if i % 97 == 50 {
|
||||
a.push(mk(words, tag));
|
||||
gold.push(bead(&[i], &[]));
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if i % 131 == 60 && i + 1 < 1500 {
|
||||
let words2 = 8 + (g.next() % 120) as usize;
|
||||
a.push(mk(words, tag));
|
||||
a.push(mk(words2, None));
|
||||
let noisy = ((words + words2) as f64 * (1.1 + (g.next() % 10) as f64 / 100.0)) as usize;
|
||||
gold.push(bead(&[i, i + 1], &[b.len()]));
|
||||
b.push(mk(noisy, tag));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
a.push(mk(words, tag));
|
||||
let noisy = (words as f64 * (1.1 + (g.next() % 10) as f64 / 100.0)) as usize;
|
||||
gold.push(bead(&[i], &[b.len()]));
|
||||
b.push(mk(noisy.max(1), tag));
|
||||
i += 1;
|
||||
}
|
||||
let found = align(&a, &b);
|
||||
let (_, _, f1) = score(&found, &gold);
|
||||
println!("book: {} vs {} paragraphs, F1 {f1:.3}", a.len(), b.len());
|
||||
assert!(f1 >= 0.9, "F1 {f1:.3}");
|
||||
// The end is on track: the last bead is the gold's last bead.
|
||||
assert_eq!(found.last(), gold.last());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edge_cases() {
|
||||
assert!(align::<&str>(&[], &[]).is_empty());
|
||||
assert_eq!(align(&["a"], &[] as &[&str]), vec![bead(&[0], &[])]);
|
||||
assert_eq!(align(&["Hello there."], &["Hallo dort."]), vec![bead(&[0], &[0])]);
|
||||
}
|
||||
Reference in New Issue
Block a user