Skip to main content

optimize

Function optimize 

Source
pub fn optimize<'a>(
    ground: &str,
    blocks: &[&'a str],
) -> (MatchingLoss, Vec<(usize, &'a str)>)
Expand description

Core function: Optimize the matching order of a list of strings against ground truth.

§Arguments

  • ground - The ground truth string to match against
  • blocks - Slice of strings to be matched/ordered

§Returns

Tuple of (MatchingLoss, Vec of (index, text) tuples representing optimal order)

Examples found in repository?
examples/run_ab.rs (line 17)
5fn main() {
6    // Load the ground truth text (a.txt) and the OCR blocks (b.txt)
7    let ground = fs::read_to_string("a.txt").expect("Failed to read a.txt");
8    let blocks_raw = fs::read_to_string("b.txt").expect("Failed to read b.txt");
9
10    // Each line in b.txt is one block
11    let blocks: Vec<&str> = blocks_raw.lines().collect();
12
13    println!("Ground truth length: {} chars", ground.len());
14    println!("Number of blocks:    {}", blocks.len());
15    println!();
16
17    let (loss, optimized) = optimize(&ground, &blocks);
18
19    println!();
20    println!("=== Result ===");
21    println!(
22        "Loss: fuzzy={:.2}, jump={:.2}, geo={:.2}, total={:.2}",
23        loss.fuzzy,
24        loss.jump_distance,
25        loss.geo_distance,
26        loss.abs()
27    );
28    println!();
29
30    // Print the optimized text
31    let result_text: String = optimized
32        .iter()
33        .map(|(_, text)| *text)
34        .collect::<Vec<&str>>()
35        .join(" ");
36
37    println!("Optimized text (first 500 chars):");
38    println!("{}", &result_text[..result_text.len().min(500)]);
39}