From 92e84db2a2016343137639b70a5f44080daae7b6 Mon Sep 17 00:00:00 2001 From: Thaza_Kun <61819672+Thaza-Kun@users.noreply.github.com> Date: Mon, 27 May 2024 00:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Choose=20weighted=20for=20onset/cod?= =?UTF-8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wasm-rs/onc/src/phonotactics.rs | 13 +++++++++++-- wasm-rs/onc/src/phonotactics/tags.rs | 8 ++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/wasm-rs/onc/src/phonotactics.rs b/wasm-rs/onc/src/phonotactics.rs index 2d256d1..35b63d1 100644 --- a/wasm-rs/onc/src/phonotactics.rs +++ b/wasm-rs/onc/src/phonotactics.rs @@ -47,12 +47,21 @@ impl PhonotacticRule { let mut fake = String::new(); let mut inner_definition = self.definition.clone(); // Possibly empty onset or coda + // The onset/coda are inversely weighted by its length (short onset/coda are prioritized) 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 onset = inner_definition + .onset + .items + .choose_weighted(rng, |i| 2 / (i.len() + 1)) + .unwrap(); let nucleus = inner_definition.nucleus.items.choose(rng).unwrap(); - let coda = inner_definition.coda.items.choose(rng).unwrap(); + let coda = inner_definition + .coda + .items + .choose_weighted(rng, |i| 2 / (i.len() + 1)) + .unwrap(); fake = format!("{}{}{}{}", fake, onset, nucleus, coda); } fake diff --git a/wasm-rs/onc/src/phonotactics/tags.rs b/wasm-rs/onc/src/phonotactics/tags.rs index 97c6bae..c3912a6 100644 --- a/wasm-rs/onc/src/phonotactics/tags.rs +++ b/wasm-rs/onc/src/phonotactics/tags.rs @@ -5,16 +5,16 @@ use nom::{ use super::{Phrase, SyllableUnit}; -#[derive(Clone, Default)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[derive(Clone, Default, serde::Deserialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct AltTagVec { pub items: Vec, #[serde(skip)] index: usize, } -#[derive(Clone, Default)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[derive(Clone, Default, serde::Deserialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct SyllableTags { pub onset: AltTagVec, pub nucleus: AltTagVec,