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