-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsummarizer.py
79 lines (58 loc) · 2.29 KB
/
summarizer.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 typing import List
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem.snowball import SnowballStemmer
nltk.download('punkt')
nltk.download('stopwords')
SYMBOLS = "!\"#$%&()*+-./:;<=>?@[\]^_`{|}~\n,"
stop_words = set(stopwords.words("english"))
stemmer = SnowballStemmer("english")
def _pre_process(doc: str):
# Convert to all lowercase
doc = doc.lower()
# Remove apostrophes
doc = doc.replace("'",'')
# Remove punctuations
for punctuation in SYMBOLS:
doc = doc.replace(punctuation,' ')
tokens = word_tokenize(doc)
# Removing stop words
tokens = [token for token in tokens if token not in stop_words]
# Stemming
stemmed_tokens = [stemmer.stem(token) for token in tokens]
return " ".join(stemmed_tokens)
def _select_top_sentences(vt: np.ndarray) -> List[int]:
'''
Select top n documents based on "cross method" as defined in
"Ozsoy, Makbule & Alpaslan, Ferda & Cicekli, Ilyas. (2011).
Text summarization using Latent Semantic Analysis. J.
Information Science. 37. 405-417. 10.1177/0165551511408848."
"https://www.researchgate.net/publication/220195824_Text_summarization_using_Latent_Semantic_Analysis"
'''
# Number of sentences to select cannot be more than the number of sentences available
n = min(5,vt.shape[1])
for row in vt:
row_avg = np.mean(row)
row[row <= row_avg] = 0
length_scores: np.ndarray = vt.sum(axis=0)
selected_sent_indices = []
for _ in range(n):
sent_index = np.argmax(length_scores)
selected_sent_indices.append(sent_index)
length_scores[sent_index] = float('-inf')
return selected_sent_indices
def summarize(text: str) -> str:
corpus = sent_tokenize(text)
vectorizer = TfidfVectorizer(preprocessor=_pre_process)
tf_idf = vectorizer.fit_transform(corpus)
n_components = min(4,len(corpus)-1)
svd = TruncatedSVD(n_components)
svd.fit_transform(tf_idf.transpose())
vt = svd.components_
selected_sentences = _select_top_sentences(vt)
summary = [corpus[i] for i in sorted(selected_sentences)]
return ' '.join(summary)