开发者

How does this Ruby app know to select the middle third of sentences?

I am currently following Beginning Ruby by Peter Cooper and have put together my first app, a text analyzer. However, whilst I understand all of the concepts and the way in which they work, I can't for the life of me understand how the app knows to select the middle third of sentences sorted by length from this line:

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

I have included the whole app for context any help is much appreciated as so far everything is making sense.

#analyzer.rb --Text Analyzer

stopwords = %w{the a by on for of are with just but and to the 开发者_StackOverflowmy 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."

Obviously I am a beginner so plain English would help enormously, cheers.


It gets a third of the length of the sentence with one_third = sentences_sorted.length / 3 then the line you posted ideal_sentances = sentences_sorted.slice(one_third, one_third + 1) says "grab a slice of all the sentences starting at the index equal to 1/3rd and continue 1/3rd of the length +1".

Make sense?


The slice method in you look it up in the ruby API say this:

If passed two Fixnum objects, returns a substring starting at the offset given by the first, and a length given by the second.

This means that if you have a sentence broken into three pieces

 ONE | TWO | THREE

slice(1/3, 1/3+1)

will return the string starting at 1/3 from the beginning

 | TWO | THREE (this is what you are looking at now)

then you return the string that is 1/3+1 distance from where you are, which gives you

 | TWO |


sentences is a list of all sentences. sentances_sorted is that list sorted by sentence length, so the middle third will be the sentences with the most average length. slice() grabs that middle third of the list, starting from the position represented by one_third and counting one_third + 1 from that point.

Note that the correct spelling is 'sentence' and not 'sentance'. I mention this only because you have some code errors that result from spelling it inconsistently.


I was stuck on this when I first started too. In plain English, you have to realize that the slice method can take 2 parameters here.

The first is the index. The second is how long slice goes for.

So lets say you start off with 6 sentences. one_third = 2 slice(one_third, one_third+1)

1/3 of 6 is 2.

1) here the 1/3 means you start at element 2 which is index[1] 2) then it goes on for 2 (6/3) more + 1 length, so a total of 3 spaces

so it is affecting indexes 1 to index 3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜