diff --git a/wasm-rs/Cargo.lock b/wasm-rs/Cargo.lock index f9a0e3a..41bec02 100644 --- a/wasm-rs/Cargo.lock +++ b/wasm-rs/Cargo.lock @@ -26,6 +26,17 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -66,6 +77,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + [[package]] name = "log" version = "0.4.21" @@ -96,10 +113,12 @@ dependencies = [ [[package]] name = "onc" -version = "0.1.0" +version = "0.2.0" dependencies = [ "itertools", "nom", + "rand", + "rand_chacha", "serde", ] @@ -109,6 +128,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.79" @@ -127,6 +152,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "serde" version = "1.0.197" @@ -218,6 +273,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -280,6 +341,7 @@ dependencies = [ "itertools", "lazy_static", "onc", + "rand", "serde", "serde-wasm-bindgen", "toml", diff --git a/wasm-rs/Cargo.toml b/wasm-rs/Cargo.toml index 61241cf..c0b56c0 100644 --- a/wasm-rs/Cargo.toml +++ b/wasm-rs/Cargo.toml @@ -12,7 +12,8 @@ crate-type = ["lib", "cdylib"] [dependencies] itertools = "0.12.1" lazy_static = { version = "1.4.0", optional = true } -onc = { version = "0.1.0", path = "onc" } +onc = { version = "0.2.0", path = "onc" } +rand = { version = "0.8.5", optional = true } serde = { version = "1.0.197", features = ["derive"] } serde-wasm-bindgen = "0.6.5" toml = "0.8.11" @@ -20,7 +21,12 @@ wasm-bindgen = { version = "0.2.92", features = ["serde"] } [features] lazystatic = ["dep:lazy_static"] +faker = ["onc/faker", "dep:rand"] [[example]] name = "imbuhan" required-features = ["lazystatic"] + +[[example]] +name = "faker" +required-features = ["faker"] diff --git a/wasm-rs/examples/faker.rs b/wasm-rs/examples/faker.rs new file mode 100644 index 0000000..7985a3d --- /dev/null +++ b/wasm-rs/examples/faker.rs @@ -0,0 +1,27 @@ +use onc::phonotactics::Faker; +use rand::seq::SliceRandom; +use wasm_rs::functions::parse_default_tatabunyi_toml; + +const CONFIG: &'static str = r#" +default = "Melayu Lama" + +[[phonotactic]] +name = "Melayu Lama" +definition.onset = [["ny", "ng", "sw", "m", "n", "p", "t", "c", "k", "b", "d", "j", "g", "s", "h", "l", "y", "w", "r"]] +definition.nucleus = [["a", "e", "i", "o", "u"]] +definition.coda = [["ng", "m", "n", "p", "t", "k", "s", "h", "l", "r"]] +"#; + +fn main() { + println!("<< Example: Fake word generator>"); + let phonotactic = parse_default_tatabunyi_toml(CONFIG.to_string()); + let mut faker = Faker::default(); + + for _ in 0..5 { + let n = [2, 3, 4, 5].choose(&mut faker.rng).unwrap(); + println!( + "{}", + phonotactic.as_rule().generate_fake_word(*n, &mut faker.rng) + ); + } +} diff --git a/wasm-rs/onc/Cargo.toml b/wasm-rs/onc/Cargo.toml index 1dd8667..de630a7 100644 --- a/wasm-rs/onc/Cargo.toml +++ b/wasm-rs/onc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "onc" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,4 +8,9 @@ edition = "2021" [dependencies] itertools = "0.12.1" nom = "7.1.3" +rand = { version = "0.8.5", optional = true } +rand_chacha = { version = "0.3.1", optional = true } serde = { version = "1.0.197", features = ["derive"] } + +[features] +faker = ["dep:rand", "dep:rand_chacha"] diff --git a/wasm-rs/onc/src/phonotactics.rs b/wasm-rs/onc/src/phonotactics.rs index 8d21334..002e57c 100644 --- a/wasm-rs/onc/src/phonotactics.rs +++ b/wasm-rs/onc/src/phonotactics.rs @@ -6,11 +6,30 @@ use std::fmt::Display; use crate::phonotactics::tags::SyllableTags; +#[cfg(feature = "faker")] +use rand::{prelude::SliceRandom, SeedableRng}; +#[cfg(feature = "faker")] +use rand_chacha::ChaCha8Rng; + #[derive(Clone, Default)] pub struct PhonotacticRule { definition: SyllableTags, } +#[cfg(feature = "faker")] +pub struct Faker { + pub rng: ChaCha8Rng, +} + +#[cfg(feature = "faker")] +impl Default for Faker { + fn default() -> Self { + Self { + rng: ChaCha8Rng::seed_from_u64(42), + } + } +} + impl PhonotacticRule { pub fn with_definitions(definition: SyllableTags) -> Self { PhonotacticRule { definition } @@ -21,6 +40,22 @@ impl PhonotacticRule { .parse_tags(&input) .map(|(r, p)| (r, p.with_postprocessing(&self.definition))) } + + #[cfg(feature = "faker")] + pub fn generate_fake_word(&self, syllables: usize, rng: &mut ChaCha8Rng) -> String { + let mut fake = String::new(); + let mut inner_definition = self.definition.clone(); + // Possibly empty onset or coda + inner_definition.onset.items.push("".into()); + inner_definition.coda.items.push("".into()); + for _ in 0..syllables { + let onset = inner_definition.onset.items.choose(rng).unwrap(); + let nucleus = inner_definition.nucleus.items.choose(rng).unwrap(); + let coda = inner_definition.coda.items.choose(rng).unwrap(); + fake = format!("{}{}{}{}", fake, onset, nucleus, coda); + } + fake + } } pub struct Phrase { diff --git a/wasm-rs/src/phonotactics.rs b/wasm-rs/src/phonotactics.rs index 4d7502a..41644a8 100644 --- a/wasm-rs/src/phonotactics.rs +++ b/wasm-rs/src/phonotactics.rs @@ -11,13 +11,13 @@ pub struct Phonotactic { name: String, definition: SyllableTags, } - -#[wasm_bindgen] impl Phonotactic { - pub(crate) fn as_rule(&self) -> PhonotacticRule { + pub fn as_rule(&self) -> PhonotacticRule { PhonotacticRule::with_definitions(self.definition.clone()) } - +} +#[wasm_bindgen] +impl Phonotactic { #[wasm_bindgen] pub fn parse_string(&mut self, input: String, options: ParseResultOptions) -> ParseResults { let text = input.to_lowercase();