Skip to content

Instantly share code, notes, and snippets.

@dzhlobo
Created January 4, 2012 07:16
Show Gist options
  • Save dzhlobo/1558927 to your computer and use it in GitHub Desktop.
Save dzhlobo/1558927 to your computer and use it in GitHub Desktop.
Simple text analyzer
# 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