Class: Spanish::Syllabifier

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spanish/syllable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sounds) ⇒ Syllabifier

Returns a new instance of Syllabifier.



72
73
74
# File 'lib/spanish/syllable.rb', line 72

def initialize(sounds)
  @sounds = sounds
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



47
48
49
# File 'lib/spanish/syllable.rb', line 47

def index
  @index
end

#soundsObject (readonly)

Returns the value of attribute sounds.



48
49
50
# File 'lib/spanish/syllable.rb', line 48

def sounds
  @sounds
end

Class Method Details

.apply_stress(syllables) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/spanish/syllable.rb', line 51

def self.apply_stress(syllables)
  return syllables if syllables.detect {|s| s.stress}
  if syllables.length == 1
    syllables[0].stress = true
  else
    last = syllables.last.to_a
    penult = syllables[-2].to_a
    if last.last.vocalic? or last.last.nasal? or (last.last.alveolar? && last.last.fricative?)
      syllables[-2].stress = true
    else
      syllables.last.stress = true
    end
  end
  syllables
end

.syllabify(arg) ⇒ Object



67
68
69
70
# File 'lib/spanish/syllable.rb', line 67

def self.syllabify(arg)
  arg = arg.kind_of?(String) ? Spanish.get_sounds(arg) : arg
  apply_stress(new(arg).entries)
end

Instance Method Details

#each(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/spanish/syllable.rb', line 76

def each(&block)
  begin
    sounds.each_index { |i| @index = i; append or do_yield(&block) }
    do_yield(&block)
  ensure
    @index = 0
    @syllable = nil
  end
end