From ab8afdcc89a0d13824be4dd641a5472f474e1e94 Mon Sep 17 00:00:00 2001 From: YinMo19 Date: Thu, 13 Feb 2025 01:59:37 +0800 Subject: [PATCH] add doc --- src/main.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main.rs b/src/main.rs index 86ded36..2218779 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { read_to_string(file) .expect(format!("No {} found", file).as_str()) @@ -12,6 +14,15 @@ fn parse_line(file: &str) -> Vec { .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> { let words_line = parse_line(file); let mut words = Vec::new(); @@ -28,6 +39,9 @@ fn parse_words(file: &str) -> Vec> { 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) -> &'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) -> &'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>, dict: &Vec) -> Vec> { let mut word_correrified = Vec::new(); for word_line in words.iter() { @@ -71,6 +100,9 @@ fn select_word_correrify(words: &Vec>, dict: &Vec) -> Vec>) { let mut file = File::create("correrified_words.txt").expect("Unable to create file"); for word_line in words.iter() {