class WordMage::VowelHarmony

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

Defined in:

vowel_harmony.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(pull : JSON::PullParser) #

[View source]
def self.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.

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

[View source]

Instance Method Detail

def active? : Bool #

Checks if harmony rules are defined.

Returns

true if rules exist and strength > 0, false otherwise


[View source]
def add_rule(from_vowel : String, to_vowel : String, preference : Float32) #

Adds or updates a harmony rule.

Parameters

  • from_vowel: The source vowel
  • to_vowel: The target vowel
  • preference: Preference weight (0.0-1.0+)

Example

harmony.add_rule("a", "e", 0.8)  # "a" prefers "e"

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

Analyzes the harmony rules for consistency.

Returns

Hash with analysis metrics


[View source]
def avoided_vowels(from_vowel : String, count : Int32 = 3) : Array(String) #

Returns the least preferred vowels for a given source vowel.

Parameters

  • from_vowel: The source vowel
  • count: Number of bottom preferences to return (default: 3)

Returns

Array of vowel strings ordered by least preference


[View source]
def default_preference : Float32 #

Default preference for vowels not in rules


[View source]
def default_preference=(default_preference : Float32) #

Default preference for vowels not in rules


[View source]
def get_transition_weight(from_vowel : String, to_vowel : String) : Float32 #

Gets the preference weight for a vowel transition.

Parameters

  • from_vowel: The current vowel
  • to_vowel: The potential next vowel

Returns

Float32 preference weight (0.0-1.0+)

Example

weight = harmony.get_transition_weight("a", "e")  # 0.8

[View source]
def preferred_vowels(from_vowel : String, count : Int32 = 3) : Array(String) #

Returns the most preferred vowels for a given source vowel.

Parameters

  • from_vowel: The source vowel
  • count: Number of top preferences to return (default: 3)

Returns

Array of vowel strings ordered by preference


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

Harmony rules: first vowel -> {second vowel -> preference weight}


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

Harmony rules: first vowel -> {second vowel -> preference weight}


[View source]
def select_vowel(from_vowel : String | Nil, available_vowels : Array(String)) : String #

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"])

[View source]
def strength : Float32 #

Harmony strength: 0.0 = ignore rules, 1.0 = absolute rules


[View source]
def strength=(strength : Float32) #

Harmony strength: 0.0 = ignore rules, 1.0 = absolute rules


[View source]
def summary : String #

Generates a summary of the vowel harmony configuration.

Returns

String describing the harmony setup


[View source]