Class: Demystify::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/demystify.rb

Overview

Currency = [“¤”, “₳”,​ “฿”, “₵”, “¢”, “₡”, “₢”, “$”, “₫”, “₯”, “₠”, “€”,

"ƒ", "₣", "₲", "₴", "₭", "₺", "ℳ", "₥", "₦", "₧", "₱", "₰",
​"£", "៛", "₨", "₪", "৳", ​"₮", "₩", "¥"]

Instance Attribute Summary collapse

Instance Method Summary collapse

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_hashObject

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

#charsObject

Returns the value of attribute chars.



23
24
25
# File 'lib/demystify.rb', line 23

def chars
  @chars
end

#contentObject

Returns the value of attribute content.



23
24
25
# File 'lib/demystify.rb', line 23

def content
  @content
end

#first_wordsObject

Returns the value of attribute first_words.



23
24
25
# File 'lib/demystify.rb', line 23

def first_words
  @first_words
end

#forwards_probability_hashObject

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_wordsObject

Returns the value of attribute last_words.



23
24
25
# File 'lib/demystify.rb', line 23

def last_words
  @last_words
end

#sentencesObject

Returns the value of attribute sentences.



23
24
25
# File 'lib/demystify.rb', line 23

def sentences
  @sentences
end

#wordsObject

Returns the value of attribute words.



23
24
25
# File 'lib/demystify.rb', line 23

def words
  @words
end

Instance Method Details

#average_sentence_lengthObject



125
126
127
# File 'lib/demystify.rb', line 125

def average_sentence_length
  word_count / sentence_count
end

#average_word_lengthObject



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_countObject



41
42
43
# File 'lib/demystify.rb', line 41

def char_count
  @chars.length
end

#intellectual_property_countObject



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_countObject



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_countObject



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_countObject



89
90
91
# File 'lib/demystify.rb', line 89

def non_letter_count
  punctuation_count + symbol_count + intellectual_property_count
end

#non_whitespace_char_countObject



61
62
63
# File 'lib/demystify.rb', line 61

def non_whitespace_char_count
  char_count - (spaces_count + new_line_count)
end

#punctuation_countObject



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_countObject



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_countObject



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_countObject



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_countObject



109
110
111
# File 'lib/demystify.rb', line 109

def word_count
  @words.count
end