-2

I have a ruby code that supposes to read a paragraph and find the total number of characters, words, and sentences then find the ARI (Automated Readability Index) and decide which grade level. It's my first time using ruby and i'm not sure how to call the function

is this correct to write it after the last end

calcARI("paragraph.txt")

paragraph.txt is the name of the file

this is the code

    def calcARI(paragraph)

      file = File.open("paragraph.txt","r")

      if file

        file_data = file.read

        file.close

        charCount=0

        wordCount=0

        sentenceCount=0

        ARIvalue==0

        gradeLevel=""

        file.each_line { |line|

            charCount+=line.size

            for i in 1..line.length

                if(line[i]=='.')

                    sentenceCount+=1

                end

                if(line[i]==' ')
                        wordCount+=1

                end

            end

            

        }

        ARIvalue==4.71*(charCount/wordCount)+0.5*(wordCount/sentenceCount)-21.43

        case ARIvalue

        when 1

            gradeLevel="5-6 Kindergarten"

        when 2

            gradeLevel="6-7 First/Second Grade"

        when 3

            gradeLevel="7-9 Third Grade"

        when 4

            gradeLevel="9-10 Fourth Grade"

        when 5

            gradeLevel="10-11Fifth Grade"

        when 6

            gradeLevel="11-12 Sixth Grade"

        when 7

            gradeLevel="12-13 Seventh Grade"

        when 8

            gradeLevel= "13-14 Eighth Grade"

        when 9

            gradeLevel= "14-15 Ninth Grade"

        when 10

            gradeLevel= "15-16 Tenth Grade"

        when 11

            gradeLevel= "16-17 Eleventh Grade"

        when 12

            gradeLevel= "17-18 Twelth Grade"

        when 13

            gradeLevel= "18-24 College student"

        when 14

            gradeLevel="24+ Professor"

        end        

        puts "Total # of Charecter: #{charCount}"

        puts "Total # of words: #{wordCount}"

        puts "Total # of sentences: #{sentenceCount}"

        puts "Total # of Automated Readability Index: #{ARIvalue}"

        puts "Grade Level: '#{gradeLevel}''"

    else

        puts "Unable to open file!"

    end
  end
RoL
  • 13
  • 4
  • Please reduce your code to the shortest example that reproduces your problem. – user229044 Apr 11 '21 at 00:26
  • This is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Apr 11 '21 at 06:49
  • Also, lease make sure to construct a [mre]. Note that all three of those words are important: it should be an *example* only, you should not post your entire actual code, rather you should create a simplified example that demonstrates your problem. Also, it should be *minimal*, i.e. it should not contain anything that is not absolutely required to demonstrate the problem. (Most beginner problems can be demonstrated in less than 5 short simple lines of code.) And it should be *reproducible*, which means that if I copy&paste and run the code, I should see the exact same problem you see. – Jörg W Mittag Apr 11 '21 at 06:49
  • https://idownvotedbecau.se/itsnotworking/ https://idownvotedbecau.se/noexceptiondetails/ https://idownvotedbecau.se/toomuchcode/ https://idownvotedbecau.se/nomcve/ – Jörg W Mittag Apr 11 '21 at 06:50

1 Answers1

1

There are a lot of (basic?) errors in your code. Here's a correct way to write it:

def calcARI filepath
  charCount = 0
  wordCount = 0
  sentenceCount = 0
  IO.foreach(filepath) do |line|
    charCount += line.size
    wordCount += line.scan(/ /).count
    sentenceCount += line.scan(/\./).count
  end
  ariValue = 4.71 * ( charCount.to_f / wordCount ) + 0.5 * ( wordCount.to_f / sentenceCount ) - 21.43
  gradeLevel = case ariValue.ceil
               when 1
                 "5-6 Kindergarten"
               when 2
                 "6-7 First/Second Grade"
               when 3
                 "7-9 Third Grade"
               when 4
                 "9-10 Fourth Grade"
               when 5
                 "10-11Fifth Grade"
               when 6
                 "11-12 Sixth Grade"
               when 7
                 "12-13 Seventh Grade"
               when 8
                  "13-14 Eighth Grade"
               when 9
                  "14-15 Ninth Grade"
               when 10
                  "15-16 Tenth Grade"
               when 11
                  "16-17 Eleventh Grade"
               when 12
                  "17-18 Twelth Grade"
               when 13
                  "18-24 College student"
               when 14
                 "24+ Professor"
               else
                 ""
               end        
  puts "Total # of Charecter: #{charCount}"
  puts "Total # of words: #{wordCount}"
  puts "Total # of sentences: #{sentenceCount}"
  puts "Total # of Automated Readability Index: #{ariValue}"
  puts "Grade Level: '#{gradeLevel}'"
rescue StandardError => e
  STDERR.puts e.message
end

BTW, your way of counting characters, words and sentences is far from accurate, but if your ARI takes that into account then they may be correct?

To reply to your question, yes you can call calcARI after you defined it.

Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • I'm sorry but didn't try the code because I didn't know how to call it, it's my first time using ruby ever, and I'm using an online compiler so filepath doesn't work I need a file name that's why I didn't use filepath. I tried calling my code by calcARI("paragraph.txt") but it didn't work that's why I asked I thought I was calling it in a wrong way but i guess there's some errors in my code – RoL Apr 11 '21 at 18:20
  • Oh! You need your own installation of Ruby in your computer because an online Ruby compiler server doesn't have the "paragraph.txt" file installed in it. – Fravadona Apr 11 '21 at 21:53