From: Max Norman Date: 2009-07-22T09:04:10+09:00 Subject: Trouble Counting Words, Sentences and Paragraphs I'm working on the first example application from 'Learning Ruby, from Novice to Professional,' a text analyzer that counts the number of characters (with and without spaces), lines, words, sentences and paragraphs in a text document. Unfortunately, I've run into trouble: the numbers don't seem to be coming out right. Attached is the text file, for testing purposes, and below is the source: lines = File.readlines("text.txt") line_count = lines.size text = lines.join puts "#{line_count} lines." total_characters = text.length puts "#{total_characters} characters." total_characters_nonspaces = text.gsub(/\s+/, '').length puts "#{total_characters_nonspaces} characters, excluding spaces." word_count = text.split.length puts "#{word_count} words." paragraph_count = text.split(/\n\n/).length puts "#{paragraph_count} paragraphs." sentence_count = text.split(/\.|\? |!/).length puts "#{sentence_count} sentences." -- Here are the results I get: 42 lines. 6446 characters. 5315 characters, excluding spaces. 1130 words. 2 paragraphs. 44 sentences. Any and all help/advice would be appreciated. Attachments: http://www.ruby-forum.com/attachment/3890/text.txt -- Posted via http://www.ruby-forum.com/.