test: Prevent correcting corrections

This commit is contained in:
Ed Page 2021-07-27 13:15:12 -05:00
parent fc4ec0e4a1
commit 41048d15b3

View file

@ -4,31 +4,60 @@ use std::collections::HashSet;
use structopt::StructOpt; use structopt::StructOpt;
fn generate<W: std::io::Write>(file: &mut W, dict: &[u8]) { fn generate<W: std::io::Write>(file: &mut W, dict: &[u8]) {
let mut wtr = csv::WriterBuilder::new().flexible(true).from_writer(file); let rows: Vec<Vec<_>> = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(dict)
.records()
.map(Result::unwrap)
.map(|r| {
let row: Vec<String> = r.iter().map(ToOwned::to_owned).collect();
row
})
.collect();
let disallowed_typos = varcon_words(); let disallowed_typos = varcon_words();
let word_variants = proper_word_variants(); let word_variants = proper_word_variants();
let rows: Vec<_> = rows
let mut reader = csv::ReaderBuilder::new() .into_iter()
.has_headers(false) .filter(|r| {
.flexible(true) let typo = &r[0];
.from_reader(dict); let is_disallowed = disallowed_typos.contains(&unicase::UniCase::new(typo));
for record in reader.records() { if is_disallowed {
let record = record.unwrap(); eprintln!("{:?} is disallowed", typo);
let mut record_fields = record.iter();
let typo = record_fields.next().unwrap();
if disallowed_typos.contains(&unicase::UniCase::new(typo)) {
continue;
} }
!is_disallowed
let mut row = vec![typo]; })
for correction in record_fields { .map(|r| {
let mut fields = r.into_iter();
let typo = fields.next().expect("at least a typo");
let mut row = vec![typo.clone()];
for correction in fields {
let correction = word_variants let correction = word_variants
.get(correction) .get(correction.as_str())
.and_then(|words| find_best_match(typo, correction, words)) .and_then(|words| find_best_match(&typo, correction.as_str(), words))
.unwrap_or(correction); .unwrap_or(&correction);
row.push(correction); row.push(correction.to_owned());
} }
row
})
.collect();
let corrections: std::collections::HashSet<_> = rows
.iter()
.flat_map(|r| {
let mut i = r.iter();
i.next();
i.map(ToOwned::to_owned)
})
.collect();
let rows: Vec<_> = rows
.into_iter()
.filter(|r| !corrections.contains(&r[0]))
.collect();
let mut wtr = csv::WriterBuilder::new().flexible(true).from_writer(file);
for row in rows {
wtr.write_record(&row).unwrap(); wtr.write_record(&row).unwrap();
} }
wtr.flush().unwrap(); wtr.flush().unwrap();