Class: Demystify::Text
- Inherits:
-
Object
- Object
- Demystify::Text
- Defined in:
- lib/demystify.rb
Overview
Currency = [“¤”, “₳”, “฿”, “₵”, “¢”, “₡”, “₢”, “$”, “₫”, “₯”, “₠”, “€”,
"ƒ", "₣", "₲", "₴", "₭", "₺", "ℳ", "₥", "₦", "₧", "₱", "₰",
"£", "៛", "₨", "₪", "৳", "₮", "₩", "¥"]
Instance Attribute Summary collapse
-
#backwards_probability_hash ⇒ Object
Returns the value of attribute backwards_probability_hash.
-
#chars ⇒ Object
Returns the value of attribute chars.
-
#content ⇒ Object
Returns the value of attribute content.
-
#first_words ⇒ Object
Returns the value of attribute first_words.
-
#forwards_probability_hash ⇒ Object
Returns the value of attribute forwards_probability_hash.
-
#last_words ⇒ Object
Returns the value of attribute last_words.
-
#sentences ⇒ Object
Returns the value of attribute sentences.
-
#words ⇒ Object
Returns the value of attribute words.
Instance Method Summary collapse
- #average_sentence_length ⇒ Object
- #average_word_length ⇒ Object
- #char_count ⇒ Object
-
#initialize(file) ⇒ Text
constructor
A new instance of Text.
- #intellectual_property_count ⇒ Object
- #letter_count ⇒ Object
- #new_line_count ⇒ Object
- #non_letter_count ⇒ Object
- #non_whitespace_char_count ⇒ Object
- #punctuation_count ⇒ Object
- #sentence_count ⇒ Object
- #sequence_count(sequence) ⇒ Object
- #spaces_count ⇒ Object
- #symbol_count ⇒ Object
- #word_count ⇒ Object
Constructor Details
#initialize(file) ⇒ Text
Returns a new instance of Text.
32 33 34 35 36 37 38 39 |
# File 'lib/demystify.rb', line 32 def initialize(file) @content = open(file).read @chars = @content.split("") @words = @content.split(/[^[[:word:]]|'|-]+/) make_sentences make_probability_hashes make_first_and_last_words end |
Instance Attribute Details
#backwards_probability_hash ⇒ Object
Returns the value of attribute backwards_probability_hash.
23 24 25 |
# File 'lib/demystify.rb', line 23 def backwards_probability_hash @backwards_probability_hash end |
#chars ⇒ Object
Returns the value of attribute chars.
23 24 25 |
# File 'lib/demystify.rb', line 23 def chars @chars end |
#content ⇒ Object
Returns the value of attribute content.
23 24 25 |
# File 'lib/demystify.rb', line 23 def content @content end |
#first_words ⇒ Object
Returns the value of attribute first_words.
23 24 25 |
# File 'lib/demystify.rb', line 23 def first_words @first_words end |
#forwards_probability_hash ⇒ Object
Returns the value of attribute forwards_probability_hash.
23 24 25 |
# File 'lib/demystify.rb', line 23 def forwards_probability_hash @forwards_probability_hash end |
#last_words ⇒ Object
Returns the value of attribute last_words.
23 24 25 |
# File 'lib/demystify.rb', line 23 def last_words @last_words end |
#sentences ⇒ Object
Returns the value of attribute sentences.
23 24 25 |
# File 'lib/demystify.rb', line 23 def sentences @sentences end |
#words ⇒ Object
Returns the value of attribute words.
23 24 25 |
# File 'lib/demystify.rb', line 23 def words @words end |
Instance Method Details
#average_sentence_length ⇒ Object
125 126 127 |
# File 'lib/demystify.rb', line 125 def average_sentence_length word_count / sentence_count end |
#average_word_length ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/demystify.rb', line 117 def average_word_length total_length = 0 @words.each do |word| total_length += word.length end total_length / word_count end |
#char_count ⇒ Object
41 42 43 |
# File 'lib/demystify.rb', line 41 def char_count @chars.length end |
#intellectual_property_count ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/demystify.rb', line 81 def intellectual_property_count count = 0 @chars.each do |char| count += 1 if Intellectual_property.include?(char) end count end |
#letter_count ⇒ Object
93 94 95 |
# File 'lib/demystify.rb', line 93 def letter_count char_count - (spaces_count + new_line_count + symbol_count + intellectual_property_count) end |
#new_line_count ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/demystify.rb', line 53 def new_line_count count = 0 @chars.each do |char| count += 1 if char == "\n" end count end |
#non_letter_count ⇒ Object
89 90 91 |
# File 'lib/demystify.rb', line 89 def non_letter_count punctuation_count + symbol_count + intellectual_property_count end |
#non_whitespace_char_count ⇒ Object
61 62 63 |
# File 'lib/demystify.rb', line 61 def non_whitespace_char_count char_count - (spaces_count + new_line_count) end |
#punctuation_count ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/demystify.rb', line 65 def punctuation_count count = 0 @chars.each do |char| count += 1 if Punctuation.include?(char) end count end |
#sentence_count ⇒ Object
113 114 115 |
# File 'lib/demystify.rb', line 113 def sentence_count @sentences.length end |
#sequence_count(sequence) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/demystify.rb', line 97 def sequence_count(sequence) count = 0 i = 0 while i < (@chars.length - sequence.length) if @chars[i...(i+sequence.length)].join("") == sequence count += 1 end i += 1 end count end |
#spaces_count ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/demystify.rb', line 45 def spaces_count count = 0 @chars.each do |char| count += 1 if char == " " end count end |
#symbol_count ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/demystify.rb', line 73 def symbol_count count = 0 @chars.each do |char| count += 1 if Symbols.include?(char) end count end |
#word_count ⇒ Object
109 110 111 |
# File 'lib/demystify.rb', line 109 def word_count @words.count end |