Last active
August 29, 2015 14:11
-
-
Save hpjaj/be0961c5fc5cd6cc8807 to your computer and use it in GitHub Desktop.
week 3 - 7e - Document Stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reads_a_file(file) | |
files_content = File.read(file) | |
end | |
def character_count_with_spaces(file_string) | |
file_string.length | |
end | |
def character_count_without_spaces(file_string) | |
file_string.split(" ").join.length | |
end | |
def line_count(file_string) | |
file_string.lines.count | |
end | |
def word_count(file_string) | |
file_string.split(" ").size | |
end | |
def sentence_count(file_string) | |
file_string.split(/[.?!]/).length | |
end | |
def paragraph_count(file_string) | |
file_string.split(/\n\n/).length | |
end | |
def average_words_per_sentence(num_words, num_sentences) | |
( num_words.to_f / num_sentences ).round(2) | |
end | |
def average_sentences_per_paragraph(num_sentences, num_paragraphs) | |
( num_sentences.to_f / num_paragraphs ).round(2) | |
end | |
# Uses this 'file' variable to capture the desired file when calling 'analyzer.rb' | |
file = ARGV[0] | |
# Uses this 'text' variable to read and hold the desired | |
# file's content, in the form of a singular string | |
text = reads_a_file(file) | |
puts "The file '#{file}' has the following statistics:" | |
puts "Character count: #{character_count_with_spaces(text)}" | |
puts "Character count (excluding spaces): #{character_count_without_spaces(text)}" | |
puts "Line count: #{line_count(text)}" | |
puts "Word count: #{word_count(text)}" | |
puts "Sentence count: #{sentence_count(text)}" | |
puts "Paragraph count: #{paragraph_count(text)}" | |
puts "Average number of words per sentence: #{average_words_per_sentence(word_count(text), sentence_count(text) )}" | |
puts "Average number of sentences per paragraph: #{average_sentences_per_paragraph(sentence_count(text), paragraph_count(text) )}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment