小型文本编辑器

2015-06-22 0 890
小型文本编辑器
stop_words = %w{the by on for of are with just but and to the my I has some in}
lines = File.readlines(ARGV[0])
line_count = lines.size
text = lines.join
total_characters = text.length
total_characters_nospaces = text.gsub(/\s+/,'').length
word_count = text.split.length
sentence_count = text.split(/\.|\?|!/).length
paragraph_count = text.split(/\n\n/).length
all_words = text.scan(/\w/)
good_words = all_words.select{|word|!stop_words.include?(word)}
good_percentage = ((good_words.length.to_f / all_words.length.to_f)*100).to_i
sentences = text.gsub(/\s+/,'').strip.split(/\.|\?|\!/)
sentences_sorted = sentences.sort_by {|sentence| sentence.length}
one_third = sentences_sorted.length / 3
ideal_sentences = sentences_sorted.slice(one_third,one_third + 1)
ideal_sentences = ideal_sentences.select {|sentence|sentence =~/is/}


puts "#{line_count} lines"
puts "#{total_characters} characters"
puts "#{total_characters_nospaces} characters excluding space"
puts "#{word_count} words"
puts "#{sentence_count} sentences"
puts "#{paragraph_count} paragraphs"
puts "#{sentence_count / paragraph_count} sentences per paragraph average"
puts "#{word_count / sentence_count} words per sentence average"
puts "#{good_percentage}%of words are non-fluff words"
puts "Summary:\n \n" + ideal_sentences.join(".")
puts "--End of analysis"

遇见资源网 ruby 小型文本编辑器 http://www.ox520.com/16376.html

上一篇: Bcode的ruby实现
下一篇: 去除C语言注释
常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务