Skip to content

Commit

Permalink
⚡ Choose weighted for onset/coda
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed May 26, 2024
1 parent 2c3716a commit 92e84db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions wasm-rs/onc/src/phonotactics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions wasm-rs/onc/src/phonotactics/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
pub items: Vec<T>,
#[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<T> {
pub onset: AltTagVec<T>,
pub nucleus: AltTagVec<T>,
Expand Down

0 comments on commit 92e84db

Please sign in to comment.