diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd819bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +*~ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bd87780 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,369 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "app_data" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07ec1b6aed854f15a2226f7e2a44cc2822b4f06896bcb0ba13431b59edd7f14f" + +[[package]] +name = "buhong" +version = "0.1.0" +dependencies = [ + "app_data", + "curl", + "js-sys", + "regex", + "unicode-normalization", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "cc" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "curl" +version = "0.4.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef3ce06df3ac230a4f5c91bfd3e3961cd66f177bd9964c87deb3cce59a54fb7b" +dependencies = [ + "curl-sys", + "libc", + "openssl-probe", + "openssl-sys", + "schannel", + "socket2", + "windows-sys", +] + +[[package]] +name = "curl-sys" +version = "0.4.91+curl-8.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd39579123e45bf762716bb893d43886ed1cabe6289455be29a4f1d4e37afb06" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", + "windows-sys", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libz-sys" +version = "1.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69965d2409bff8c8cea3829009fff66a86f05a0570656552ef70f616e8e4bd85" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "openssl-probe" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" + +[[package]] +name = "openssl-sys" +version = "0.9.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19c413d6ec9ebaf5e1c829d355cb3e9f783778d700f135be0921d52fce3c60ae" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "schannel" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tinyvec" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "unicode-normalization" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e833358 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "buhong" +version = "0.1.0" +edition = "2024" +description = "a Taiwanese/Hokkien processing tool" +repository = "" +license = "MIT" + +[target.dependencies] + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +regex = "1.13.1" +unicode-normalization = "0.1.26" + +[target.'cfg(unix)'.dependencies] +app_data = "0.1.1" +curl = "0.4" + +[target.'cfg(windows)'.dependencies] +app_data = "0.1.1" +curl = "0.4" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2" +js-sys = "0.3" +web-sys = "0.3" diff --git a/README.md b/README.md index f0a4fd4..9c234d1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ +<<<<<<< HEAD # buhong -a Taigi processing tool \ No newline at end of file +a Taigi processing tool +======= +# Buhong - a Taigi toolkit + +## Requirements + - libssl-dev: 3.0.13 + - rust: 1.99.1 + - cargo: 1.99.1 + - wasm-pack: 0.15.1 + - only if you want to compile it to browser-runnable WASM code. It's recommended to install with + `cargo install wasm-pack` + +## How to build the wasm version + + - To build the browser-runnable version, run `wasm-pack build --target web` inside the project directory. + - to use in the `.html` file, insert the code: +``` + +``` + +## Other documents + + - For the implement details of the tool, see `./docs`. +>>>>>>> c4d520c (initial commit) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5338bba --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,177 @@ +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; + + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5afaf47 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,13 @@ +use buhong::*; +fn main(){ + +// Write the contents of rust-lang.org to stdout + + + // download_corpus_content("dict/kamjitian/kamjitian.hanji.dict.yaml", "."); + is_diacriticized_tl_single_syllable("góa"); + + + +} +