-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummary.py
More file actions
38 lines (26 loc) · 922 Bytes
/
Copy pathsummary.py
File metadata and controls
38 lines (26 loc) · 922 Bytes
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
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
from collections import Counter, defaultdict
def initialize():
nltk.download('punkt')
nltk.download('stopwords')
def summarize(text: str) -> str:
stopWords = set(stopwords.words('english'))
words = word_tokenize(text)
counter = Counter(words)
for word in stopWords:
if word in counter:
del counter[word]
sentences = sent_tokenize(text)
sentVal = defaultdict(int)
for sentence in sentences:
for word in sentence.lower():
sentVal[sentence] += counter[word] if word in counter else 0
sum_values = sum(sentVal.values())
avg = sum_values // len(sentVal)
summary = ''
for sentence in sentences:
if (sentence in sentVal) and (sentVal[sentence] > (1.2 * avg)):
summary += " " + sentence
return summary