Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:12:20+00:00 2026-05-24T05:12:20+00:00

I am currently following Beginning Ruby by Peter Cooper and have put together my

  • 0

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 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."

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:12:22+00:00Added an answer on May 24, 2026 at 5:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following CSS for my 4 columns which might grow to
I currently have the following row in my table: course_data: user_id days <-- This
I currently have the following ValuesQuerySet object after running the following code: sites =
I currently have a Combobox like the following: //XAML <ComboBox> <ComboBoxItem> Awake & Alive</ComboBoxItem>
Say I have the following style of lines in a text file: 12 34
P.S. I am new to php & webform scripting; Currently I have the following
I'm currently beginning to learn Ruby and the Ruby on Rails framework. I've found
I currently have the following loop: do { checkedWord = articleWords.Dequeue().TrimEnd('?', '.', ',', '!');
Currently, I have a stringstream called Data . I am seeking to the beginning
I have the following String Sample however in some occurences there may be one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.