use hyphenation_commons::dictionary::extended::*;
use hyphenation_commons::dictionary::*;
pub trait Score<'d> {
type Value;
fn score(&'d self, word : &str) -> Vec<Self::Value>;
fn denotes_opportunity(value : Self::Value) -> bool;
}
impl<'d> Score<'d> for Standard {
type Value = u8;
#[inline]
fn denotes_opportunity(v : Self::Value) -> bool { v % 2 != 0 }
fn score(&'d self, word : &str) -> Vec<u8> {
let match_str = [".", word, "."].concat();
let hyphenable_length = word.len();
let mut values : Vec<u8> = vec![0; hyphenable_length.saturating_sub(1)];
for i in 0 .. match_str.len() - 1 {
let substring = &match_str.as_bytes()[i ..];
for tally in self.prefix_tallies(substring) {
for &Locus { index, value } in tally {
let k = i + index as usize;
if k > 1 && k <= hyphenable_length && value > values[k - 2] {
values[k - 2] = value;
}
}
}
}
values
}
}
impl<'d> Score<'d> for Extended {
type Value = (u8, Option<&'d Subregion>);
#[inline]
fn denotes_opportunity((v, _) : Self::Value) -> bool { v % 2 != 0 }
fn score(&'d self, word : &str) -> Vec<Self::Value> {
let match_str = [".", word, "."].concat();
let hyphenable_length = word.len();
let mut values : Vec<u8> = vec![0; hyphenable_length.saturating_sub(1)];
let mut regions : Vec<Option<&Subregion>> = vec![None; values.len()];
for i in 0 .. match_str.len() - 1 {
let substring = &match_str.as_bytes()[i ..];
for tally in self.prefix_tallies(substring) {
for &(Locus { index, value }, ref r) in tally.subregion.iter() {
let k = i + index as usize;
if k > 1 && k <= hyphenable_length && value > values[k - 2] {
values[k - 2] = value;
regions[k - 2] = Some(r);
}
}
for &Locus { index, value } in tally.standard.iter() {
let k = i + index as usize;
if k > 1 && k <= hyphenable_length && value > values[k - 2] {
values[k - 2] = value;
regions[k - 2] = None;
}
}
}
}
values.into_iter().zip(regions).collect()
}
}