class
WordMage::VowelHarmony
- WordMage::VowelHarmony
- Reference
- Object
Overview
Defines vowel harmony rules and transition preferences.
VowelHarmony manages which vowels can follow other vowels, with configurable strictness from absolute rules (traditional vowel harmony) to loose statistical preferences (vowel transitions). This allows modeling both strict languages like Finnish and flexible patterns like constructed languages.
Example
# Strict harmony (Finnish-style)
harmony = VowelHarmony.new({
"a" => {"a" => 1.0, "o" => 1.0, "u" => 0.0}, # Back vowels only
"e" => {"e" => 1.0, "i" => 1.0, "y" => 1.0} # Front vowels only
}, strength: 1.0)
# Loose transitions (Elvish-style)
harmony = VowelHarmony.new({
"a" => {"e" => 0.8, "o" => 0.6, "u" => 0.2} # Preferences
}, strength: 0.7)
Included Modules
- JSON::Serializable
Defined in:
vowel_harmony.crConstructors
- .new(pull : JSON::PullParser)
-
.new(rules : Hash(String, Hash(String, Float32)) = Hash(String, Hash(String, Float32)).new, strength : Float32 = 0.0_f32, default_preference : Float32 = 0.5_f32)
Creates a new VowelHarmony configuration.
Instance Method Summary
-
#active? : Bool
Checks if harmony rules are defined.
-
#add_rule(from_vowel : String, to_vowel : String, preference : Float32)
Adds or updates a harmony rule.
-
#analyze_consistency : Hash(String, Float32)
Analyzes the harmony rules for consistency.
-
#avoided_vowels(from_vowel : String, count : Int32 = 3) : Array(String)
Returns the least preferred vowels for a given source vowel.
-
#default_preference : Float32
Default preference for vowels not in rules
-
#default_preference=(default_preference : Float32)
Default preference for vowels not in rules
-
#get_transition_weight(from_vowel : String, to_vowel : String) : Float32
Gets the preference weight for a vowel transition.
-
#preferred_vowels(from_vowel : String, count : Int32 = 3) : Array(String)
Returns the most preferred vowels for a given source vowel.
-
#rules : Hash(String, Hash(String, Float32))
Harmony rules: first vowel -> {second vowel -> preference weight}
-
#rules=(rules : Hash(String, Hash(String, Float32)))
Harmony rules: first vowel -> {second vowel -> preference weight}
-
#select_vowel(from_vowel : String | Nil, available_vowels : Array(String)) : String
Selects a vowel based on harmony rules and weights.
-
#strength : Float32
Harmony strength: 0.0 = ignore rules, 1.0 = absolute rules
-
#strength=(strength : Float32)
Harmony strength: 0.0 = ignore rules, 1.0 = absolute rules
-
#summary : String
Generates a summary of the vowel harmony configuration.
Constructor Detail
Creates a new VowelHarmony configuration.
Parameters
#rules
: Hash mapping vowels to their preferred followers#strength
: How strictly to follow rules (0.0-1.0)#default_preference
: Default weight for unspecified transitions
Instance Method Detail
Adds or updates a harmony rule.
Parameters
from_vowel
: The source vowelto_vowel
: The target vowelpreference
: Preference weight (0.0-1.0+)
Example
harmony.add_rule("a", "e", 0.8) # "a" prefers "e"
Returns the least preferred vowels for a given source vowel.
Parameters
from_vowel
: The source vowelcount
: Number of bottom preferences to return (default: 3)
Returns
Array of vowel strings ordered by least preference
Gets the preference weight for a vowel transition.
Parameters
from_vowel
: The current vowelto_vowel
: The potential next vowel
Returns
Float32 preference weight (0.0-1.0+)
Example
weight = harmony.get_transition_weight("a", "e") # 0.8
Returns the most preferred vowels for a given source vowel.
Parameters
from_vowel
: The source vowelcount
: Number of top preferences to return (default: 3)
Returns
Array of vowel strings ordered by preference
Harmony rules: first vowel -> {second vowel -> preference weight}
Harmony rules: first vowel -> {second vowel -> preference weight}
Selects a vowel based on harmony rules and weights.
Parameters
from_vowel
: The current vowel (nil if first vowel)available_vowels
: Array of possible vowels to choose from
Returns
String representing the selected vowel
Example
next_vowel = harmony.select_vowel("a", ["e", "i", "o", "u"])