ruby - 这个 Ruby 应用程序如何知道选择句子的中间三分之一?

标签 ruby slice

我目前正在关注 Peter Cooper 的 Beginning Ruby,并组装了我的第一个应用程序,一个文本分析器。然而,虽然我了解所有概念及其工作方式,但我一生都无法理解应用程序如何知道从这一行中选择按长度排序的中间三分之一的句子:

ideal_sentances = sentences_sorted.slice(one_third, one_third + 1)

我已经包含了整个应用程序的上下文,非常感谢任何帮助,因为到目前为止一切都很有意义。

#analyzer.rb --Text Analyzer

stopwords = %w{the a by on for of are with just but and to the my I has some in do}
lines = File.readlines(ARGV[0]) 
line_count = lines.size 
text = lines.join 

#Count the characters
character_count = text.length 
character_count_nospaces = text.gsub(/\s+/, '').length

#Count the words, sentances, and paragraphs
word_count = text.split.length 
paragraph_count = text.split(/\n\n/).length 
sentence_count = text.split(/\.|\?|!/).length

#Make a list of words in the text that aren't stop words,
#count them, and work out the percentage of non-stop words
#against all words
all_words = text.scan(/\w+/)
good_words = all_words.select {|word| !stopwords.include?(word)}
good_percentage = ((good_words.length.to_f / all_words.length.to_f)*100).to_i

#Summarize the text by cherry picking some choice sentances
sentances = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/)
sentances_sorted = sentences.sort_by { |sentence| sentance.length }
one_third = sentences_sorted.length / 3
ideal_sentances = sentences_sorted.slice(one_third, one_third + 1)
ideal_sentances = ideal_sentences.select{ |sentence| sentence =~ /is|are/ }

#Give analysis back to user

puts "#{line_count} lines" 
puts "#{character_count} characters" 
puts "#{character_count_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)"
puts "#{good_percentage}% of words are non-fluff words"
puts "Summary:\n\n" + ideal_sentences.join(". ")
puts "-- End of analysis."

显然我是一个初学者,所以简单的英语会有很大帮助,干杯。

最佳答案

它会使用 one_third = Sentences_sorted.length/3 获取句子长度的三分之一,然后是您发布的行 ideal_sentances = statements_sorted.slice(one_third, one_third + 1) code> 表示“从等于 1/3 的索引开始抓取所有句子的切片,并继续长度的 1/3 +1”。

有道理吗?

关于ruby - 这个 Ruby 应用程序如何知道选择句子的中间三分之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6902337/

相关文章:

ruby-on-rails - Rails simple_form text_area 不显示错误

ruby - 它是 JSON 数组吗?

ruby - 当我要求它匹配数字时,为什么扫描匹配 ""个字符?

go - 仅从 slice 中获取初始化的内容

javascript - 当未设置限制时,动态数组切片以限制长度

javascript - 使用slice()时的bug没有效果?

ruby - Rails 关联 - 同一个类的多个 has_one 关系

ruby-on-rails - ActionController::RoutingError(没有路由匹配 {:controller= >"users", :action= >"profile"})

python - 如何使用python将数组中的字符串切片到另一个数组中

python - 为散点图切片值时颜色数组的值错误