My readability program seems to work perfectly and since the output matches all of the outputs for the given inputs in the problem specification. However, when I submit my program, I only receive 31%.
Here is my code :
from nltk.tokenize import sent_tokenize
from cs50 import get_string
def main():
# store text
text = get_string("Text: ")
# find number of words
word_num = len(text.split())
q = word_num / 100
# find and calculate average number of letters per 100
let_num = len([text[index:index+1] for index in range(len(text)) if text[index:index+1].isalpha() == True])
let_av = let_num / q
# find and calculate average number of sentences per 100
sen_num = len(sent_tokenize(text))
sen_av = sen_num / q
index = round(0.0588 * let_av - 0.296 * sen_av - 15.8)
if index < 1:
print("Before Grade 1")
elif index > 1 and index < 16:
print(f"Grade: {index}")
else:
print("Grade 16+")
if __name__ == "__main__":
main ()
sent_tokenize
is not exactly matching the specification. I'm actually more confused by this running at all, since nltk.tokenize used to not work well with thesentimental/readability
checker.text[index:index+1]
for a string is same astext[index]
, at least for a valid index, which you should have. – Blauelf Jan 16 '20 at 14:07sent_tokenize
furthermore there is no check50 link :/. Thanks for thetext[index]
suggestion! – rassenguy Jan 16 '20 at 14:54that any occurrence of a period, exclamation point, or question mark indicates the end of a sentence
, andsent_tokenize
does not always agree with that, in both directions. The result for the sample texts would probably be the same, I've just read in other places that using the nltk package causes issues on the server, crashing the script before it comes to the input prompt. – Blauelf Jan 16 '20 at 15:06