Class: RandomText::Dictionary
- Inherits:
-
Object
- Object
- RandomText::Dictionary
- Defined in:
- lib/random_text/dictionary.rb
Instance Method Summary collapse
-
#initialize(text) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #paragraph ⇒ Object
- #paragraphs(n) ⇒ Object
- #sentence ⇒ Object
- #sentences(n = :all) ⇒ Object
- #uniq_words(n = :all) ⇒ Object (also: #unique_words)
- #word ⇒ Object
- #words(n = :all) ⇒ Object
Constructor Details
#initialize(text) ⇒ Dictionary
Returns a new instance of Dictionary.
3 4 5 6 7 8 9 10 11 |
# File 'lib/random_text/dictionary.rb', line 3 def initialize(text) @words = RandomStrings.new(text.scan(/\w{3,}/u). collect{ |w| RandomText.downcase(w) }. reject{ |w| w =~ /^[0-9]/u }. uniq.map{ |w| w }) @sentences = RandomStrings.new(text.split(/[\r\n]+/u). map(&:strip).compact. delete_if(&:empty?).uniq) end |
Instance Method Details
#paragraph ⇒ Object
34 35 36 |
# File 'lib/random_text/dictionary.rb', line 34 def paragraph @sentences.get(5).join(' ') end |
#paragraphs(n) ⇒ Object
38 39 40 |
# File 'lib/random_text/dictionary.rb', line 38 def paragraphs(n) Array.new(n){ paragraph } end |
#sentence ⇒ Object
26 27 28 |
# File 'lib/random_text/dictionary.rb', line 26 def sentence @sentences.get end |
#sentences(n = :all) ⇒ Object
30 31 32 |
# File 'lib/random_text/dictionary.rb', line 30 def sentences(n = :all) @sentences.get(n) end |
#uniq_words(n = :all) ⇒ Object Also known as: unique_words
21 22 23 |
# File 'lib/random_text/dictionary.rb', line 21 def uniq_words(n = :all) @words.uniq(n) end |
#word ⇒ Object
13 14 15 |
# File 'lib/random_text/dictionary.rb', line 13 def word @words.get end |
#words(n = :all) ⇒ Object
17 18 19 |
# File 'lib/random_text/dictionary.rb', line 17 def words(n = :all) @words.get(n) end |