Skip to content

Commit

Permalink
sol: pset6/sentimental-readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jfvillablanca committed Nov 9, 2023
1 parent a4132a6 commit 98e5f16
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions pset6/sentimental-readability/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/x/2023/psets/6/readability/
57 changes: 57 additions & 0 deletions pset6/sentimental-readability/readability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from cs50 import get_string


def main():
text = get_string("Text: ")
grade = coleman_liau(count_letters(text), count_words(text), count_sentences(text))

if grade < 1:
print("Before Grade 1")
elif grade >= 16:
print("Grade 16+")
else:
print(f"Grade {grade}")


def count_letters(text):
count = 0
for char in text:
if char.isalpha():
count += 1
return count


def count_words(text):
# Assumptions that a sentence:
# - will contain at least one word;
# - will not start or end with a space; and
# - will not have multiple spaces in a row.

count = 1
for char in text:
if char == " ":
count += 1
return count


def count_sentences(text):
# Assumptions that a sentence:
# - is any sequence of characters that ends with a . or a ! or a ?;
# Sentence boundary detection here is rudimentary

count = 0
for char in text:
if char in [".", "?", "!"]:
count += 1
return count


def coleman_liau(letter_count, word_count, sentence_count):
L = letter_count / word_count * 100
S = sentence_count / word_count * 100
return round(0.0588 * L - 0.296 * S - 15.8)


if __name__ == "__main__":
main()

0 comments on commit 98e5f16

Please sign in to comment.