diff --git a/README.md b/README.md index f4faf6d..e9bf214 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,61 @@ a Taigi processing tool ## Other documents - For the implement details of the tool, see `./docs`. + +## 了後ê做法 + - 將台文語料轉作數字台羅需要函數 + - 小寫化臺語文、數字加escape字元(1 => \1),koh "\" => "\\" + - 切臺語ê句變做音節 + - 若準講判定是臺語音節,轉做數字音節 + - 就變成按呢 +``` +我共你講 gua2 ka7 li2 kong2 +代誌 tai7-tsi3 + + +``` + + - 了後處理做: + +``` +s_arrayized_series = [] +for s in 句ê集: + s_arrayized = arrayize(s); + s_arrayized = ["HEAD"].append(s_arrayized) + s_arrayized_series.push(s_arrayized) + +``` +tshiau揣:,代先按呢切: + +``` +阮老師欲來。 == n字 +"gun2" " " "lau7" "-" "su1" " " "beh4" " " "lai5" "." 2n-1字 +``` + +先檢查「羅字數量 == 漢字數量*2 - 1」,無就修正語料 + +``` +for j in range(1,len(sentencePair[i].漢)): + 漢 = sentencePair[i].漢[j] + 頂字 = sentencePair[i].漢[j-1] + 羅字 = sentencePair[i].羅[2*j-1] + 羅字頂符 = sentencePair[i].羅[2*j-2] + if 羅字頂符 in ["-", "--"]": // 空白不算 + 頭前敢欲連字 = true + else: + false + HASHMAPHEAD = 漢 + 羅 + 頂字 + 頭前敢欲連字.as_str() + if not HASHMAPHEAD in freq_dict.keys() + freq_dict(HASHMAPHEAD) = [漢,羅,頂字,頭前敢欲連字,1] + else + freq_dict(HASHMAPHEAD) = [漢,羅,頂字,頭前敢欲連字,freq_dict["HASHMAPHEAD"]["freq"]+1] +``` + +``` +漢,羅,頂字,頭前敢欲連字,出現改數 +扁,pinn2,水,false,2 +``` + + + +``` diff --git a/src/lib.rs b/src/lib.rs index e7fc34d..b4fe6c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ use std::str; use regex::{Regex, RegexSet}; use std::fs; use unicode_normalization::char::decompose_canonical; +use std::collections::HashMap; + #[cfg(target_arch = "wasm32")] // not #[wasm_bindgen] use wasm_bindgen::prelude::*; @@ -14,14 +16,12 @@ 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)] +#[derive(Debug, PartialEq)] enum SyllableCategory{ GeneralSyl, MSyl, @@ -123,10 +123,25 @@ pub fn add_for_browser(left: u64, right: u64) -> u64 { add(left, right) } -/// check if the single syllable latin (without hyphens) letter is +/// check if the smallcaps and 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.) +/// `input`: small-cased latin word (without hyphens and other puncutation, but diacritics are allowed.) +/// `return value`: boolean value of whether the input syllable is a valid Tailo pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ + match is_diacriticized_tl_single_syllable_aux(latin_word){ + None => false, + Some(a) => true, + } +} + + +/// auxlliary function check if the single syllable latin (without hyphens) letter is +/// a diacricitized Tâi-lô romanization of Ministry of Education syllable: +/// `input`: small-cased latin word (without hyphens and other puncutation, but diacritics are allowed.) +/// `return value`: +/// - `None` if it's not a valid TL syllable. +/// - `Some(string)`, where `string` is the number-tone TL syllable +pub fn is_diacriticized_tl_single_syllable_aux(latin_word: &str) -> Option{ // decomposed the diacritics let mut result_decomposed_chars = vec![]; @@ -142,7 +157,7 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ // raw chacking if it matches the pattern // U+0300 = ◌̀ 3rd - // U+0301 = ◌́ 3nd + // U+0301 = ◌́ 2nd // U+0302 = ◌̂ 5th // U+0304 = ◌̄ 7th // U+030B = ◌̋ 9th @@ -151,10 +166,16 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ // 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_m = r"^(([ptk]|ts)h?|[blgsjnh]|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])?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"("+ + r"[aeiou]?[aeiou][\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?[aeiou]?" + // general + r"|"+ + r"[ei][\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?r" + // er, ir + r"|"+ + r"ere[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?" + //ere + r")" + r"(n[ng]|[ptkhmn]|nnh)?$"; @@ -167,7 +188,7 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ // assert_eq!(raw_matched.is_empty(),true); if raw_matched.is_empty(){ println!("Invalid Tâi-lô syllable==>1"); - return false; + return None; } else{ match raw_matched[0] { @@ -176,43 +197,175 @@ pub fn is_diacriticized_tl_single_syllable(latin_word: &str) -> bool{ 2 => syllable_attribute = SyllableCategory::NgSyl, _ => { println!("The pattern should't be matched."); - return false; + return None; } } } 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}").expect("Invalid regex"); + let mut diacritic_mark = ' '; + 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 mut chars_without_diactritics = Vec::new(); + let mut chars_without_diacritics = Vec::new(); for ch in result_decomposed_chars { if valid_diacritics_pattern.is_match(&(ch.to_string())){ if has_one_diacritics_mark == false{ has_one_diacritics_mark = true; - diacritics_mark = ch; + diacritic_mark = ch; }else{ // the diacritics mark occured twice or more is invalid - return false; + return None; } }else{ - chars_without_diactritics.push(ch); + chars_without_diacritics.push(ch); } } - println!("chars_without_diactritics: {:?}", chars_without_diactritics); - println!("diacritics_mark: {}", diacritics_mark); + println!("chars_without_diactritics: {:?}", chars_without_diacritics); + println!("diacritic_mark: {}", diacritic_mark); println!("syllable_attribute: {:?}", syllable_attribute); println!("======="); + // raw chacking if it matches the pattern + // U+0300 = ◌̀ 3rd + // U+0301 = ◌́ 2nd + // U+0302 = ◌̂ 5th + // U+0304 = ◌̄ 7th + // U+030B = ◌̋ 9th + // U+030C = ◌̌ 6th + // U+030D = ◌̍ 8th + // Hashmap for mapping from diacritic to tone number (2~3, 5~9) + let diacritic_to_tone_number = HashMap::from([ + ('\u{0301}', 2), + ('\u{0300}', 3), + ('\u{0302}', 5), + ('\u{030C}', 6), + ('\u{0304}', 7), + ('\u{030D}', 8), + ('\u{030B}', 9), + ]); - //let pattern = Regex::new(r"(([ptk]|ts)h?|[blgjmnh]|ng)?aeiou(nn|[ptkh]|nnh)") - // .expect("Invalid regex"); + // range: 1~3, 5~9. later will be 1~9. + // IMPORTANT: 1 is means both the "4th tone" and the 1st tone now! + // The correct 4th tone will be set later. + let mut tone_number: u32 = 1; + if diacritic_mark != ' ' { + tone_number = diacritic_to_tone_number[&diacritic_mark]; + } - return true; + // 1st tone (tone_number == 1) is always without ending -p, -t, -k or -h + // 4th tone (tone_number == 1) is always with ending -p, -t ,-k, or -h, + // other tone nubmer (except tone_number == 8) is not with -p, -t, -k, or -h. + // 8th tone (tone_number == 8) is always with ending with -p, -t, -k or -h + // i.e. + // 1. if the ending is -p,-t,-k, or -h + // but the tone number is not 1 or 8, return false. + // 2. if the ending is not -p,-t,-k, or -h + // but the tone number is 8, return false. + let have_entering_tone_coda = ['p', 't', 'k', 'h'] + .contains(chars_without_diacritics.last().unwrap()); + // Condition 1, eg. sák + if have_entering_tone_coda && !([1, 8].contains(&tone_number)){ + println!("COND 1, latin_word={}", latin_word); + return None; + } + // Condition 2, eg. sa̍ + if !have_entering_tone_coda && tone_number == 8{ + println!("COND 2, latin_word={}", latin_word); + return None; + } + // set the tone number for the yin-entering (4th) tone to the correct one + if have_entering_tone_coda && tone_number == 1{ + tone_number = 4; + } + + // the syllable_m and syllable_ng is checked OK, return true + if syllable_attribute == SyllableCategory::MSyl || syllable_attribute == SyllableCategory::NgSyl{ + let syllable_without_diacritics : String = chars_without_diacritics.iter().collect(); + let syllable_with_tone_number : String = format!("{}{}", syllable_without_diacritics, tone_number); + println!("latin_word={} ({:?}) returns TRUE\n\n", latin_word, syllable_with_tone_number); + return Some(syllable_with_tone_number); + } + + // for other general syllable, check the validity of vowel + // get the vowel + println!("decomposed_latin_word={}", decomposed_latin_word); + let general_pattern = Regex::new(r"^(?([ptk]|ts)h?|[blgsjmnh]|ng)?(?[^mngptkh]+)(?[mngptkh]+)?$") + .expect("Invalid regex."); + let caps = general_pattern.captures(&decomposed_latin_word).unwrap(); + let onset = caps.name("onset").map_or("", |m|m.as_str()); + let vowel = caps.name("vowel").map_or("", |m|m.as_str()); + let coda = caps.name("coda").map_or("", |m|m.as_str()); + + + let vowel_pattern_set = RegexSet::new([ + // Let's categorize the rimes. T for diacritics tone mark, but it's optional + // eT? + r"^e[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?$", + // iT? + r"^i[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?$", + // oT?o? + r"^o[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?o?$", + // u + r"^u[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?$", + // i?aT?u? => ia, a, au, iau + r"^i?a[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?u?$", + // u?aT?i? => ua, a, ai, uai + r"^u?a[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?i?$", + // iT?o + r"^i[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?o$", + // iT?u + r"^i[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?u$", + // uT?e + r"^u[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?e$", + // uT?i + r"^u[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?i$", + // [ie]T?r + r"^[ie][\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?r$", + // ereT? + r"^ere[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?$"]) + .expect("The regex set contains invalid one"); + + let vowel_matched = vowel_pattern_set.is_match(&vowel); + if !vowel_matched{ + println!("Illegal vowel pattern, latin_word={}", latin_word); + return None; + } + + // filter of impossible phomene combination: + // bVnn(h), lVnn(h), and gVnn(h), bVm, (m|n|ng)V(n|m|ng) , -e[nt] is not allowed. + let nnh_pattern = Regex::new(r"^nnh?$").expect("Invalid regex"); + let e_pattern = Regex::new("^e[\u{300}\u{301}\u{302}\u{304}\u{30B}\u{30C}\u{30D}]?$") + .expect("Invalid regex"); + let nasal_consonants = ["m", "n", "ng"]; + // [blg]Vnn(h) + if ["b", "l", "g"].contains(&onset) && nnh_pattern.is_match(coda){ + println!("Illegal pattern [blg]Vnn(h), latin_word={}", latin_word); + return None; + // bVm + } else if onset == "b" && coda == "m"{ + println!("Illegal pattern [bm]Vm, latin_word={}", latin_word); + return None; + // (m|n|ng)V(n|m|ng) + } else if nasal_consonants.contains(&onset) && nasal_consonants.contains(&coda){ + println!("Illegal pattern (m|n|ng)V(n|m|ng), latin_word={}", latin_word); + return None; + } + // -e[nt] + else if e_pattern.is_match(vowel) && ["n", "t"].contains(&coda){ + println!("Illegal pattern -e[nt], latin_word={}", latin_word); + return None; + } + else{ + let syllable_without_diacritics : String = chars_without_diacritics.iter().collect(); + let syllable_with_tone_number : String = format!("{}{}", syllable_without_diacritics, tone_number); + println!("latin_word={} ({:?}) returns TRUE\n\n", latin_word, syllable_with_tone_number); + return Some(syllable_with_tone_number); + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 97d4d57..93224e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,11 @@ fn main(){ 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"]; + "sn̍gh", "hm̂", "m̄", "m", "m̍h", "mn̍gh", "sía", "góa", + "bân", "bâm", "ngāng", "nàm", "nàng", "lêt", "le̍t", "len"]; for w in a { - is_diacriticized_tl_single_syllable(w); + is_diacriticized_tl_single_syllable_aux(w); } // download_corpus_content("dict/kamjitian/kamjitian.hanji.dict.yaml", ".");