pub fn extract_without_order<I, T, P, S>(
query: &str,
choices: I,
processor: P,
scorer: S,
score_cutoff: u8,
) -> Vec<(String, u8)>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
P: Fn(&str, bool) -> String,
S: Fn(&str, &str, bool, bool) -> u8
{
let processed_query: String = processor(query, false);
if processed_query.is_empty() {
}
let mut results = vec![];
for choice in choices {
let processed: String = processor(choice.as_ref(), false);
let score: u8 = scorer(processed_query.as_str(), processed.as_str(), true, true);
if score >= score_cutoff {
results.push((choice.as_ref().to_string(), score))
}
}
results
}
pub fn extract_one<I, T, P, S>(
query: &str,
choices: I,
processor: P,
scorer: S,
score_cutoff: u8,
) -> Option<(String, u8)>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
P: Fn(&str, bool) -> String,
S: Fn(&str, &str, bool, bool) -> u8
{
let best = extract_without_order(query, choices, processor, scorer, score_cutoff);
if best.is_empty() {
return None;
}
best.iter()
.rev()
.cloned()
.max_by(|(_, acc_score), (_, score)| acc_score.cmp(score))
}
#[cfg(test)]
mod tests {
use super::*;
use fuzz;
use utils;
mod process {
use super::*;
fn get_baseball_strings() -> &'static [&'static str] {
&[
"new york mets vs chicago cubs",
"chicago cubs vs chicago white sox",
"philladelphia phillies vs atlanta braves",
"braves vs mets",
]
}
fn unwrap_extract_one_choice(query: &str) -> String {
extract_one(
query,
get_baseball_strings().iter(),
&utils::full_process,
&fuzz::wratio,
0,
)
.unwrap()
.0
}
#[test]
fn test_get_best_choice1() {
let query = "new york mets at atlanta braves";
let best = unwrap_extract_one_choice(query);
assert_eq!(best.as_str(), get_baseball_strings()[3])
}
#[test]
fn test_get_best_choice2() {
let query = "philadelphia phillies at atlanta braves";
let best = unwrap_extract_one_choice(query);
assert_eq!(best.as_str(), get_baseball_strings()[2])
}
#[test]
fn test_get_best_choice3() {
let query = "atlanta braves at philadelphia phillies";
let best = unwrap_extract_one_choice(query);
assert_eq!(best.as_str(), get_baseball_strings()[2])
}
#[test]
fn test_get_best_choice4() {
let query = "chicago cubs vs new york mets";
let best = unwrap_extract_one_choice(query);
assert_eq!(best.as_str(), get_baseball_strings()[0])
}
}
}