-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.py
365 lines (274 loc) · 9.88 KB
/
main_test.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# In[]:
from crawling import crawl as cr
import utilfunc as ut
# -*- coding: utf-8 -*-
import numpy as np
from konlpy.tag import Okt
okt = Okt()
from konlpy.tag import Mecab
mecab = Mecab()
import pandas as pd
# n = input("typing the number of tsv file: ")
n = "0"
dataset = pd.read_csv("/Users/bobopack/Downloads/CS489-Team-14-repository-main 3/crawling_output/crawling_output_{0}.tsv".format(n)
, delimiter='\t', names = ['제목/댓글번호','기사작성시간/댓글본문','기사본문/좋아요','none/싫어요','none/답글수'])
datatable = dataset.values.tolist()
title = datatable[0][0]
article = datatable[0][2]
def article_proc(article):
art = article.split("▶")
if len(art) > 1:
icle = art[:-3]
s = ""
for t in icle:
s += t
article = s
return article
article = article_proc(article)
comments = []
replys = []
for i in range(1,len(datatable)):
line = datatable[i]
if datatable[i-1][0] == datatable[i][0]: #대댓글일때 (라인넘버, 대댓글본문)
replys.append(line[:2])
else:
comments.append(line[:2]+line[4:]) #댓글일때 (라인넘버, 댓글본문, 답글갯수)
# In[]:
#okt
# def get_morphs(text):
# tags = okt.pos(text, norm='True', stem='True')
# l = []
# for words in tags:
# l.append(words[0])
# return l
# def get_nouns(text):
# key = ["Noun"]
# tags = okt.pos(text, norm='True', stem='True')
# l = []
# for words in tags:
# if words[1] in key:
# l.append(words[0])
# return l
# def num_pos(text):
# tags = okt.pos(text, norm='True', stem='True')
# return len(tags)
# In[]:
#mecab
def get_morphs(text):
tags = mecab.pos(text)
l = []
for words in tags:
l.append(words[0])
return l
def get_nouns(text):
# 일반명사,고유명사
key = ["NNG", "NNP"]
tags = mecab.pos(text)
l = []
for words in tags:
if words[1] in key:
l.append(words[0])
return l
def num_pos(text):
tags = mecab.pos(text)
return len(tags)
# In[]:
# 명사개수
def num_naive(text):
nouns = get_nouns(text)
return len(nouns)
# 명사종류개수
def num_only(text):
nouns = get_nouns(text)
return len(set(nouns))
# 본문포함된 명사개수
def num_naive_article(article, text):
article_nouns = get_nouns(article)
nouns = get_nouns(text)
real_nouns = []
for noun in nouns:
if noun in article_nouns:
real_nouns.append(noun)
return len(real_nouns)
# 본문포함된 명사종류개수
def num_only_article(article, text):
article_nouns = set(get_nouns(article))
nouns = set(get_nouns(text))
inter = nouns.intersection(article_nouns)
return len(inter)
# processed comments and replys
proc_comments = []
# proc_replys = []
for comment in comments:
content = comment[1]
processing = comment + [num_pos(content),
num_naive(content),
num_only(content),
num_naive_article(article, content),
num_only_article(article, content)]
proc_comments.append(processing)
# Jaccard Similarity
def jac_sim(article, text):
article = set(get_morphs(article))
comment = set(get_morphs(text))
inter = comment.intersection(article)
uni = comment.union(article)
return len(inter)/len(uni)
def nonset_jac_sim(article, text):
article = get_morphs(article)
comments = get_morphs(text)
inter = []
for comment in comments:
if comment in article:
inter.append(comment)
uni = set(comments).union(set(article))
return len(inter)/len(uni)
def noun_jac_sim(article, text):
article_nouns = set(get_nouns(article))
nouns = set(get_nouns(text))
inter = nouns.intersection(article_nouns)
uni = nouns.union(article_nouns)
return len(inter)/len(uni)
def nonset_noun_jac_sim(article, text):
article_nouns = get_nouns(article)
nouns = get_nouns(text)
real_nouns = []
for noun in nouns:
if noun in article_nouns:
real_nouns.append(noun)
uni = set(nouns).union(set(article_nouns))
return len(real_nouns)/len(uni)
# revised rated comments and replys
rated_comments = []
# rated_replys = []
for comment in comments:
content = comment[1]
# n = num_pos(content)
rating = comment + [num_pos(content),
num_naive_article(article, content) / (num_naive(content)+1),
num_only_article(article, content) / (num_only(content)+1),
jac_sim(article, content),
nonset_jac_sim(article, content),
noun_jac_sim(article, content),
nonset_noun_jac_sim(article, content)]
rated_comments.append(rating)
# part for sklearn, similarity
from sklearn.feature_extraction.text import TfidfVectorizer
def tfid_vectorize(article, comment):
sent = (article, comment)
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(sent) #문장 벡터화 진행
#idf = tfidf_vectorizer.idf_
#print(dict(zip(tfidf_vectorizer.get_feature_names(), idf)))
return tfidf_matrix
#Cosine_similarity
from sklearn.metrics.pairwise import cosine_similarity
def cos_sim(matrix):
sim = cosine_similarity(matrix[0:1], matrix[1:2]) #첫번째와 두번째 문장 비교
#array([[0.113]])
return sim[0][0]
#Euclidean Distance or L2-Distance
from sklearn.metrics.pairwise import euclidean_distances
def euc_dis(matrix):
sim = euclidean_distances(matrix[0:1], matrix[1:2])
#array([[1.331]])
return sim[0][0]
#L1-Normalization
def l1_normalize(v):
norm = np.sum(v)
return v / norm
def nor_euc_dis(matrix):
tfidf_norm_l1 = l1_normalize(matrix)
sim = euclidean_distances(tfidf_norm_l1[0:1], tfidf_norm_l1[1:2])
# array([[0.212]])
return sim[0][0]
#Manhattan Similarity or L1-Distance
from sklearn.metrics.pairwise import manhattan_distances
def manh_dis(matrix):
sim = manhattan_distances(matrix[0:1], matrix[1:2])
#array([[0.857]])
return sim[0][0]
def nor_manh_dis(matrix):
tfidf_norm_l1 = l1_normalize(matrix)
sim = manhattan_distances(tfidf_norm_l1[0:1], tfidf_norm_l1[1:2])
#array([[0.857]])
return sim[0][0]
# similarity commnets
similarity_comments = []
for comment in comments:
content = comment[1]
tfidf_matrix = tfid_vectorize(article, content)
#tfidf_matrix = tfid_vectorize(summary, content)
similarity = comment + [cos_sim(tfidf_matrix), euc_dis(tfidf_matrix), nor_euc_dis(tfidf_matrix), manh_dis(tfidf_matrix), nor_manh_dis(tfidf_matrix)]
similarity_comments.append(similarity)
df0 = pd.DataFrame(comments, columns=['댓글번호','댓글본문','답글수'])
df1 = pd.DataFrame(proc_comments, columns = ['댓글번호','댓글내용','답글수','pos길이',
'명사개수','명사종류개수',
'본문명사개수','본문명사종류개수'])
new_df1 = df1.sort_values(by='본문명사개수', ascending=False)
df2 = pd.DataFrame(rated_comments, columns = ['댓글번호','댓글내용','답글수','pos길이',
'본문/전체명사','본문/전체명사종류',
'자카드','nonset자카드',
'자카드명사','nonset자카드명사'])
new_df2 = df2.sort_values(by='본문/전체명사', ascending=False)
df3 = pd.DataFrame(similarity_comments, columns = ['댓글번호','댓글내용','답글수',
'코사인유사도',
'유클리디언','norm유클',
'맨해튼','norm맨해'])
new_df3 = df3.sort_values(by='코사인유사도', ascending=False)
# In[]:
################
### for TEST ###
################
like = dataset['기사본문/좋아요'].tolist()[1:]
dislike = dataset['none/싫어요'].tolist()[1:]
likes = [float(ke) for ke in like]
dislikes = [float(dke) for dke in dislike]
comment_only = df0['댓글본문'].tolist()
num = [n+1 for n in range(len(comment_only))]
replyN_only = df0['답글수'].tolist()
jaccard_only = df2['nonset자카드'].tolist()
cosine_only = df3['코사인유사도'].tolist()
euclidean_only = df3['norm유클'].tolist()
testList = df1['pos길이'].tolist()
testLog = cr.log_scale(testList, 2)
testNorm = cr.norm_data(testList)
testRank = cr.norm_rank(testList, True)
textRankN_only = ut.com_to_trscore(article, comment_only)
# In[]:
# reply list
n1_list = cr.norm_data(cr.log_scale(replyN_only, 2))
def list_calculate(l1, l2, k):
if l2 == []:
l = (np.array(l1)*k).tolist()
else:
if k == 0:
l = l1
elif k == 1:
ldf = pd.DataFrame([l1]+[l2])
ldf = ldf.T
ldf[2] = ldf[0] + ldf[1]
l =ldf[2].tolist()
elif k == -1:
ldf = pd.DataFrame([l1]+[l2])
ldf = ldf.T
ldf[2] = ldf[0] - ldf[1]
l =ldf[2].tolist()
elif k == 2:
ldf = pd.DataFrame([l1]+[l2])
ldf = ldf.T
ldf[2] = (ldf[0] * ldf[1])**(1/2)
l =ldf[2].tolist()
elif k == -2:
ldf = pd.DataFrame([l1]+[l2])
ldf = ldf.T
ldf[2] = (ldf[0] / ldf[1])**(1/2)
l =ldf[2].tolist()
return l
n2_list = cr.norm_data(cr.log_scale(list_calculate(likes,dislikes,1), 2))
n3_list = cr.norm_data(list_calculate(textRankN_only, jaccard_only, -2))
n4_list = list_calculate(cr.norm_rank(cosine_only, True), cr.norm_rank(euclidean_only, False), 2)
final = [num,comment_only,n1_list,n3_list,n3_list]
# print("a: {0}, b: {1}, c: {2}, d: {3}, e: {4}, f: {5}".format(len(num), len(comment_only), len(n1_list), len(n2_list), len(n3_list), len(n4_list)))
fdf = pd.DataFrame(final)
fdf.T