fix regex

This commit is contained in:
Yoxem 2026-11-09 00:05:54 +08:00
parent 4498209094
commit 3de08368f3
2 changed files with 59 additions and 14 deletions

View file

@ -1,3 +1,4 @@
use std::os::raw;
use std::path::{PathBuf};
use std::str;
use regex::{Regex, RegexSet};
@ -13,6 +14,20 @@ use app_data::AppData;
#[cfg(not(target_arch = "wasm32"))]
use curl::easy::Easy;
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
}
fn get_custom_data_folder() -> PathBuf{
let custom_data_folder: PathBuf;
@ -121,8 +136,9 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{
result_decomposed_chars.append(&mut ch_decomposed);
}
let decomposed_latin_word = result_decomposed_chars.into_iter().collect::<String>();
let decomposed_latin_word = result_decomposed_chars.clone().into_iter().collect::<String>();
println!("result string: {}", decomposed_latin_word);
println!("result_decomposed_chars: {:?}", result_decomposed_chars);
// raw chacking if it matches the pattern
// U+0300 = ◌̀ 3rd
@ -132,26 +148,47 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{
// 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();
// 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 raw_matched = raw_pattern.is_match(latin_word);
if !raw_matched{
let regex_set = RegexSet::new([raw_pattern_general.as_str(), raw_pattern_m, raw_pattern_ng]).expect("illegal regex pattern");
let raw_matched : Vec<_> = regex_set.matches(&decomposed_latin_word).into_iter().collect();
let syllable_attribute : SyllableCategory;
// assert_eq!(raw_matched.is_empty(),true);
if raw_matched.is_empty(){
println!("Invalid Tâi-lô syllable==>1");
return false;
}
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;
}
}
}
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 valid_diacritics_pattern = Regex::new(r"\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}").expect("Invalid regex");
let chars_without_diactritics = Vec::new();
let mut chars_without_diactritics = Vec::new();
for ch in result_decomposed_chars {
if valid_diacritics_pattern.is_match(ch){
if valid_diacritics_pattern.is_match(&(ch.to_string())){
if has_one_diacritics_mark == false{
has_one_diacritics_mark = true;
diacritics_mark = ch;
@ -164,7 +201,11 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{
}
}
println!("")
println!("chars_without_diactritics: {:?}", chars_without_diactritics);
println!("diacritics_mark: {}", diacritics_mark);
println!("syllable_attribute: {:?}", syllable_attribute);
println!("=======");

View file

@ -3,11 +3,15 @@ fn main(){
// Write the contents of rust-lang.org to stdout
let a = ["oá", "guá", "a̍h", "tshiànn", "hiannh", "ah","hí",
"hi", "ha̍k", "n̂g", "sng", "ng", "sngh",
"sn̍gh", "hm̂", "m̄", "m", "m̍h"];
for w in a {
is_diacriticized_tl_single_syllable(w);
}
// download_corpus_content("dict/kamjitian/kamjitian.hanji.dict.yaml", ".");
is_diacriticized_tl_single_syllable("góa");
}