use std::path::{PathBuf}; use std::str; use regex::{Regex, RegexSet}; use std::fs; use unicode_normalization::char::decompose_canonical; #[cfg(target_arch = "wasm32")] // not #[wasm_bindgen] use wasm_bindgen::prelude::*; #[cfg(not(target_arch = "wasm32"))] use app_data::AppData; #[cfg(not(target_arch = "wasm32"))] use curl::easy::Easy; fn get_custom_data_folder() -> PathBuf{ let custom_data_folder: PathBuf; #[cfg(target_arch = "wasm32")] { custom_data_folder = std::env::temp_dir(); // for wasm browser only } #[cfg(not(target_arch = "wasm32"))]{ custom_data_folder = AppData::new("buhong") .ensure_data_dir() .expect("The custom data directory can not be created."); } return custom_data_folder; } /// download corpus content from the default corpus repo /// i.e. https://repo.kianting.info/yoxem/buhong-corpus /// if it fails, download it from /// https://repo.kianting.info/yoxem/buhong-corpus/raw/branch/main/dict/kamjitian/kamjitian.hanji.dict.yaml /// - path: eg. `dict/kamjitian/kamjitian.hanji.dict.yaml` for /// `https://repo.kianting.info/yoxem/buhong-corpus/raw/branch/main/dict/kamjitian/kamjitian.hanji.dict.yaml` /// - dest_dir: the folder inside the `custom_data_folder`. It should be relative path. #[cfg(not(target_arch = "wasm32"))] pub fn download_corpus_content(download_path: &str, dest_dir: &str){ let main_repo_url = "https://repo.kianting.info/yoxem/buhong-corpus/raw/branch/main/"; let backup_repo_url = "https://github.com/Yoxem/buhong-corpus/raw/refs/heads/main/"; let path_seperator = Regex::new(r"[//]").expect("Invalid regex"); let path_splitted: Vec<&str> = path_seperator.split(download_path).into_iter().collect(); let file_name = path_splitted.last().unwrap(); let dest_folder = get_custom_data_folder().join(dest_dir); let mut total_url = format!("{main_repo_url}{download_path}"); let mut file_content : Vec = Vec::new(); let mut handle = Easy::new(); handle.url(&total_url).unwrap(); { let mut transfer = handle.transfer(); transfer.write_function(|data| { file_content.extend_from_slice(data); Ok(data.len()) }).unwrap(); transfer.perform().unwrap(); } let mut res_code= handle.response_code() .expect("The response code should be returned."); if res_code < 200{ let file_string = String::from_utf8(file_content).expect("Our bytes should be valid utf8"); fs::write(dest_folder.join(PathBuf::from(file_name)), file_string) .expect("The downloaded file can't be written!"); }else{ total_url = format!("{backup_repo_url}{download_path}"); handle.url(&total_url).unwrap(); { let mut transfer = handle.transfer(); transfer.write_function(|data| { file_content.extend_from_slice(data); Ok(data.len()) }).unwrap(); transfer.perform().unwrap(); } res_code= handle.response_code() .expect("The response code should be returned."); if res_code < 400{ let file_string = String::from_utf8(file_content).expect("Our bytes should be valid utf8"); fs::write(dest_folder.join(PathBuf::from(file_name)), file_string) .expect("The downloaded file can't be written!"); }else{ panic!("The file you want to download is not from the main repo or from the backup repo!"); } } } pub fn add(left: u64, right: u64) -> u64 { left + right } #[wasm_bindgen] #[cfg(target_arch = "wasm32")] pub fn add_for_browser(left: u64, right: u64) -> u64 { add(left, right) } /// check if the single syllable latin (without hyphens) letter is /// a diacricitized Tâi-lô romanization of Ministry of Education syllable: /// `input`: latin word (without hyphens and other puncutation, but diacritics are allowed.) pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ // decomposed the diacritics let mut result_decomposed_chars = vec![]; for ch in latin_word.chars(){ let mut ch_decomposed = Vec::new(); decompose_canonical(ch, |c| { ch_decomposed.push(c); }); result_decomposed_chars.append(&mut ch_decomposed); } let decomposed_latin_word = result_decomposed_chars.into_iter().collect::(); println!("result string: {}", decomposed_latin_word); // raw chacking if it matches the pattern // U+0300 = ◌̀ 3rd // U+0301 = ◌́ 3nd // U+0302 = ◌̂ 5th // U+0304 = ◌̄ 7th // U+030B = ◌̋ 9th // U+030C = ◌̌ 6th // U+030D = ◌̍ 8th let raw_pattern_general = Regex::new(r"(([ptk]|ts)h?|[blgjmnh]|ng)?[aeiour\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]{1,4}(n[ng]|[ptkhmn]|nnh").expect("illegal regex pattern");; let raw_pattern_m = Regex::new(r"m[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?h?").expect("illegal regex pattern");; let raw_pattern_ng = Regex::new(r"n[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?gh?)").expect("illegal regex pattern");; let set = RegexSet::new([raw_pattern_general, raw_pattern_m, raw_pattern_ng]).unwrap(); let raw_matched = raw_pattern.is_match(latin_word); if !raw_matched{ return false; } let mut has_one_diacritics_mark = false; let mut diacritics_mark = ' '; let valid_diacritics_pattern = Regex::new(r"\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}"); let chars_without_diactritics = Vec::new(); for ch in result_decomposed_chars { if valid_diacritics_pattern.is_match(ch){ if has_one_diacritics_mark == false{ has_one_diacritics_mark = true; diacritics_mark = ch; }else{ // the diacritics mark occured twice or more is invalid return false; } }else{ chars_without_diactritics.push(ch); } } println!("") //let pattern = Regex::new(r"(([ptk]|ts)h?|[blgjmnh]|ng)?aeiou(nn|[ptkh]|nnh)") // .expect("Invalid regex"); return true; }