How to understand the flaw in my simple three part python code?

107 Views Asked by At

My Python exercise in 'classes' is as follows:

You have been recruited by your friend, a linguistics enthusiast, to create a utility tool that can perform analysis on a given piece of text. Complete the class "analyzedText" with the following methods:

  1. Constructor (_init_) - This method should take the argument text, make is lowercase and remove all punctuation. Assume only the following punctuation is used: period (.), exclamation mark (!), comma (,), and question mark (?). Assign this newly formatted text to a new attribute called fmtText.

  2. freqAll - This method should create and return dictionary of all unique words in the text along with the number of times they occur in the text. Each key in the dictionary should be the unique word appearing in the text and the associated value should be the number of times it occurs in the text. Create this dictionary from the fmtText attribute.

This was my code:

class analysedText(object)

    def __init__ (self, text):
        formattedText = text.replace('.',' ').replace(',',' ').replace('!',' ').replace('?',' ')
        formattedText = formattedText.lower()
        self.fmtText = formattedText

    def freqAll(self):
        wordList = self.fmtText.split(' ')

        wordDict = {}
        for word in set(wordList):
            wordDict[word] = wordList(word)

        return wordDict

     

I get errors on both of these and I can't seem to figure it out after a lot of little adjustments. I suspect the issue in the first part is when I try to assign a value to the newly formatted text but I cannot think of a workable solution. As for the second part, I am at a complete loss - I was wrongfully confident my answer was correct but I received a fail error when I ran it through the classroom's code cell to test it.

1

There are 1 best solutions below

1
On

On the assumption that by 'errors' you mean a TypeError, this is caused because of line 13, wordDict[word] = wordList(word).
wordList is a list, and by using the ()/brackets you're telling Python that you want to call that list as a function. Which it cannot do.

According to your task, you are to instead find the occurrences of words in the list, which you could achieve with the .count() method. This method basically returns the total number of occurrences of an element in a list. (Feel free to read more about it here)

With this modification, (this is assuming you want wordDict to contain a dictionary with the word as the key, and the occurrence as the value) your freqAll function would look something like this:

def freqAll(self):
    wordList = self.fmtText.split()

    wordDict = {}
    for word in set(wordList):
        wordDict[word] = wordList.count(word) # wordList.count(word) returns the number of times the string word appears as an element in wordList

    return wordDict

Although you could also achieve this same task with a class known as collections.Counter, (of course this means you have to import collections) which you can read more about here