Day 34 - Analyzing Writing Level Using String Manipulation Functions

In this activity, we will take three writing samples from different academic levels, and work through the development of a function that uses text manipulation to produce a score that relates to the level of writing in the text.

Starter Code

str4th = "The moon seems to change shape every night. These shapes are called moon phases. The cycle starts with the new moon,which we can't see because it's between the Earth and the sun. Then, as the moon moves, we see more of it lit up, first a crescent, then a half moon, and finally a full moon, which is all bright and round. After the full moon, it looks like the moon is shrinking, going back through the half moon and crescent until it's new again. This whole cycle takes about a month."
str10th = "The moon goes through different phases, which make it look like it's changing shape. This happens over a cycle called a lunar month, lasting about 29.5 days. It starts with the new moon, when the moon is lined up between Earth and the sun, showing us its shadowed side. As the moon moves, we see more of its sunlit side, moving through crescent to quarter, then to a full moon when it's completely lit up. After the full moon, the moon's visible light starts to decrease, moving back towards the new moon."
strPhd = "The lunar phases reflect a cyclical pattern of visibility and illumination, governed by the moon’s synodic period of roughly 29.53 days. Initially, during the new moon phase, the moon is positioned directly between Earth and the sun, displaying its unilluminated side to us. As it orbits, the sunlit portion we can see increases—this is called waxing. It progresses through crescent to gibbous until the full moon, where the entire face is visible. Then it begins to wane, or decrease in visibility, returning once more to the new moon phase, completing the cycle."

Finished Code

def cleanPS(strText):
    strText = strText.replace(".", " ")
    strText = strText.replace(",", " ")
    strText = strText.replace("?", " ")
    strText = strText.replace("!", " ")
    strText = strText.replace("(", " ")
    strText = strText.replace(")", " ")
    strText = strText.replace("   ", " ")
    strText = strText.replace("  ", " ")
    strText = strText.replace("  ", " ")

    return strText

def retLen(string):
    return len(string)


def wordCount(strText):
    strText = cleanPS(strText)

    liText = strText.split()
    liNew = []

    for strItem in liText:
        if strItem.isnumeric() == False:
            liNew.append(strItem)

    return len(liNew)


def sentLen(strText):
    strText = strText.replace("!", ".")
    strText = strText.replace("?", ".")

    liText = strText.split(".")

    intNumSent = len(liText)

    intLength = len(strText)

    flAvgLen = intLength / intNumSent

    return flAvgLen

def wordLen(strText):
    strText = cleanPS(strText)

    liText = strText.split()
    liNew = []

    for strItem in liText:
        if strItem.isnumeric() == False or strItem != "of" or strItem != "and" or strItem != "a" or strItem != "the":
            liNew.append(strItem)

    intNumWords = len(liNew)

    intLen = len(strText)

    return int((intLen/intNumWords)*10)

def top10Len(strText):

    strClean = cleanPS(strText)

    arWords = strClean.split()

    arWords.sort(reverse=True, key=retLen)

    arTop10 = arWords[0:9]

    strTop10 = "".join(arTop10)

    return len(strTop10)

def scoreLevel(strLabel, strText):
    flSL = sentLen(strText)
    flWL = wordLen(strText)
    flTT = top10Len(strText)

    flScore = flSL + flWL + flTT

    print(f"The {strLabel} text has a score of {flScore:.2f}")


scoreLevel("4th", str4th)
scoreLevel("10th", str10th)
scoreLevel("PhD",strPhd)