生成器 def summarizer(text): '''This function takes in the text and returns its summary using text analysis''' # Tokenize Text words = word_tokenize(text) # Generate word frequencies freq = {} for word in words: if word not in freq.keys(): freq[word] = 1 else: freq[word] += 1 # Generate Sentence Score based on the frequencies sentences = sent_tokenize(text) sent_score = {} for sentence in sentences: words_in_sentence = word_tokenize(sentence) score = 0 for word in words_in_sentence: if word in freq.keys(): score += freq[word] sent_score[sentence] = score # Extract Top N Sentences based on the score top_sentences = sorted(sent_score.items(), key=lambda x: x[1], reverse=True)[:3] # Generate Summary summary = '' for sentence in top_sentences: summary += sentence[0] + ' ' return summary if __name__=='__main__': text = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.''' summary = summarizer(text) print(summary)