-
Notifications
You must be signed in to change notification settings - Fork 5
/
summarize.py
39 lines (27 loc) · 968 Bytes
/
summarize.py
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
# for scraping the webpage
from newspaper import Article
# for removing square brackets
import re
# Gensim summarizer
from gensim.summarization.summarizer import summarize
# For extracting the keywords
from gensim.summarization import keywords
# downloads the article and parses the html, uses lxml parser
def downloadwebpage(url):
# downloads the whole webpage
article = Article(url)
article.download()
# parses the downloaded html
article.parse()
text = article.text
return text
def sum_it_up(url):
# url = 'https://en.wikipedia.org/wiki/Elon_Musk'
content = downloadwebpage(url)
# remove the reference numbers
re.sub(r'\[.+\]', '', content)
# finds a list of 10 important keywords, usses lemmetatization instead of stemming
k = keywords(content, words=10, lemmatize=True).split('\n')
kwords = ', '.join(k)
# computes summary and reduces size by 20%
return(summarize(content, 0.2), kwords)