Created
January 4, 2012 07:16
-
-
Save dzhlobo/1558927 to your computer and use it in GitHub Desktop.
Simple text analyzer
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
# analyzer.rb -- Text Analyzer | |
lines = File.readlines(ARGV.first) | |
line_count = lines.size | |
text = lines.join | |
# Count the characters | |
total_characters = text.length | |
total_characters_nospaces = text.gsub(/\s+/, '').length | |
# Count the words, sentences, and paragraphs | |
word_count = text.split.length | |
sentence_count = text.split(/\.|\?|!/).length | |
paragraph_count = text.split(/\n\n/).length | |
# Give the analysis back to the user | |
puts "#{line_count} lines" | |
puts "#{total_characters} characters" | |
puts "#{total_characters_nospaces} characters excluding spaces" | |
puts "#{word_count} words" | |
puts "#{paragraph_count} paragraphs" | |
puts "#{sentence_count} sentences" | |
puts "#{sentence_count / paragraph_count} sentences per paragraph (average)" | |
puts "#{word_count / sentence_count} words per sentence (average)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment