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. Use it like this:

>> phrase = “to be or not to beer”.to_phrase >> phrase.flat_rhymes

> [“adhere”, “alvear”, “amir”, …]

>> “to be or not to beer”.to_phrase.syllables

> 6

Defined Under Namespace

Classes: Pronunciation, Pronunciations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phrase) ⇒ Phrase

Returns a new instance of Phrase.



15
16
17
18
19
20
21
22
# File 'lib/ruby_rhymes.rb', line 15

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

Instance Attribute Details

#phraseObject

Returns the value of attribute phrase.



14
15
16
# File 'lib/ruby_rhymes.rb', line 14

def phrase
  @phrase
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)


40
41
42
# File 'lib/ruby_rhymes.rb', line 40

def dict?
  @last_word_pronunciation.first.dict?
end

#flat_rhymesObject

return a flat array of rhymes, rather than by pronunciation



51
52
53
# File 'lib/ruby_rhymes.rb', line 51

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

#last_wordObject

returns the last word in the phrase (the one used for rhyming)



56
57
58
# File 'lib/ruby_rhymes.rb', line 56

def last_word
  @last_word_pronunciation.first.word.downcase
end

#rhyme_keyObject

returns the first rhyme key or nil



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

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)



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

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



45
46
47
48
# File 'lib/ruby_rhymes.rb', line 45

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

#syllablesObject

returns the number of syllables in the phrase



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

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

#to_sObject



60
61
62
# File 'lib/ruby_rhymes.rb', line 60

def to_s
  @phrase
end