import spacy from spacy.lang.en.stop_words import STOP_WORDS from string import punctuation from collections import Counter from heapq import nlargest nlp = spacy.load("en_core_web_sm") doc1 = """Meditation is a practice where an individual uses a technique – such as mindfulness, or focusing the mind on a particular object, thought, or activity – to train attention and awareness, and achieve a mentally clear and emotionally calm and stable state. Research shows that meditation can improve attention, increase feelings of calm, and reduce stress and anxiety. It has been practiced for thousands of years in numerous religious traditions and beliefs, often as part of the path towards spiritual enlightenment. In modern times, it has been adopted as a means to promote relaxation and improve overall well-being. Meditation can take many forms, including guided meditations, mindfulness meditation, and transcendental meditation. Each type of meditation has its own unique techniques and benefits. Regular practice can lead to greater emotional health, improved concentration, and a more positive outlook on life.""" docx = nlp(doc1) stopwords = list(STOP_WORDS) word_frequencies = {} for word in docx: if word.text.lower() not in stopwords and word.text not in punctuation: if word.text.lower() not in word_frequencies: word_frequencies[word.text.lower()] = 1 else: word_frequencies[word.text.lower()] += 1 max_freq = max(word_frequencies.values()) for word in word_frequencies.keys(): word_frequencies[word] = word_frequencies[word] / max_freq sentence_scores = {} for sent in docx.sents: for word in sent: if word.text.lower() in word_frequencies.keys(): if sent not in sentence_scores: sentence_scores[sent] = word_frequencies[word.text.lower()] else: sentence_scores[sent] += word_frequencies[word.text.lower()] select_length = int(len(sentence_scores) * 0.3) summary = nlargest(select_length, sentence_scores, key=sentence_scores.get) final_summary = [word.text for word in summary] summary = ' '.join(final_summary) print("Original Text Length:", len(doc1.split())) print("Summary Length:", len(summary.split())) print("\nSummary:\n", summary)