2026-11-09 00:05:54 +08:00
|
|
|
use std::os::raw;
|
2026-11-08 01:41:23 +08:00
|
|
|
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;
|
|
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
use crate::SyllableCategory::GeneralSyl;
|
|
|
|
|
|
|
|
|
|
/// the categories of the vaild syllables
|
|
|
|
|
/// the description of the categories is shown below:
|
|
|
|
|
/// - GeneralSyl : Non-syllablized nasal consonants, eg. à, i̍k, bah, hó, tshiànn
|
|
|
|
|
/// - MSyl : Syllablized "m", eg. m̄, mh, hm̂
|
|
|
|
|
/// - NGSyl : Syllablized "ng", eg. n̂g, ngh, nn̄g
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum SyllableCategory{
|
|
|
|
|
GeneralSyl,
|
|
|
|
|
MSyl,
|
|
|
|
|
NgSyl
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-08 01:41:23 +08:00
|
|
|
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<u8> = 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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
let decomposed_latin_word = result_decomposed_chars.clone().into_iter().collect::<String>();
|
2026-11-08 01:41:23 +08:00
|
|
|
println!("result string: {}", decomposed_latin_word);
|
2026-11-09 00:05:54 +08:00
|
|
|
println!("result_decomposed_chars: {:?}", result_decomposed_chars);
|
2026-11-08 01:41:23 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
// raw_pattern_m should be in front of raw_pattern_general
|
|
|
|
|
// to avoid the bug of "m̍h" miscategorized as `GeneralSyl`:
|
|
|
|
|
let raw_pattern_m = r"^(([ptk]|ts)h?|[blgsjmnh]|ng)?m[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?h?$";
|
|
|
|
|
let raw_pattern_ng = r"^(([ptk]|ts)h?|[blgsjmnh]|ng)?n[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?gh?$";
|
|
|
|
|
let raw_pattern_general = r"^(([ptk]|ts)h?|[blgsjmnh]|ng)?".to_owned() +
|
|
|
|
|
r"[aeiou]?[aeiou][\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?r?[aeiou]?" +
|
|
|
|
|
r"(n[ng]|[ptkhmn]|nnh)?$";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let regex_set = RegexSet::new([raw_pattern_general.as_str(), raw_pattern_m, raw_pattern_ng]).expect("illegal regex pattern");
|
2026-11-08 01:41:23 +08:00
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
let raw_matched : Vec<_> = regex_set.matches(&decomposed_latin_word).into_iter().collect();
|
2026-11-08 01:41:23 +08:00
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
let syllable_attribute : SyllableCategory;
|
|
|
|
|
// assert_eq!(raw_matched.is_empty(),true);
|
|
|
|
|
if raw_matched.is_empty(){
|
|
|
|
|
println!("Invalid Tâi-lô syllable==>1");
|
2026-11-08 01:41:23 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-11-09 00:05:54 +08:00
|
|
|
else{
|
|
|
|
|
match raw_matched[0] {
|
|
|
|
|
0 => syllable_attribute = SyllableCategory::GeneralSyl,
|
|
|
|
|
1 => syllable_attribute = SyllableCategory::MSyl,
|
|
|
|
|
2 => syllable_attribute = SyllableCategory::NgSyl,
|
|
|
|
|
_ => {
|
|
|
|
|
println!("The pattern should't be matched.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-08 01:41:23 +08:00
|
|
|
|
|
|
|
|
let mut has_one_diacritics_mark = false;
|
|
|
|
|
let mut diacritics_mark = ' ';
|
2026-11-09 00:05:54 +08:00
|
|
|
let valid_diacritics_pattern = Regex::new(r"\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}").expect("Invalid regex");
|
2026-11-08 01:41:23 +08:00
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
let mut chars_without_diactritics = Vec::new();
|
2026-11-08 01:41:23 +08:00
|
|
|
|
|
|
|
|
for ch in result_decomposed_chars {
|
2026-11-09 00:05:54 +08:00
|
|
|
if valid_diacritics_pattern.is_match(&(ch.to_string())){
|
2026-11-08 01:41:23 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 00:05:54 +08:00
|
|
|
println!("chars_without_diactritics: {:?}", chars_without_diactritics);
|
|
|
|
|
println!("diacritics_mark: {}", diacritics_mark);
|
|
|
|
|
println!("syllable_attribute: {:?}", syllable_attribute);
|
|
|
|
|
println!("=======");
|
|
|
|
|
|
2026-11-08 01:41:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//let pattern = Regex::new(r"(([ptk]|ts)h?|[blgjmnh]|ng)?aeiou(nn|[ptkh]|nnh)")
|
|
|
|
|
// .expect("Invalid regex");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|