Class: Phrase
- Inherits:
-
Object
- Object
- Phrase
- 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
-
#phrase ⇒ Object
Returns the value of attribute phrase.
Instance Method Summary collapse
-
#dict? ⇒ Boolean
returns whether the last word in the phrase a dictionary word (useful to know before calling rhymes and rhyme_keys).
-
#flat_rhymes ⇒ Object
return a flat array of rhymes, rather than by pronunciation.
-
#initialize(phrase) ⇒ Phrase
constructor
A new instance of Phrase.
-
#last_word ⇒ Object
returns the last word in the phrase (the one used for rhyming).
-
#rhyme_key ⇒ Object
returns the first rhyme key or nil.
-
#rhyme_keys ⇒ Object
returns the rhyme keys associated with this word (useful in matching with other words to see if they rhyme).
-
#rhymes ⇒ Object
returns a map from rhyme key to a list of rhyming words in that key.
-
#syllables ⇒ Object
returns the number of syllables in the phrase.
- #to_s ⇒ Object
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
#phrase ⇒ Object
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)
40 41 42 |
# File 'lib/ruby_rhymes.rb', line 40 def dict? @last_word_pronunciation.first.dict? end |
#flat_rhymes ⇒ Object
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_word ⇒ Object
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_key ⇒ Object
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_keys ⇒ Object
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 |
#rhymes ⇒ Object
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 |
#syllables ⇒ Object
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_s ⇒ Object
60 61 62 |
# File 'lib/ruby_rhymes.rb', line 60 def to_s @phrase end |