Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python programming question. Thank you! Create a new class called Sentence. a) T

ID: 3676472 • Letter: P

Question

Python programming question. Thank you!

Create a new class called Sentence.

a) The constructor should accept a single parameter that is a string. Create an instance variable that stores the sentence as a string. Assume the sentence has no punctuation.

b) Write accessor method getSentence: Return the sentence as a string.

c) Write accessor method getWords: Return the list of words in the sentence.

d) Write accessor method getNumWords: Return the number of words in the sentence.

e) Write a mutator method for the Sentence class that allows you to capitalize all the words in a sentence.

f) Write a mutator method for the Sentence class that allows you to add a punctuation mark to the end of the sentence.

g) Add a str method to the Sentence class

h) Implement the getitem method for the Sentence class. This method should allow you to use the index operator notation to access a word in the sentence.

i) Implement the len method so that you can use the len operator.

j) Implement the contains method so that you can ask if a word is in a sentence.

k) Implement the getslice method to allow you to retrieve portions of the sentence. The getslice method should return a Sentence consisting of the words indicated by the slice. For example, if the original sentence was a = Sentence ("the quick brown fox"), then a[1:3] should return the sentence consisting of the words "quick brown".

l) Implement the add method to allow you to concatenate two sentences together. Make sure your add method returns a new instance of Sentence.

m) Implement the frequencyTable method that determine the word frequency of a sentence using a dictionary and print a frequency table

n) Write a function that tests all the above functions

Thank you!

Explanation / Answer


class Sentence:
    def __init__(self, istring) -> :
        self.string = istring

    def getSentence(self) -> :
        return self.string

    def getWords(self) -> :
        return self.getSentence().split()

    def getLength(self) -> :
        return len(self.getSentence())

    def getNumWords(self) -> :
        return len(self.getWords())

    def capitalizeAllWords(self) -> :
        newWords = []
        for word in self.getWords():
            newWords.append(word[0].upper() + word[1:])
        self.string = ' '.join(newWords)

    def addPunctuation(self, punct) -> :
        self.string += punct

    def __str__(self) -> :
        return self.getSentence()

    def __getitem__(self, item) -> :
        return self.getWords()[item]

    def __len__(self) -> :
        return self.getNumWords()

    def __contains__(self, item) -> :
        return item in self.getWords()

    def __add__(self, other) -> :
        return Sentence(str(self) + " " + str(other))
      
      
      
       Test.py

string = "the quick brown fox"
for sent in [Sentence(string), SentenceList(string)]:
    print(' %s' % type(sent))
    print('%s (%d chars)' % (sent.getSentence(), sent.getLength()))
    print('%s (%d words)' % (sent.getWords(), sent.getNumWords()))
    sent.capitalizeAllWords()
    sent.addPunctuation('?')
    print('capitalized with punctuation: %s' % str(sent))
    print('getitem 2: %s' % sent[2])
    print('len: %s' % len(sent))
    print('contains "Quick"? %s "Lazy"? %s' % ('Quick' in sent, 'Lazy' in sent))
    newsent = Sentence('jumps over the lazy brown dog')
    print('add "%s": %s' % (newsent, sent + newsent))