-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Algo Summary.py
265 lines (193 loc) · 7.14 KB
/
2. Algo Summary.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
import pandas as pd
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
from sumy.summarizers.lsa import LsaSummarizer
from sumy.summarizers.luhn import LuhnSummarizer
from collections import Counter
from functools import reduce
import warnings
warnings.filterwarnings("ignore")
DATASET = 'US_Election'
LIMIT = 100 # US Election Dataset
def lin(V, m, lamb):
'''DSDR with linear reconstruction
Parameters
==========
- V : 2d array_like, the candidate data set
- m : int, the number of sentences to be selected
- lamb : float, the trade off parameter
Returns
=======
- L : list, the set of m summary sentences indices
'''
L = []
V = np.array(V)
B = np.dot(V, V.T) / lamb
n = len(V)
for t in range(m):
scores = []
for i in range(n):
score = np.sum(B[:,i] ** 2) / (1. + B[i,i])
scores += [(score, i)]
max_score, max_i = max(scores)
L += [max_i]
B = B - np.outer(B[:,max_i], B[:,max_i]) / (1. + B[max_i,max_i])
return L
def non(V, gamma, eps=1.e-8):
'''DSDR with nonnegative linear reconstruction
Parameters
==========
- V : 2d array_like, the candidate sentence set
- gamma : float, > 0, the trade off parameter
- eps : float, for converge
Returns
=======
- beta : 1d array, the auxiliary variable to control candidate sentences
selection
'''
V = np.array(V)
n = len(V)
A = np.ones((n,n))
beta = np.zeros(n)
VVT = np.dot(V, V.T) # V * V.T
np.seterr(all='ignore')
while True:
_beta = np.copy(beta)
beta = (np.sum(A ** 2, axis=0) / gamma) ** .5
while True:
_A = np.copy(A)
A *= VVT / np.dot(A, VVT + np.diag(beta))
A = np.nan_to_num(A) # nan (zero divide by zero) to zero
if np.sum(A - _A) < eps: break
if np.sum(beta - _beta) < eps: break
return beta
# 1. Load the data
data = pd.read_csv('Data/' + DATASET + '/New_Input.txt', sep = '<\|\|>', header=None, engine = 'python')
data.columns = ['user_id', 'user_name', 'tweet_id', 'type', 'text']
# print(data.shape)
# print(data.head())
# 2. Clean the data
data['clean_text'] = data['text'].str.replace(r"@\S+", "")
data['clean_text'] = data['clean_text'].str.replace(r"http\S+", "")
data['clean_text'] = data['clean_text'].str.replace("[^a-zA-Z]", " ")
# data['clean_text'] = data['text']
# nltk.download('stopwords')
stopwords=nltk.corpus.stopwords.words('english')
def remove_stopwords(text):
clean_text=' '.join([word for word in text.split() if word not in stopwords])
return clean_text
data['clean_text'] = data['clean_text'].apply(lambda text : remove_stopwords(text.lower()))
data['clean_text'] = data['clean_text'].apply(lambda x: x.split())
from nltk.stem.porter import *
stemmer = PorterStemmer()
data['clean_text'] = data['clean_text'].apply(lambda x: [stemmer.stem(i) for i in x])
data['clean_text'] = data['clean_text'].apply(lambda x: ' '.join([w for w in x]))
# print(data.head())
# 3. Convert word to vectors
count_vectorizer = CountVectorizer(stop_words='english')
cv = count_vectorizer.fit_transform(data['clean_text'])
# print(cv.shape)
V = cv.toarray()
# type(V)
# 4. DSDR Linear
L = lin(V, m=LIMIT, lamb=0.1)
# print(L)
columns=count_vectorizer.get_feature_names_out()
file_dsdr_lin = open('Data/' + DATASET + '/DSDRLin.txt', 'w+')
for i in L:
file_dsdr_lin.write(data.iloc[i]['text'] + "\n")
file_dsdr_lin.close()
# 5. DSDR Non Linear
beta = non(V, gamma=0.1)
file_dsdr_non_lin = open('Data/' + DATASET + '/DSDRNonLin.txt', 'w+')
i = 1
for score, v, content in sorted(zip(beta, V, data['text']), reverse=True, key = lambda x: x[0]):
if i > LIMIT :
break
file_dsdr_non_lin.write(content + "\n")
i += 1
file_dsdr_non_lin.close()
# 6. Lex Rank
summarizer_lex = LexRankSummarizer()
s = ''
for ps in data['clean_text'] :
s += ps + '\n\n'
parser = PlaintextParser.from_string(s, Tokenizer("english"))
# Summarize using sumy LexRank
summary_lex = summarizer_lex(parser.document, LIMIT)
file_lex_rank = open('Data/' + DATASET + '/LexRank.txt', 'w+')
for sentence in summary_lex :
row_no = data[data['clean_text'] == str(sentence)].index[0]
file_lex_rank.write(data.iloc[row_no]['text'] + "\n")
file_lex_rank.close()
# 7. LSA
summarizer_lsa = LsaSummarizer()
# Summarize using sumy LSA
summary_lsa = summarizer_lsa(parser.document, LIMIT)
file_lsa = open('Data/' + DATASET + '/LSA.txt', 'w+')
for sentence in summary_lsa :
row_no = data[data['clean_text'] == str(sentence)].index[0]
file_lsa.write(data.iloc[row_no]['text'] + "\n")
file_lsa.close()
# 8. LUHN
summarizer_luhn = LuhnSummarizer()
summary_luhn = summarizer_luhn(parser.document,LIMIT)
file_luhn = open('Data/' + DATASET + '/LUHN.txt', 'w+')
for sentence in summary_luhn :
row_no = data[data['clean_text'] == str(sentence)].index[0]
file_luhn.write(data.iloc[row_no]['text'] + "\n")
file_luhn.close()
# 9. SUM BASIC
def count_words(texts):
"""
Counts the words in the given texts, ignoring puncuation and the like.
@param texts - Texts (as a single string or list of strings)
@return Word count of texts
"""
if type(texts) is list:
return len(texts)
return len(texts.split())
def sumbasic_summarize(limit, data, update):
data_list = data.apply(lambda x: x.split())
# Counter for all words.
cnts = Counter()
for sent in data_list :
cnts += Counter(sent)
# Number of tokens.
N = float(sum(cnts.values()))
# Unigram probabilities.
probs = {w: cnt / N for w, cnt in cnts.items()}
# print(probs)
# List of all sentences in all documents.
sentences = data.tolist()
# print(len(sentences))
summary = []
# Add sentences to the summary until there are no more sentences or word
# limit is exceeded.
while len(sentences) > 0 and len(summary) < limit:
# Track the max probability of a sentence with corresponding sentence.
max_prob, max_sent = 0.0, None
for i, sent in enumerate(sentences):
prob = 1
for w in data_list[i] :
prob *= probs[w]
if max_prob < prob:
max_prob, max_sent = prob, sent
if len(max_sent) > 0 :
summary.append(max_sent)
sentences.remove(max_sent)
if update:
# Apply the update for non-redundancy.
for w in data_list[i]:
probs[w] = probs[w] ** 2
return summary
summary_sumbasic = sumbasic_summarize(LIMIT, data['clean_text'],update=False)
file_sum_basic = open('Data/' + DATASET + '/SumBasic.txt', 'w+')
for sentence in summary_sumbasic :
row_no = data[data['clean_text'] == str(sentence)].index[0]
file_sum_basic.write(data.iloc[row_no]['text'] + "\n")
file_sum_basic.close()