0

I am pretty new to python, and I am trying to inherit a superclass.

The superclass looks like:

class LanguageModel:
    # Initialize and train the model (ie, estimate the model's underlying probability
    # distribution from the training corpus)
    def __init__(self, corpus):
        print("""Your task is to implement three kinds of n-gram language models:


    #enddef

    # Generate a sentence by drawing words according to the 
    # model's probability distribution
    # Note: think about how to set the length of the sentence 
    # in a principled way
    def generateSentence(self):
        print("Implement the generateSentence method in each subclass")
        return "mary had a little lamb ."
    #emddef

And here is the subclass

class UnigramModel(LanguageModel):
    def __init__(self, corpus):
        print("Subtask: implement the unsmoothed unigram language model")
    #endddef

    def generateSentence(self):
        # Gets the total number of words in the corpus
        wordCount = 0
        for sentence in self.corpus:
            for word in sentence:
                wordCount += 1
        print(wordCount)

I first tried to get the total wordcount in the corpus by doing the above, but it gives me an error that says "UnigramModel' object has no attribute 'corpus' when I try to call that function.

1
  • 1
    after def __init__(self, corpus): add self.corpus = corpus. Commented Sep 27, 2017 at 4:24

1 Answer 1

1

You have to declare corpus like mentioned above. Or you can use getter-setter methods for private attribute.

def init(self,corpus): self.corpus = corpus @property def corpus(self): return self.__corpus @setter def corpus(self, value): self.__corpus = value

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.