class BookBot::Section
Attributes
title[RW]
Public Class Methods
new(file)
click to toggle source
# File lib/bookbot/section.rb, line 8 def initialize(file) @file = file slurp_file ascii_scrub # Report stuff report_scrub_string(@file_string.clone) count_words count_sentences count_syllables end
Public Instance Methods
ascii_scrub(string = @file_string)
click to toggle source
def has_bom?
@file_string.match(/^\uFEFF/)
end
# File lib/bookbot/section.rb, line 40 def ascii_scrub(string = @file_string) string.gsub!(/\uFEFF/, '') # BOM string.gsub!(/\u2018/, "\'") # Apos string.gsub!(/\u2019/, "\'") # Apos string.gsub!(/\u0060/, "\'") # Apos string.gsub!(/\u00B4/, "\'") # Apos string.gsub!(/\u201C/, "\"") # Double Quote string.gsub!(/\u201D/, "\"") # Double Quote string.gsub!(/\u2026/, "...") # Ellipsis string end
average_word_length()
click to toggle source
# File lib/bookbot/section.rb, line 79 def average_word_length word_char_count = @clean_array.inject(0) { |sum, word| sum += word.length } avg = word_char_count.to_f / @clean_array.length avg.round(1) end
char_count()
click to toggle source
# File lib/bookbot/section.rb, line 71 def char_count @file_string.length end
contraction_count()
click to toggle source
# File lib/bookbot/section.rb, line 116 def contraction_count # this falsely reports single quoted strings, and possessives. @file_string.count("'") end
count_sentences()
click to toggle source
# File lib/bookbot/section.rb, line 75 def count_sentences @sentences = @file_string.count('.') + @file_string.count('?') end
count_syllables()
click to toggle source
# File lib/bookbot/section.rb, line 108 def count_syllables @syllables = 0 @clean_array.each { |word| @syllables += syllables_per_word(word) } @syllables end
count_words()
click to toggle source
# File lib/bookbot/section.rb, line 85 def count_words @word_count = @clean_array.length end
file_name()
click to toggle source
# File lib/bookbot/section.rb, line 26 def file_name File.basename(@file, '.*').downcase end
file_name_label()
click to toggle source
# File lib/bookbot/section.rb, line 30 def file_name_label label = file_name.gsub(/ /, "_") label.gsub!(/[^a-z0-9_]/, "") label end
grade_level()
click to toggle source
# File lib/bookbot/section.rb, line 121 def grade_level # https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests # 0.39 * ( total_words / total_sentences ) # + 11.8 * ( total_syllables / total_words ) # - 15.59 # level = 0.39 * ( @word_count.to_f / @sentences ) level += 11.8 * ( @syllables.to_f / @word_count ) level -= 15.59 level.round(1) end
report_scrub_string(string)
click to toggle source
# File lib/bookbot/section.rb, line 57 def report_scrub_string(string) string.gsub!(/n't/, ' ') string.gsub!(/'ll/, ' ') string.gsub!(/'s/, ' ') string.gsub!(/'ve/, ' ') string.gsub!(/'re/, ' ') @clean_string = string.gsub(/[,:!.?'"-]/, ' ').downcase @clean_array = @clean_string.split() @clean_array.each { |word| @clean_array.delete(word) unless word =~ /[aeiou]/i @clean_array.delete(word) if word =~ /^ing/i } end
slurp_file()
click to toggle source
# File lib/bookbot/section.rb, line 20 def slurp_file File.open(@file) { |f| @file_string = f.read } end
syllables_per_word(word)
click to toggle source
# File lib/bookbot/section.rb, line 89 def syllables_per_word(word) count = 0 vowels = "aeiouy" if vowels.include?(word[0]) count += 1 end word.each_char.with_index { |letter, index| next if index == 0 if vowels.include?(letter) unless vowels.include?(word[word.index(letter) - 1]) count += 1 end end } count -= 1 if word.end_with?('e') count += 1 if count == 0 count end
to_s()
click to toggle source
# File lib/bookbot/section.rb, line 52 def to_s @file_string.strip! @file_string end