-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_analysis_module_divide.py
More file actions
344 lines (232 loc) ยท 13.5 KB
/
text_analysis_module_divide.py
File metadata and controls
344 lines (232 loc) ยท 13.5 KB
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
#!/usr/bin/env python
# coding: utf-8
# 1. TDM์์ ํน์ ๋ฌธ์์์๋ง ์์ฃผ ์ฌ์ฉ๋ ๋จ์ด์ ๋ํด์๋ ๋ฐ์ด๋ด ๋ง์ผํ
๋จ์ด๋ก ํ๋จํ๋ค. (์
์ฒด ์ ๋ณด์ ๋ํ ๊ณผํ ์ดํ) #
# -> ์ถํ ๋จธ์ ๋ฌ๋์ ํตํด ํด๋น ๋จ์ด๋ค์ ํ์ตํ๊ณ , ๋ฐ์ด๋ด ๋ง์ผํ
๊ตฌ๋ถ ๋ฌธ์์ธ์ง๋ฅผ ํ์ต์ํจ๋ค.
# 2. ๊ณ ๋ฃ์ ๊ดํ ์ธ๊ธ์ด ์๋ ๊ฒฝ์ฐ ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ๋ก ํ๋จํ๋ค. #
# 3. Co-occurrence์์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋์ง ์๋ ๋ฌธ์ ์กฐํฉ์ด ์๋ ๊ฒฝ์ฐ, ์ด์ํ ๋ฌธ์ฅ์ผ๋ก ํ๋จํ์ฌ ๋ฐ์ด๋ด ๋ง์ผํ
๋ฐ์ดํฐ๋ก ์์ฌํ๋ค.
# -> ์ถํ ๋จธ์ ๋ฌ๋์ ๋์
ํ์ฌ ์ผ๋ฐ์ ์ธ ๋ฌธ์ ์กฐํฉ์ ๋ํ ํ์ต
# 4. TDM์์ ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ์ ์ฌ์ฉ๋ ๋ฌธ์ ์ค 1 ๋ฐ 2์ ๋ํ ๋ฌธ์๋ฅผ ๋จธ์ ๋ฌ๋์ด ํ์ตํ๊ณ , ์ค์๋๋ฅผ ๋ถ์ฌํ์ฌ ์ถํ ๋ค๋ฅธ ๊ธ์ ๋ถ์ํ ๋, ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ์ฒ๋๋ก ์ฌ์ฉํ๋ค.
# 5. ๋ค๋ฅธ ๊ธ๊ณผ ๋น๊ตํ ๋ ์ ์ฌํ ๋ฌธ์ฅ ์๋ฅผ ๋ถ์ํ์ฌ ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ ๊ตฌ๋ถ์ ๋ํ ์ฒ๋๋ก ์ฌ์ฉํ๋ค. #
# 6. ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ์ง ์์ ๋จ์ด๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ๋ฐ์ด๋ด ๋ง์ผํ
๋ฌธ๊ตฌ๋ก ํ๋จ. #
# -> ์ถํ ๋จธ์ ๋ฌ๋์ ํตํด ๊ฐ์ค์น ๋ถ์ฌ ๋ฐ ํ๋จ ์ฒ๋๋ก ์ฌ์ฉ
# 7. Co-occurrence matrix ๋ถ์ ๊ฒฐ๊ณผ์ ๋จธ์ ๋ฌ๋์ ํตํด ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ์์ ์ฌ์ฉํ ๋ฌธ์ ์กฐํฉ(๋ฌธ์ฅ)์ ๋ฐ์ด๋ด ๋ง์ผํ
๋ฐ์ดํฐ๋ก์จ์ ๊ฐ์ค์น๋ฅผ ๋ถ์ฌํ๋ค.
# -> ์ถํ ์๋ก์ด ๋ฐ์ดํฐ๊ฐ ๋ค์ด์์ ๋, ๋ฐ์ด๋ด ๋ง์ผํ
๊ธ์ ํ๋ณํ๋๋ฐ ์ฌ์ฉํ๋ค.
# In[20]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
pd.set_option('display.max_rows', 999) # pd.options.display.max_rows = 999
pd.set_option('display.max_columns', 999) # pd.options.display.max_columns = 999
pd.set_option('display.width', 1000)
# ํ๊ธํฐํธ ์ ์ฉ
plt.rcParams['font.family'] = 'Malgun Gothic'
# In[21]:
absolute_file_address = input("")
# read text data set
text_set = pd.read_csv(absolute_file_address)
text_set = text_set.fillna("")
text_set
# In[22]:
text_set.shape
# In[23]:
text_set = text_set.astype({'text':'string', 'url':'string'})
text_set.dtypes
# In[ ]:
from sklearn.feature_extraction.text import CountVectorizer
from konlpy.tag import Okt
import nltk
from nltk import bigrams
import itertools
import copy
import openpyxl
import csv
# co-occurrence matrix๋ฅผ ํ์ฑํ๋ ํจ์
def generate_co_occurrence_matrix(corpus):
vocab = set(corpus)
vocab = list(vocab)
vocab_index = {word: i for i, word in enumerate(vocab)}
bi_grams = list(bigrams(corpus))
bigram_freq = nltk.FreqDist(bi_grams).most_common(len(bi_grams))
co_occurrence_matrix = np.zeros((len(vocab), len(vocab)))
for bigram in bigram_freq:
current = bigram[0][1]
previous = bigram[0][0]
count = bigram[1]
pos_current = vocab_index[current]
pos_previous = vocab_index[previous]
co_occurrence_matrix[pos_current][pos_previous] = count
co_occurrence_matrix = np.matrix(co_occurrence_matrix, dtype=np.int32)
return co_occurrence_matrix, vocab_index
f = open('submission.csv', 'w', newline='')
wr = csv.writer(f)
# ๊ณผ๋ํ ์ ๋ณด ํ๊ธฐ , ๊ณ ์ ๋จ์ด ์ฌ์ฉ, ๊ณ ๋ฃ ํ๊ธฐ, ์ ์ฌํ ๋ฌธ์ฅ ์ฌ์ฉ, ์ ์ฌํ ๋ฌธ์ฅ ์ฌ์ฉ
wr.writerow(["url", "tmi", "use_unique_words", "indication_reward", "use_similar_sentences(including_body)", "use_similar_sentences(without_body)"])
# wb = openpyxl.load_workbook('submission.csv')
# sheet = wb.active
# sheet.cell(row=1, column=1).value = "๊ณผ๋ํ ์ ๋ณด ํ๊ธฐ"
# sheet.cell(row=1, column=2).value = "๊ณ ์ ๋จ์ด ์ฌ์ฉ"
# sheet.cell(row=1, column=3).value = "๊ณ ๋ฃ ํ๊ธฐ"
# sheet.cell(row=1, column=4).value = "์ ์ฌํ ๋ฌธ์ฅ ์ฌ์ฉ(๋ณธ๋ฌธ ํฌํจ)"
# sheet.cell(row=1, column=5).value = "์ ์ฌํ ๋ฌธ์ฅ ์ฌ์ฉ(๋ณธ๋ฌธ ๋ฏธํฌํจ)"
# wb.save("submission.csv")
analysis_count = 0
while analysis_count < len(text_set):
if analysis_count+5 < len(text_set)-1:
text_sub_set = text_set[analysis_count : analysis_count+5]
else:
text_sub_set = text_set[analysis_count : len(text_set)-1]
# matrix(DataFrame)์์ text๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
text_list = text_sub_set['text']
url_list = text_sub_set['url']
url_list = url_list.tolist()
# ํ๊ตญ์ด ์กฐ์ฌ ์ ๋ฆฌ.
# ํ๊ตญ์ด ๋ฌธ๋ฒ๋ก ๋๊ฐ ์ด์ต์ญ ์์ธ๋ ๋ช
์๊ต์๋๊ป์ ์ฐ์ 'ํ๊ตญ์ด๋ฌธ๋ฒ'์ฐธ๊ณ .
# ์์ฐ์ด์ฒ๋ฆฌ ๋ถ์ผ์์ ํ๊ตญ์ด ์กฐ์ฌ๋ ๋ถ์์ ๊น๋ค๋ก์์ผ๋ก ์ธํด ์ ์ฒ๋ฆฌ ๋ ์์ ์ ๊ฑฐ๋๋ ๋ถ์ฉ์ด(stop_words)๋ก ์ทจ๊ธ๋จ.
stop_words = ["์", "๋", "์", "๋ฅผ", "์ด", "๊ฐ", "์", "์", "๋ก", "์ผ๋ก", "๊ณผ", "์", "๋", "์์", "๋ง"
, "์ด๋", "๋", "๊น์ง", "๋ถํฐ", "์๊ฒ", "๋ณด๋ค", "๊ป", "์ฒ๋ผ", "์ด๋ผ๋", "๋ผ๋", "์ผ๋ก์", "๋ก์"
, "์กฐ์ฐจ", "๋งํผ", "๊ฐ์ด", "๋ง์ ", "์ด๋๋ง", "๋๋ง", "ํํ
", "๋๋ฌ", "์๊ฒ์", "ํํ
์", "๊ป์"
, "์ด์ผ", "์ด๋ผ์ผ"]
commissional_words = ["ํ์ฐฌ", "๊ณ ๋ฃ", "๊ด๊ณ ", "ํ์", "์๊ณ "]
# Open Korea Text๋ฅผ ์ฌ์ฉํ ๋ช
์ฌ ์ถ์ถ ๋ชจ๋ ํ์ฑ - tokenizer์งํ
# ๋ณธ๋ CountVectorizer๋ ํ ํฌ๋์ด์ง๊ณผ ๋ฒกํฐํ๋ฅผ ๋์ํด ํด์ฃผ๋, ์ด๋ ํ๊ตญ์ด๋ฅผ ๋์์ผ๋กํ์ง๋ ์์.
# ๋ฐ๋ผ์ Okt๋ชจ๋์ ํตํด ํ ํฌ๋์ด์ง์ ๋จผ์ ํ ํ CountVectorizer์์
ํ์
okt=Okt()
text_token_set = list()
count = 0
for text in text_list.tolist():
text_token_set.append(okt.nouns(text))
# ์์คํ
์ด 2์ธต, 2์ธต์, 2์ธต์ผ๋ก ์ ๊ฐ์ ๊ธ๋ค์ ๊ฐ๊ธฐ ๋ค๋ฅธ ํ๋์ ๋ช
์ฌ ๋จ์ด๋ก ํ๋จํ ์ ์๊ธฐ์ ํ ํฌ๋์ด์ง์ ์ฒ๋ฆฌํจ
# ์ถ๋ ฅ๊ฒฐ๊ณผ NxM: N๊ฐ์ ๋ฐ์ดํฐ์์ M๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์๋: ๋ฒกํฐํ
# ๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฐ ๋ฌธ์์ ์ด๋ค ๋จ์ด๊ฐ ๋ช๋ฒ ๋ฑ์ฅํ๋์ง๋ฅผ ํ์
ํ ์ ์์
try:
cv = CountVectorizer(tokenizer=lambda x: x, lowercase=False)
tdm = cv.fit_transform(text_token_set)
except ValueError:
# dummy_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
# wb = openpyxl.load_workbook('submission.csv')
# sheet = wb.active
# for row in range(1, 6):
# sheet.cell(row=analysis_count+row+1, column=1).value = dummy_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=2).value = dummy_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=3).value = dummy_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=4).value = dummy_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=5).value = dummy_value[row-1]
# wb.save("submission.csv")
for row in range(0, 5):
wr.writerow([0, 0, 0, 0, 0])
analysis_count = analysis_count+5
# TDM ์ถ๋ ฅ
# TDM ๋๊ท๋ชจ ๋ฐ์ดํฐ์์ ๋๋ถ๋ถ์ ๊ฐ์ 0์ผ๋ก ๋ํ๋ผ ๊ฒ์.
# ์ด์ ๋ ํ๋์ ํ
์คํธ์ 2000๊ฐ ์ข
๋ฅ์ ๋จ์ด๋ฅผ ์ฌ์ฉํ๋ค ํด๋, ์ ์ฒด ๋จ์ด ์
์ ๋ช ๋ง๊ฐ๋ ๋ ๊ฒ์ด๊ธฐ์
# ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ ๋ฌธ์ ๋ฅผ ์ด๋ํ๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ์, CountVectorizer๋ ํฌ์ํ๋ ฌ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ.
# ๋ค์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ํฌ์ํ๋ ฌ์
tdm_dataframe = pd.DataFrame(tdm.toarray())
# TDM ๋ถ์: ๋๋ฌด ๋
์์ ์ธ ๋จ์ด ์ฌ์ฉ, ๋๋ฌด TMI์ ๋จ์ด ์ฌ์ฉ ๊ฐ์ง (ํ๊ท * 5ํ ๋ณด๋ค ์ฌ์ฉ์๊ฐ ๋ง๊ฑฐ๋ ๊ฐ๊ณ , 5ํ ์ด์ ์ฌ์ฉ๋ ๋จ์ด)
use_tmi_words_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
# TDM ๋ถ์: ๋ชจ๋ ๋
์์ ์ธ ๋จ์ด์ ๋ํ ์์น (ํผ์ ์ฌ์ฉ๋ ๋จ์ด๊ฐ 10๊ฐ์ค 1๊ฐ ๋ฏธ๋ง ์ผ ๊ฒฝ์ฐ)
own_words_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
for col in range(0, tdm_dataframe.shape[1]):
avg = sum(tdm_dataframe[col], 0.0) / len(tdm_dataframe[col]) # avg * 5 < ํน์ ํ ๋ฌธ์์์๋ง TMI์ ์ผ๋ก ์ฌ์ฉ๋ ๋จ์ด ์ฌ์ฉ ๋น๋
for row in range(0, len(tdm_dataframe[col])):
if tdm_dataframe[col][row] >= (avg * 5) and tdm_dataframe[col][row] >= 5:
use_tmi_words_value[row] = use_tmi_words_value[row] + 1
print("use_tmi_words_value:", use_tmi_words_value)
# TDM ๋ถ์: ๋ชจ๋ ๋
์์ ์ธ ๋จ์ด์ ๋ํ ์์น (ํผ์ ์ฌ์ฉ๋ ๋จ์ด๊ฐ 10๊ฐ์ค 1๊ฐ ๋ฏธ๋ง ์ผ ๊ฒฝ์ฐ)
own_words_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
for col in range(0, tdm_dataframe.shape[1]):
used_value = 0
for row in tdm_dataframe[col]:
if row != 0:
used_value += 1
for row in range(0, len(tdm_dataframe[col])):
if(len(text_list) == 0):
continue
if len(text_list) >= 20:
if (used_value / 20) <= 1 and tdm_dataframe[col][row] != 0:
own_words_value[row] = own_words_value[row] + 1
else:
if(used_value / len(text_list)) <= 1 and tdm_dataframe[col][row] != 0:
own_words_value[row] = own_words_value[row] + 1
print("own_words_value", own_words_value)
commissional_words_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
for index in range(0, len(text_list)):
for word in commissional_words:
if(len(text_list) == 0):
continue
if(text_list.tolist()[index].find(word) != -1):
commissional_words_value[index] = 1
print("commissional_words_value:", commissional_words_value)
# # Co-occurrence matrix ํ์ฑ
co_occurrence_matrix_list = list()
for text in text_list:
tmp = text.split("\n")
text_data = [okt.nouns(line) for line in tmp]
text_data = list(itertools.chain.from_iterable(text_data))
matrix, vocab_index = generate_co_occurrence_matrix(text_data)
matrix_dataframe = pd.DataFrame(matrix, index=vocab_index, columns=vocab_index)
co_occurrence_matrix_list.append(matrix_dataframe)
# Co-occurrence matrix ๋ถ์: ๋๋ฌด ๋
์์ ์ธ ๋จ์ด ์ฌ์ฉ, ๋๋ฌด TMI์ ๋จ์ด ์ฌ์ฉ ๊ฐ์ง
awkward_sentence_value = np.zeros(shape=(len(text_list),), dtype=np.int64)
# # ๋น์ทํ ๋ฌธ์ฅ ํ๋
# 1. ๋ชจ๋ ๋ฌธ์ฅ์ ๋ํด BoW์ ์ฉ
# 2. ๋ชจ๋ text ๋์ดํฐ์ ๋ํด ๋ํด ์ ์ฌ๊ธ ์์น ๋ถ์ฌ (๊ฐฑ์ )
# 3. ๋ชจ๋ text ๋ฐ์ดํฐ์ ๋ํด ์ ์ฌ๊ธ ์์น ํ๊ท ๊ณ์ฐ
# text๋ฐ์ดํฐ ๋ณ ๋น์ทํ ๋ฌธ์ฅ ์ ๋ฆฌ์คํธ
similar_sentence_value_all = np.zeros(shape=(len(text_list),), dtype=np.int64)
similar_sentence_value_bes = np.zeros(shape=(len(text_list),), dtype=np.int64)
# text_listํ ํฐํ
text_data_token_set = list()
for text in text_list:
lines = text.split('\n')
lines_token_set = [okt.nouns(line) for line in lines]
text_data_token_set.append(lines_token_set)
comp_list = list(itertools.chain.from_iterable(text_data_token_set))
for i in range(0, len(text_list)):
for line in text_data_token_set[i]:
for comp in comp_list:
line_len = len(line)
comp_len = len(comp)
line_to_comp_sub_len = len([x for x in line if x not in comp])
comp_to_line_sub_len = len([x for x in comp if x not in line])
if line_len + comp_len != 0:
if (line_to_comp_sub_len + comp_to_line_sub_len) / (line_len + comp_len) < 0.5:
similar_sentence_value_all[i] = similar_sentence_value_all[i] + 1
for i in range(0, len(text_list)):
for line in text_data_token_set[i]:
explore_dest = copy.deepcopy(text_data_token_set)
del explore_dest[i]
for comp in list(itertools.chain.from_iterable(explore_dest)):
line_len = len(line)
comp_len = len(comp)
line_to_comp_sub_len = len([x for x in line if x not in comp])
comp_to_line_sub_len = len([x for x in comp if x not in line])
if line_len + comp_len != 0:
if (line_to_comp_sub_len + comp_to_line_sub_len) / (line_len + comp_len) < 0.5:
similar_sentence_value_bes[i] = similar_sentence_value_bes[i] + 1
print("similar_sentence_value_all:", similar_sentence_value_all)
print("similar_sentence_value_bes: ", similar_sentence_value_bes)
print("Analysis position:", analysis_count, "->", analysis_count+5)
print("\n")
# wb = openpyxl.load_workbook('submission.csv')
# sheet = wb.active
# for row in range(1, 6):
# if(len(use_tmi_words_value) < row):
# break
# sheet.cell(row=analysis_count+row+1, column=1).value = use_tmi_words_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=2).value = own_words_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=3).value = commissional_words_value[row-1]
# sheet.cell(row=analysis_count+row+1, column=4).value = similar_sentence_value_all[row-1]
# sheet.cell(row=analysis_count+row+1, column=5).value = similar_sentence_value_bes[row-1]
# wb.save("submission.csv")
for row in range(0, 5):
if(len(use_tmi_words_value) <= row):
break
wr.writerow([url_list[row], use_tmi_words_value[row], own_words_value[row], commissional_words_value[row], similar_sentence_value_all[row], similar_sentence_value_bes[row]])
analysis_count = analysis_count+5
f.close()
# # ๊ฒฐ๊ณผ ๊ธฐ๋ก
# In[ ]:
print("text_module_finish")
# In[ ]: