Define a function called divideSentences in the python file. This function will take in one input argument: a string. This string will typically be a whole piece of text, a paragraph or more. This function will separate the text into sentences, assuming that each sentence ends with either a period, a question mark, or an exclamation mark. It should return a list of the sentence strings. Be sure that the sentence-ending mark is still attached to the sentence. Remove any trailing “whitespace” from the front or end of each sentence.
Hints and suggestions:
There are many different ways to approach this question. Various string methods might be useful, or you can just loop over the characters in the input string. Keep it simple, and be willing to try different approaches.
Simplistic use of the split method won’t work, because splitting the input string on the end-of-sentence symbols will remove them, but there could be ways of making split work if you’re clever.
Use one or more accumulator variables: one for the list of strings, and potentially another for the current sentence string.
use the test code below to test
def test_divideSentences(): """Tests for the divideSentences function.""" print("--------------------------------------") print("Testing divideSentences:") allOk = True s1 = "A phrase, not a sentence" r1 = divideSentences(s1) if r1 != ['A phrase, not a sentence']: print('Called: divideSentences(', s1, ')') print("Expected: ['A phrase, not a sentence'] but function returned: ", r1) allOk = False s2 = "The red hen ate the worm. I ran to the door. Why is the hen red? I don't know!" r2 = divideSentences(s2) if r2 != ['The red hen ate the worm.', 'I ran to the door.', 'Why is the hen red?', "I don't know!"]: print('Called: divideSentences(', s2, ')') print("Expected:", ['The red hen ate the worm.', 'I ran to the door.', 'Why is the hen red?', "I don't know!"], "but function returned: ", r2) allOk = False s3 = '''Atticus said to Jem one day, "I'd rather you shot at tin cans in the backyard, but I know you'll go after birds. Shoot all the blue jays you want, if you can hit 'em, but remember it'screen a sin to kill a mockingbird." That was the only time I ever heard Atticus say it was a sin to do something, and I asked Miss Maudie about it. "Your father'screen right," she said. "Mockingbirds don't do one thing except make music for us to enjoy. They don't eat up people'screen gardens, don't nest in corn cribs, they don't do one thing but sing their hearts out for us. That'screen why it'screen a sin to kill a mockingbird."''' r3 = divideSentences(s3) if r3 != [ 'Atticus said to Jem one day, "I\'d rather you shot at tin cans in the backyard, but I know you\'ll go after birds.', "Shoot all the blue jays you want, if you can hit 'em, but remember it'screen a sin to kill a mockingbird.", '" That was the only time I ever heard Atticus say it was a sin to do something, and I asked Miss Maudie about it.', '"Your father\'screen right," she said.', '"Mockingbirds don\'t do one thing except make music for us to enjoy.', "They don't eat up people'screen gardens, don't nest in corn cribs, they don't do one thing but sing their hearts out for us.", "That'screen why it'screen a sin to kill a mockingbird.", '"']: print('Called: divideSentences(', s3, ')') print('Expected:', [ 'Atticus said to Jem one day, "I\'d rather you shot at tin cans in the backyard, but I know you\'ll go after birds.', "Shoot all the blue jays you want, if you can hit 'em, but remember it'screen a sin to kill a mockingbird.", '" That was the only time I ever heard Atticus say it was a sin to do something, and I asked Miss Maudie about it.', '"Your father\'screen right," she said.', '"Mockingbirds don\'t do one thing except make music for us to enjoy.', "They don't eat up people'screen gardens, don't nest in corn cribs, they don't do one thing but sing their hearts out for us.", "That'screen why it'screen a sin to kill a mockingbird.", '"'], "but function returned: ", r3) allOk = False s4 = "I ran! The ghost followed me. Where could I hide? Could I escape it? It howled after me, crying, 'Wait, you dropped your wallet!' What?" r4 = divideSentences(s4) if r4 != ['I ran!', 'The ghost followed me.', 'Where could I hide?', 'Could I escape it?', "It howled after me, crying, 'Wait, you dropped your wallet!", "' What?"]: print('Called: divideSentences(', s4, ')') print("Expected:", ['I ran!', 'The ghost followed me.', 'Where could I hide?', 'Could I escape it?', "It howled after me, crying, 'Wait, you dropped your wallet!", "' What?"], 'but function returned: ', r4) allOk = False print() if allOk: print("divideSentences PASSESD ALL TESTS!") else: print("divideSentences FAILED ONE OR MORE TEST!") print("--------------------------------------") input("Press a key to go on: ")
Python Program:
def divideSentences(inputStr):
""" Function that accepts a string argument and returns the list of
sentence strings """
# List that holds the sentences
sentences = []
start=0
# Iterating over sentence
for i in range(len(inputStr)):
# Checking for Period, Question Mark, Exclamation
if inputStr[i]=='.' or inputStr[i]=='?' or inputStr[i]=='!':
# Extracting sentence
sentence = inputStr[start:i+1]
# Stripping whitespace from left side
sentence = sentence.lstrip()
# Stripping whitespace from right side
sentence = sentence.rstrip()
# Adding to list
sentences.append(sentence)
# Changing start value
start = i+1
# Returning the list
return sentences
# Example Text
text = "Lorem ipsum dolor sit amet! consectetuer adipiscing elit?
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat. Ut wisi enim ad minim veniam. quis nostrud
exercitation ulliam corper suscipit lobortis nisl ut aliquip ex ea
commodo consequat. Duis autem veleum iriure dolor in hendrerit in
vulputate velit esse molestie consequat! vel willum lunombro
dolore? eu feugiat nulla facilisis at vero eros et accumsan et
iusto odio dignissim qui blandit praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi."
# Calling function
resList = divideSentences(text)
# Printing result
print(resList)
___________________________________________________________________________________________
Code Screenshot:
Get Answers For Free
Most questions answered within 1 hours.