Skip to content

Instantly share code, notes, and snippets.

@kotp
Forked from johncrisostomo/analyzer_first.rb
Created May 5, 2011 18:17
Show Gist options
  • Save kotp/957569 to your computer and use it in GitHub Desktop.
Save kotp/957569 to your computer and use it in GitHub Desktop.
Document Analyzer
def document_to_string(filename)
text = ''
File.open(filename) do |f|
while line = f.gets
text << line
end
end
text
end
def character_count(text)
text.length
end
def character_count_without_spaces(text)
text.gsub(/ /,'').length
end
def line_count(text)
text.split("\n").size
end
def word_count(text)
words = text.split(' ')
words.size
end
def sentence_count(text)
sentences = text.gsub(/ /,'')
no_punctuations = sentences.gsub(/[\.\?\!\;]/, ' ')
no_punctuations.split(' ').size
end
def paragraph_count(text)
text.split("\n\n").size
end
def calculate_average_number_of_words_per_sentence(words, sentences)
words/sentences.to_f
end
def calculate_average_number_of_sentences_per_paragraph(sentences, paragraphs)
sentences/paragraphs.to_f
end
print "Enter filename : "
fname = gets.chomp!
sample = document_to_string(fname)
average_words = calculate_average_number_of_words_per_sentence(word_count(sample), sentence_count(sample.gsub(/\n/, '')))
average_sentences = calculate_average_number_of_sentences_per_paragraph(sentence_count(sample.gsub(/\n/, '')), paragraph_count(sample))
puts <<EOS
Analyzing #{fname} . . .\n
The number of characters is #{character_count(sample)}
The number of characters (without spaces) is #{character_count_without_spaces(sample.gsub(/\n/, ''))}
The number of lines is #{line_count(sample)}
The number of words is #{word_count(sample)}
The number of sentences is #{sentence_count(sample.gsub(/\n/, ''))}
The number of paragraphs is #{paragraph_count(sample)}
EOS
puts "The average number of words in a sentence is %.2f" % average_words
puts "The average number of sentences in a paragraph is %.2f" % average_sentences
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment