Skip to content

Commit

Permalink
✨ Fake word generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed May 25, 2024
1 parent d9e39dd commit f082f30
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 7 deletions.
64 changes: 63 additions & 1 deletion wasm-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion wasm-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ 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"
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"]
27 changes: 27 additions & 0 deletions wasm-rs/examples/faker.rs
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
7 changes: 6 additions & 1 deletion wasm-rs/onc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
[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

[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"]
35 changes: 35 additions & 0 deletions wasm-rs/onc/src/phonotactics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

#[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<String>) -> Self {
PhonotacticRule { definition }
Expand All @@ -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<O: Copy> {
Expand Down
8 changes: 4 additions & 4 deletions wasm-rs/src/phonotactics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ pub struct Phonotactic {
name: String,
definition: SyllableTags<String>,
}

#[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();
Expand Down

0 comments on commit f082f30

Please sign in to comment.