class
WordMage::PhonemeSet
- WordMage::PhonemeSet
- Reference
- Object
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.crConstructors
-
.new(consonants : Set(String), vowels : Set(String))
Creates a new PhonemeSet with the given consonants and vowels.
Instance Method Summary
-
#add_phoneme(phoneme : String, type : Symbol, positions : Array(Symbol) = [] of Symbol)
Adds a phoneme to the set with optional positional constraints.
-
#add_weight(phoneme : String, weight : Float32)
Assigns a weight to a phoneme for weighted sampling.
- #consonants : Set(String)
- #consonants=(consonants : Set(String))
-
#get_consonants(position : Symbol | Nil = nil) : Array(String)
Returns consonants, optionally filtered by position.
-
#get_vowels(position : Symbol | Nil = nil) : Array(String)
Returns vowels, optionally filtered by position.
-
#is_vowel?(phoneme : String) : Bool
Checks if a phoneme is a vowel.
- #position_rules : Hash(Symbol, Set(String))
- #position_rules=(position_rules : Hash(Symbol, Set(String)))
-
#sample_phoneme(type : Symbol, position : Symbol | Nil = nil) : String
Randomly selects a phoneme of the given type, respecting position and weights.
- #vowels : Set(String)
- #vowels=(vowels : Set(String))
- #weights : Hash(String, Float32)
- #weights=(weights : Hash(String, Float32))
Constructor Detail
Creates a new PhonemeSet with the given consonants and vowels.
Instance Method Detail
Adds a phoneme to the set with optional positional constraints.
Parameters
phoneme
: The phoneme string to addtype
: 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
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
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
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
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