Class: Phrase

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rhymes.rb,
lib/syllable_arrays.rb

Overview

this class is the gateway to generating exciting poetry

Defined Under Namespace

Classes: Pronunciation, Pronunciations

Instance Method Summary collapse

Constructor Details

#initialize(phrase) ⇒ Phrase

Returns a new instance of Phrase.



7
8
9
10
11
12
13
# File 'lib/ruby_rhymes.rb', line 7

def initialize(phrase)
  @phrase_tokens = Phrase.clean_and_tokenize(phrase)
  
  # [[p1a,p1b],[p2],p3]
  @pronunciations = @phrase_tokens.map{|pt| Pronunciations.get_pronunciations(pt)} #pronunciation objects
  @last_word_pronunciation = @pronunciations.last
end

Instance Method Details

#dict?Boolean

returns whether the last word in the phrase a dictionary word (useful to know before calling rhymes and rhyme_keys)

Returns:

  • (Boolean)


31
32
33
# File 'lib/ruby_rhymes.rb', line 31

def dict?
  @last_word_pronunciation.first.dict?
end

#flat_rhymesObject

return a flat array of rhymes, rather than by pronunciation



42
43
44
# File 'lib/ruby_rhymes.rb', line 42

def flat_rhymes
  rhymes.empty? ? [] : @rhymes.values.flatten
end

#rhyme_keyObject

returns the first rhyme key or nil



21
22
23
# File 'lib/ruby_rhymes.rb', line 21

def rhyme_key
  rhyme_keys.first
end

#rhyme_keysObject

returns the rhyme keys associated with this word (useful in matching with other words to see if they rhyme)



16
17
18
# File 'lib/ruby_rhymes.rb', line 16

def rhyme_keys
  @last_word_pronunciation.map(&:rhyme_key).compact||[]
end

#rhymesObject

returns a map from rhyme key to a list of rhyming words in that key



36
37
38
39
# File 'lib/ruby_rhymes.rb', line 36

def rhymes
  @rhymes = load_rhymes if @rhymes.nil?
  @rhymes
end

#syllablesObject

returns the number of syllables in the phrase



26
27
28
# File 'lib/ruby_rhymes.rb', line 26

def syllables
  @pronunciations.map{|p| p.first.num_syllables}.inject(:+)
end