Question

Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated...

Python 3

Implement the function condense, which takes a "passage" (a string containing multiple sentences separated by the '\n' character), and returns a condensed version of that passage based on the following logic:

  • the frequency with which each word appears in the entire passage is computed
  • a score for each sentence in the passage is computed by summing the frequency of each word in the sentence
  • the sentences with the top 3 scores (we assume there are no ties) are combined together and returned as the condensed version of the passage

For example, consider the following passage:

I love cake
cake is the best
cake cake cake
vanilla cake is good
but so is chocolate
but the best cake is pure love

The word frequencies of each word are:

{'I': 1,
'love': 2,
'cake': 7,
'is': 4,
'the': 2,
'best': 2,
'vanilla': 1,
'good': 1,
'but': 2,
'so': 1,
'chocolate': 1,
'pure': 1}

Based on these frequences, here is how we compute scores for two sample sentences:

  • I love cake = 1 + 2 + 7 = 10
  • cake cake cake = 7 + 7 + 7 = 21

The top three sentences for this passage (and their scores) are:

  • cake cake cake (21)
  • but the best cake is pure love (20)
  • cake is the best (15)

To break a passage into sentences, use passage.split('\n'), and to break some string s into words, use s.split(). To combine strings s1, s2, s3 into a passage, do '\n'.join(s1, s2, s3)

def condense(passage):
# YOUR CODE HERE
pass

Homework Answers

Answer #1

Here is the Python code to the given question.

Detailed comments are added to the code and sample output is added at the end.

Code:

def condense(passage):
    
    sentences=passage.split('\n')    #stores the list of all sentences
    
    wordsDict={}    #dictionary to store all words and their frequencies
    
    for sentence in sentences:     #for each sentence is all sentences
        
        words=sentence.split()    #split sentence into words
        
        for word in words:     #for each word in words
            
            if word in wordsDict.keys():   #if the word already exists in dictionary
                
                wordsDict[word]+=1      #increment the value of present word by 1
            
            else:
                
                wordsDict[word]=1       #insert the present word with value 1 into dictionary
            
    sentenceScore={}      #dictionary to store sentences and their respective score
    
    for sentence in sentences:     #for each sentence in sentences
        
        score=0       #initialize score of present sentence to 0
        
        words=sentence.split()     #split sentence into words
        
        for word in words:      #for each word in words
            
            score+=wordsDict[word]      #increment the value of score by the word frequency of current word
        
        sentenceScore[sentence]=score    #insert this sentence with its score into dictionary
    
    
    finalPassage=[0,0,0]    #list to hold sentences in final passage
    
    words=list(sentenceScore.values())    #converts all values in sentenceScore into list
    
    words.sort(reverse=True)   #sorts the list in descending order
    
    for sentence in sentenceScore.keys():     #for each sentence in keys of sentenceScore
        
        if sentenceScore[sentence]==words[0]:     #if the sentence score is equal to highest score of the passage
            
            finalPassage[0]=sentence+"\n"     #insert the sentence at starting position of the list
        
        if sentenceScore[sentence]==words[1]:    #if the sentence score is second highest in passage
            
            finalPassage[1]=sentence+"\n"     #insert the sentence at second position of the list
            
        if sentenceScore[sentence]==words[2]:    #if the sentence score is third highest in passage
            
            finalPassage[2]=sentence+"\n"     #insert the sentence at third position of the list
    
    
    finalPassageString=""    #string to hold sentences with highest scores
    
    return finalPassageString.join(finalPassage)     #converts list to string and returns it



def main():
    
    print(condense("I love cake\ncake is the best\ncake cake cake\nvanilla cake is good\nbut so is chocolate\nbut the best cake is pure love")) #prints three sentences with highest frequency
    
    
    
if __name__=="__main__":
    
    main()    #calls main
    
    
    
    
    
    
    
    

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions