class WordMage::PhonemeSet

Overview

Manages consonants and vowels with positional constraints and weights for word generation.

The PhonemeSet class provides a unified interface for managing phoneme inventories with support for positional rules (e.g., certain phonemes only at word boundaries) and weighted sampling for more realistic distribution.

Example

phonemes = PhonemeSet.new(Set{"p", "t", "k"}, Set{"a", "e", "i"})
phonemes.add_phoneme("p", :consonant, [:word_initial])
phonemes.add_weight("p", 2.0_f32)  # Make "p" twice as likely
consonant = phonemes.sample_phoneme(:consonant, :word_initial)

Defined in:

phoneme_set.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(consonants : Set(String), vowels : Set(String)) #

Creates a new PhonemeSet with the given consonants and vowels.


[View source]

Instance Method Detail

def add_phoneme(phoneme : String, type : Symbol, positions : Array(Symbol) = [] of Symbol) #

Adds a phoneme to the set with optional positional constraints.

Parameters

  • phoneme: The phoneme string to add
  • type: Either :consonant or :vowel
  • positions: Array of position symbols (:word_initial, :word_medial, :word_final, etc.)

Example

phonemes.add_phoneme("ng", :consonant, [:word_final])  # "ng" only at word end

[View source]
def add_weight(phoneme : String, weight : Float32) #

Assigns a weight to a phoneme for weighted sampling.

Phonemes with higher weights are more likely to be selected. Default weight is 1.0 for all phonemes.

Example

phonemes.add_weight("p", 3.0_f32)  # "p" is 3x more likely than default

[View source]
def consonants : Set(String) #

[View source]
def consonants=(consonants : Set(String)) #

[View source]
def get_consonants(position : Symbol | Nil = nil) : Array(String) #

Returns consonants, optionally filtered by position.

Parameters

  • position: Optional position to filter by (e.g., :word_initial)

Returns

Array of consonant strings that can appear at the given position


[View source]
def get_vowels(position : Symbol | Nil = nil) : Array(String) #

Returns vowels, optionally filtered by position.

Parameters

  • position: Optional position to filter by (e.g., :word_initial)

Returns

Array of vowel strings that can appear at the given position


[View source]
def is_vowel?(phoneme : String) : Bool #

Checks if a phoneme is a vowel.

Returns

true if the phoneme is in the vowels set, false otherwise


[View source]
def position_rules : Hash(Symbol, Set(String)) #

[View source]
def position_rules=(position_rules : Hash(Symbol, Set(String))) #

[View source]
def sample_phoneme(type : Symbol, position : Symbol | Nil = nil) : String #

Randomly selects a phoneme of the given type, respecting position and weights.

Parameters

  • type: Either :consonant or :vowel
  • position: Optional position constraint

Returns

A randomly selected phoneme string

Raises

Raises if no candidates are available for the given type and position


[View source]
def vowels : Set(String) #

[View source]
def vowels=(vowels : Set(String)) #

[View source]
def weights : Hash(String, Float32) #

[View source]
def weights=(weights : Hash(String, Float32)) #

[View source]