pub fn histogram_glyph_matcher(
    histogram: &[u8],
    set: &glyphs::GlyphSet,
    min_width: usize,
) -> Vec<(glyphs::Glyph, u8)> {
    let v = histogram;
    let mut i: usize = 0;
    let mut res: Vec<(glyphs::Glyph, u8)> = Vec::new();

    while i < v.len() - 1 {
        if v[i] == 0 {
            i += 1;
            continue;
        }
        // v[i] is now the first non-zero entry.
        let remainder = &v[i..];

        type ScoreType = u8;
        let mut scores: Vec<ScoreType> = vec![];
        scores.resize(set.entries.len(), 0 as ScoreType);
        for (glyph, score) in set.entries.iter().zip(scores.iter_mut()) {
            // In this function, all glyphs must have an lstrip histogram as all whitespace is
            // stirpped.
            let hist = glyph
                .lstrip_hist()
                .expect("all glyphs must have lstrip histogram");
            *score = calc_score_min(hist, &remainder, min_width);
        }
        // println!("{scores:?}");

        // The lowest score is the best match, (the least difference)...
        // https://stackoverflow.com/a/53908709
        let index_of_min: Option<usize> = scores
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(index, _)| index);

        if let Some(best) = index_of_min {
            let token = &set.entries[best];
            let score = scores[best];
            res.push((token.clone(), score));

            i += token
                .lstrip_hist()
                .expect("all glyphs must have lstrip histogram")
                .len();
        } else {
            // println!("Huh? didn't have a lowest score??");
            i += 1;
        }
    }
    res
}
/// Simple histogram matcher that removes any zero bins and just matches lowest scoring glyphs.
Simple histogram matcher that removes any zero bins and just matches lowest scoring glyphs.
Simple histogram matcher that removes any zero bins and just matches lowest scoring glyphs .
pub fn histogram_glyph_matcher(
    histogram: &[u8],
    set: &glyphs::GlyphSet,
    min_width: usize,
) -> Vec<(glyphs::Glyph, u8)> {
    let v = histogram;
    let mut i: usize = 0;
    let mut res: Vec<(glyphs::Glyph, u8)> = Vec::new();
    while i < v.len() - 1 {
        if v[i] == 0 {
            i += 1;
            continue;
        }
        // v[i] is now the first non-zero entry.
        let remainder = &v[i..];
        type ScoreType = u8;
        let mut scores: Vec<ScoreType> = vec![];
        scores.resize(set.entries.len(), 0 as ScoreType);
        for (glyph, score) in set.entries.iter().zip(scores.iter_mut()) {
            // In this function, all glyphs must have an lstrip histogram as all whitespace is
            // stirpped.
            let hist = glyph
                .lstrip_hist()
                .expect("all glyphs must have lstrip histogram");
            *score = calc_score_min(hist, &remainder, min_width);
        }
        // println!("{scores:?}");
        // The lowest score is the best match, (the least difference)...
        // https://stackoverflow.com/a/53908709
        let index_of_min: Option<usize> = scores
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(index, _)| index);
        if let Some(best) = index_of_min {
            let token = &set.entries[best];
            let score = scores[best];
            res.push((token.clone(), score));
            i += token
                .lstrip_hist()
                .expect("all glyphs must have lstrip histogram")
                .len();
        } else {
            // println!("Huh? didn't have a lowest score??");
            i += 1;
        }
    }
    res
}
pub fn histogram_glyph_matcher ( histogram : & [ u8 ] , set : & glyphs :: GlyphSet , min_width : usize , ) -> Vec < ( glyphs :: Glyph , u8 ) > { let v = histogram ; let mut i : usize = 0 ; let mut res : Vec < ( glyphs :: Glyph , u8 ) > = Vec :: new ( ) ; while i < v . len ( ) - 1 { if v [ i ] == 0 { i += 1 ; continue ; } // v[i] is now the first non-zero entry. let remainder = & v [ i .. ] ; type ScoreType = u8 ; let mut scores : Vec < ScoreType > = vec ! [ ] ; scores . resize ( set . entries . len ( ) , 0 as ScoreType ) ; for ( glyph , score ) in set . entries . iter ( ) . zip ( scores . iter_mut ( ) ) { // In this function, all glyphs must have an lstrip histogram as all whitespace is // stirpped. let hist = glyph . lstrip_hist ( ) . expect ( " " ) ; * score = calc_score_min ( hist , & remainder , min_width ) ; } // println!("{scores:?}"); // The lowest score is the best match, (the least difference)... // https://stackoverflow.com/a/53908709 let index_of_min : Option < usize > = scores . iter ( ) . enumerate ( ) . min_by ( | ( _ , a ) , ( _ , b ) | a . partial_cmp ( b ) . unwrap_or ( std :: cmp :: Ordering :: Equal ) ) . map ( | ( index , _ ) | index ) ; if let Some ( best ) = index_of_min { let token = & set . entries [ best ] ; let score = scores [ best ] ; res . push ( ( token . clone ( ) , score ) ) ; i += token . lstrip_hist ( ) . expect ( " " ) . len ( ) ; } else { // println!("Huh? didn't have a lowest score??"); i += 1 ; } } res }
Simple histogram matcher that removes any zero bins and just matches lowest scoring glyphs.
Simple histogram matcher that removes any zero bins and just matches lowest scoring glyphs .
histogram
&[u8]
set
&glyphs::GlyphSet
min_width
usize