forked from everydaycodings/Text-Summarization-using-NLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
159 lines (108 loc) · 4.43 KB
/
helper.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from random import random
from spacy.lang.en.stop_words import STOP_WORDS
import en_core_web_sm
from string import punctuation
from heapq import nlargest
import spacy_streamlit
import requests
import json
from bs4 import BeautifulSoup
import configparser
import streamlit as st
import random
nlp= en_core_web_sm.load()
#nlp= spacy.load("en_core_web_sm")
stopwords = list(STOP_WORDS)
punctuation = punctuation + "\n"
config = configparser.ConfigParser()
config.read("config.ini")
news_api_key = config["API"]["news_api"]
def spacy_rander(summary, text=None):
summ = nlp(summary)
if text == "Yes":
rend = spacy_streamlit.visualize_ner(summ, labels=nlp.get_pipe("ner").labels, title="Full Article Visualization", show_table=False, key=random.randint(0, 100))
else:
rend = spacy_streamlit.visualize_ner(summ, labels=nlp.get_pipe("ner").labels, title="Summary Visualization", show_table=False, key=random.randint(0, 100))
return rend
def word_frequency(doc):
word_frequencies = {}
for word in doc:
if word.text.lower() not in stopwords:
if word.text.lower() not in punctuation:
if word.text not in word_frequencies.keys():
word_frequencies[word.text] = 1
else:
word_frequencies[word.text] += 1
return word_frequencies
def sentence_score(sentence_tokens, word_frequencies):
sentence_score = {}
for sent in sentence_tokens:
for word in sent:
if word.text.lower() in word_frequencies.keys():
if sent not in sentence_score.keys():
sentence_score[sent] = word_frequencies[word.text.lower()]
else:
sentence_score[sent] += word_frequencies[word.text.lower()]
return sentence_score
@st.cache(allow_output_mutation=False)
def fetch_news_links(query):
link_list = []
title_list = []
thumbnail_list = []
if query == "":
reqUrl = "https://newsapi.org/v2/everything?sources=bbc-news&q=india&language=en&apiKey={}".format(news_api_key)
else:
reqUrl = "https://newsapi.org/v2/everything?sources=bbc-news&q={}&language=en&apiKey={}".format(query, news_api_key)
headersList = {
"Accept": "*/*",
"User-Agent": "Thunder Client (https://www.thunderclient.com)"
}
payload = ""
response = requests.request("GET", reqUrl, data=payload, headers=headersList).text
response = json.loads(response)
tw = 0
for i in range(len(response["articles"])):
if tw ==10:
pass
else:
if "/news/" in response["articles"][i]["url"] and "stories" not in response["articles"][i]["url"]:
link_list.append(response["articles"][i]["url"])
title_list.append(response["articles"][i]["title"])
thumbnail_list.append(response["articles"][i]["urlToImage"])
else:
pass
tw += 1
return link_list, title_list, thumbnail_list
@st.cache(allow_output_mutation=False)
def fetch_news(link_list):
news = []
news_list = []
for i in range(len(link_list)):
news_reqUrl = link_list[i]
headersList = {
"Accept": "*/*",
"User-Agent": "Thunder Client (https://www.thunderclient.com)"
}
payload = ""
news_response = requests.request("GET", news_reqUrl, data=payload, headers=headersList)
soup = BeautifulSoup(news_response.content, features="html.parser")
soup.findAll("p", {"class":"ssrcss-1q0x1qg-Paragraph eq5iqo00"})
soup.findAll("div", {"data-component":"text-block"})
for para in soup.findAll("div", {"data-component":"text-block"}):
news.append(para.find("p").getText())
joinnews = " ".join(news)
news_list.append(joinnews)
news.clear()
return news_list
def get_summary(text):
doc = nlp(text)
word_frequencies = word_frequency(doc)
for word in word_frequencies.keys():
word_frequencies[word] = word_frequencies[word] / max(word_frequencies.values())
sentence_tokens = [sent for sent in doc.sents]
sentence_scores = sentence_score(sentence_tokens, word_frequencies)
select_length = int(len(sentence_tokens)*0.10)
summary = nlargest(select_length, sentence_scores, key=sentence_scores.get)
summary = [word.text for word in summary]
summary = " ".join(summary)
return summary