This commit is contained in:
YinMo19 2025-02-13 01:59:37 +08:00
parent 765f1f3d77
commit ab8afdcc89

View File

@ -4,6 +4,8 @@ use levenshtein::levenshtein;
use std::fs::{File, read_to_string};
use std::io::Write;
/// open a specified file and return a vector of strings
/// where each element is a line.
fn parse_line(file: &str) -> Vec<String> {
read_to_string(file)
.expect(format!("No {} found", file).as_str())
@ -12,6 +14,15 @@ fn parse_line(file: &str) -> Vec<String> {
.collect()
}
/// Lines of a words.txt are like
/// ```plaintext
/// 1234 hello I/am/a/test/you/can
/// 1231 correrify my/posibily/orrer
/// ```
/// We want to parse a line into a vector
/// which elements represents each words,
/// include first number.
fn parse_words(file: &str) -> Vec<Vec<String>> {
let words_line = parse_line(file);
let mut words = Vec::new();
@ -28,6 +39,9 @@ fn parse_words(file: &str) -> Vec<Vec<String>> {
words
}
/// Binary-search first. If the word is NOT in the dictionary,
/// we will find the word with the minimum distance.
fn correrify<'a>(word: &'a str, dict: &'a Vec<String>) -> &'a str {
if let Ok(_) = dict.binary_search(&word.to_string()) {
return word;
@ -48,6 +62,21 @@ fn correrify<'a>(word: &'a str, dict: &'a Vec<String>) -> &'a str {
temp_min.1
}
/// The words's shape is just like
/// ```
/// [
/// ["1324", "word1", "word2", "word3"],
/// ["1325", "word1", "word2", "word3"],
/// ]
/// ```
/// , and We can assert
/// ```
/// assert!(word_line[0].len() == 4);
/// assert!(word_line[0].chars().all(|c| c.is_numeric()));
/// ```
/// We just skip the first word(4 digits number)
/// and correrify the rest of words.
fn select_word_correrify(words: &Vec<Vec<String>>, dict: &Vec<String>) -> Vec<Vec<String>> {
let mut word_correrified = Vec::new();
for word_line in words.iter() {
@ -71,6 +100,9 @@ fn select_word_correrify(words: &Vec<Vec<String>>, dict: &Vec<String>) -> Vec<Ve
word_correrified
}
/// this function just write to the correrified_words.txt
/// with same format as words.txt
fn write_correrified_words(words: &Vec<Vec<String>>) {
let mut file = File::create("correrified_words.txt").expect("Unable to create file");
for word_line in words.iter() {