-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
214 lines (184 loc) · 6.49 KB
/
preprocess.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
import ijson
import codecs
import re
import kss
from tqdm import tqdm
from soynlp.normalizer import *
WIKI_REMOVE_CHARS = re.compile("'+|(=+.{2,30}=+)|__TOC__|(ファイル:).+|:(en|de|it|fr|es|kr|zh|no|fi):|\n")
WIKI_SPACE_CHARS = re.compile("(\\s|゙|゚| )+")
EMAIL_PATTERN = re.compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
URL_PATTERN = re.compile("(ftp|http|https)?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
WIKI_REMOVE_TOKEN_CHARS = re.compile("(\\*$|:$|^파일:.+|^;)")
MULTIPLE_SPACES = re.compile(' +')
def load_json(filename):
count=0
with open(filename, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix.endswith('.title'):
print('\nindex=', count+1)
print("TITLE: %s" % value)
elif prefix.endswith('.text'):
print("CONTENT: %s" % value)
count += 1
if count==10 : # 10개만 출
break
def load_and_write_content(filename, filename2):
count=0
file = codecs.open(filename2, 'w', encoding='utf-8')
with open(filename, 'r') as fd:
for item in ijson.items(fd, 'item'):
count+=1
file.write('[[제목]]: ')
file.write(item['title'])
file.write('\n')
file.write('[[내용]]: \n')
file.write(item['text'])
file.write("\n")
file.close()
print('contents count=', count)
"""
doc_num 갯수만큼 나무 위키 데이터 만들기
제목, 내용으로 텍스트 파일 생성
>>> make_mini_namu(namu_origin,mini_namu,is_mini=False)
"""
def make_mini_namu(namu_origin, mini_namu, doc_num = 100, is_mini=True):
count = 0
mini_file = open(mini_namu, 'w', encoding='utf-8')
with open(namu_origin, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix.endswith('.title'): # 제목
mini_file.write("\n\n\n" + remove_punct(value).replace('\n', ''))
elif prefix.endswith('.text'): # 내용
mini_file.write("\n" + remove_punct(value).replace('\n\n', '\n'))
count += 1
if is_mini and count == doc_num: # 10개만 출
break
mini_file.close()
"""
일부 문자 변경
"""
def change_mapping(text, mapping):
for p in mapping:
text = text.replace(p, mapping[p])
return text.strip()
"""
반복 제거
"""
def remove_repeat(text, num_repeats=2):
text= repeat_normalize(text, num_repeats=num_repeats)
return text
"""
"""
def namu_preprocess(namu_origin, processed_file):
file = open(processed_file, 'w', encoding='utf-8')
r_f= open(namu_origin, 'r')
while True:
line = r_f.readline()
if not line: break
if line =='\n':
file.write(line)
continue
line = remove_punct(line)
line = replace_punct(line)
line = clean_text_using_regexr(line)
line = remove_line(line)
if line =='':
continue
kss_sentence_seperator(file,line)
file.close()
def clean_html(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def remove_punct(text):
# punct = "/-'?!.,#$%\'()*+-/:;<=>@[\\]^_`{|}}~\"" #+ '""“”’' + '∞θ÷α•à−β∅³π‘₹´°£€\×™√²—–&'
my_punct = ['"""', "'''", "===", "[[", "]]", "{{{", "}}}", "!!", "!!", "~~", "{{", "}}",">--", "--" ]
for p in my_punct:
text = text.replace(p, '')
return text.strip()
def replace_punct(text):
my_punct = {"||":'|'}
for p in my_punct:
text = text.replace(p,my_punct[p])
return text.strip()
"""
WIKI_REMOVE_CHARS = re.compile("'+|(=+.{2,30}=+)|__TOC__|(ファイル:).+|:(en|de|it|fr|es|kr|zh|no|fi):|\n", re.UNICODE)
WIKI_SPACE_CHARS = re.compile("(\\s|゙|゚| )+", re.UNICODE)
EMAIL_PATTERN = re.compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", re.UNICODE)
URL_PATTERN = re.compile("(ftp|http|https)?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", re.UNICODE)
WIKI_REMOVE_TOKEN_CHARS = re.compile("(\\*$|:$|^파일:.+|^;)", re.UNICODE)
MULTIPLE_SPACES = re.compile(' +', re.UNICODE)
"""
def clean_text_using_regexr(raw_html):
cleaners = [re.compile('<.*?>'), # html 태그
re.compile('\[youtube\(.*\)\]()'), # 유튭 태그
re.compile('\[include\(.*\)\]()'), # include 태그
re.compile('#[0-9a-f]{3,6}'), # 정규식 태그
re.compile('\[\*.*\]'), # 정규식 태그
# WIKI_REMOVE_CHARS,
# WIKI_SPACE_CHARS,
EMAIL_PATTERN,
URL_PATTERN,
# WIKI_REMOVE_TOKEN_CHARS,
# MULTIPLE_SPACES
]
for cleanr in cleaners:
raw_html = re.sub(cleanr, '', raw_html)
return raw_html
def remove_line(raw_text):
cleaner = [
"#redirect",
]
for pattern in cleaner:
if pattern in raw_text:
return ''
else:
return raw_text
"""
문장 분리
"""
def kss_sentence_seperator(file, text):
for sent in kss.split_sentences(text):
file.write(sent.replace('\n', '')+"\n")
"""
"""
def remove_short_docs(from_file, to_file, cut_len=10):
file = open(to_file, 'w', encoding='utf-8')
r_f= open(from_file, 'r')
# 전체 라인 세기
num_lines = sum(1 for line in open(from_file, 'r'))
print(num_lines)
count = 0
doc = ""
for line in tqdm(r_f,
desc='namuwiki data maker',
total=num_lines):
if line == "\n":
if count<3:
count+=1
else:
if len(doc) < cut_len:
doc =""
count = 1
else:
file.write("\n"+doc)
doc = ""
count = 1
elif line != "\n":
doc += line
r_f.close()
file.close()
if __name__ == "__main__":
namu_origin = '../data/docData200302.json'
mini_namu = './namuwiki.txt'
processed_file1 = './namu_processed.txt'
processed_file2 = './namu_processed-doc.txt'
data_path = "/Volumes/My Passport for Mac/00 nlp data"
# 나무위키 데이터 json -> text
# make_mini_namu(namu_origin,mini_namu,is_mini=False)
# 데이터 전처리
# namu_preprocess(mini_namu, processed_file1)
# 짧은 문서 삭제
remove_short_docs(processed_file1,processed_file2,20)