forked from vedangj044/News_stock_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarize.py
79 lines (55 loc) · 2.39 KB
/
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
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
from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
import numpy as np
import networkx as nx
class Summarize:
def __init__(self, text, top_n):
"""Get the summary of the text of news scraped."""
self.text = text
self.top_n = top_n
def read_article(self):
article = self.text.split(". ")
sentences = []
for sentence in article:
sentences.append(sentence.replace("[^a-zA-Z]", "").split(" "))
return sentences
@staticmethod
def sentence_similarity(sent1, sent2, stopwords):
sent1 = [w.lower() for w in sent1]
sent2 = [w.lower() for w in sent2]
all_words = list(set(sent1 + sent2))
vector1 = [0] * len(all_words)
vector2 = [0] * len(all_words)
# build the vector for the first sentence
for w in sent1:
if w in stopwords:
continue
vector1[all_words.index(w)] += 1
# build the vector for the second sentence
for w in sent2:
if w in stopwords:
continue
vector2[all_words.index(w)] += 1
return 1 - cosine_distance(vector1, vector2)
def build_similarity_matrix(self, sentences, stop_words):
# Create an empty similarity matrix
similarity_matrix = np.zeros((len(sentences), len(sentences)))
for idx1, _ in enumerate(sentences):
for idx2, _ in enumerate(sentences):
if idx1 == idx2: #ignore if both are same sentences
continue
# print(sentences[idx1], sentences[idx2], stop_words)
similarity_matrix[idx1][idx2] = self.sentence_similarity(sentences[idx1], sentences[idx2], stop_words)
return similarity_matrix
def generate_summary(self):
stop_words = stopwords.words('english')
summarize_text = []
sentences = self.read_article()
sentence_similarity_martix = self.build_similarity_matrix(sentences, stop_words)
sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix)
scores = nx.pagerank(sentence_similarity_graph)
ranked_sentence = sorted(((scores[i],s) for i,s in enumerate(sentences)), reverse=True)
for i in range(self.top_n):
summarize_text.append(" ".join(ranked_sentence[i][1]))
summary = "".join(summarize_text)
return summary