-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnltk_summarization.py
More file actions
39 lines (31 loc) · 1.27 KB
/
Copy pathnltk_summarization.py
File metadata and controls
39 lines (31 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import nltk
import nltk.corpus
from nltk.tokenize import word_tokenize , sent_tokenize
import heapq
nltk.download('stopwords')
nltk.download('punkt')
def nltk_summarizer(raw_text):
stopwords = nltk.corpus.stopwords.words('english')
word_frequency = {}
for word in nltk.word_tokenize(raw_text):
if word not in stopwords:
if word not in word_frequency.keys():
word_frequency[word] = 1
else :
word_frequency[word] += 1
maximum_frequency = max(word_frequency.values())
for word in word_frequency.keys():
word_frequency[word] = (word_frequency[word]/maximum_frequency)
sentence_list = nltk.sent_tokenize(raw_text)
sentence_scores = {}
for sent in sentence_list:
for word in nltk.word_tokenize(sent.lower()):
if word in word_frequency.keys():
if len(sent.split(' ')) < 30 :
if sent not in sentence_scores.keys():
sentence_scores[sent] = word_frequency[word]
else :
sentence_scores[sent] += word_frequency[word]
summary_sentences = heapq.nlargest(7,sentence_scores, key=sentence_scores.get)
summary = ' '.join(summary_sentences)
return summary