-
Notifications
You must be signed in to change notification settings - Fork 0
/
augmentation.py
262 lines (229 loc) ยท 9.1 KB
/
augmentation.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
import pickle
import re
import pandas as pd
from hanspell import spell_checker
from konlpy.tag import Okt
from pykospacing import Spacing
from soynlp.normalizer import repeat_normalize
from tqdm.auto import tqdm
def under_sampling(data_path: str) -> pd.DataFrame:
"""
label ๊ฐ์ด 0์ธ ๋ฐ์ดํฐ๋ฅผ under samplingํ๋ ํจ์
Args:
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
Returns:
df_new (DataFrame): under sampling๋ ๋ฐ์ดํฐ
"""
df = pd.read_csv(data_path)
df_0 = df[df["label"] == 0][1000:2000].copy()
df_new = df[df["label"] != 0].copy()
df_new = pd.concat([df_new, df_0])
return df_new
def swap_sentence(data_path: str) -> pd.DataFrame:
"""
sentence 1๊ณผ sentence 2์ ์์น๋ฅผ ๋ฐ๊พธ์ด ์ฆ๊ฐํ๋ ํจ์
Args:
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
Returns:
df_swapped (DataFrame): ์ฆ๊ฐ๋ ๋ฐ์ดํฐ
"""
df = pd.read_csv(data_path)
df_swapped = df.copy()
df_swapped["sentence_1"] = df["sentence_2"]
df_swapped["sentence_2"] = df["sentence_1"]
df_swapped = df_swapped[df_swapped["label"] != 0]
return df_swapped
def copy_sentence(data_path: str, index_min=250, index_max=750) -> pd.DataFrame:
"""
sentence 1๊ณผ sentence 2์ ๊ฐ์ ๋ฌธ์ฅ์ ๋ฐฐ์นํด 5์ ์ง๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ ํจ์
Args:
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
index_min (int): ์ฆ๊ฐํ ๋ฐ์ดํฐ์์ ์ฌ๋ผ์ด์ฑ ์์ defalt = 250
index_max (int): ์ฆ๊ฐํ ๋ฐ์ดํฐ์์ ์ฌ๋ผ์ด์ฑ ๋ defalt = 750
Returns:
df_copied (DataFrame): ์ฆ๊ฐ๋ ๋ฐ์ดํฐ
"""
df = pd.read_csv(data_path)
df_copied = df[df["label"] == 0][index_min:index_max].copy()
df_copied["sentence_1"] = df_copied["sentence_2"]
df_copied["label"] = 5.0
return df_copied
def concat_data(data_path: str, *dataframes: pd.DataFrame):
"""
๋ฐ์ดํฐํ๋ ์์ ํฉ์ณ์ csv ํ์ผ๋ก ์ ์ฅํ๋ ํจ์
Args:
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
dataframes (DataFrame): ํฉ์น๋ ค๊ณ ํ๋ ๋ฐ์ดํฐํ๋ ์
"""
result = pd.concat(dataframes)
result.to_csv(data_path, index=False)
def augment(source_data_path, dest_data_path):
df_under_sampled = under_sampling(source_data_path)
df_swapped_sentence = swap_sentence(source_data_path)
df_copied_sentence = copy_sentence(source_data_path)
concat_data(
dest_data_path, df_under_sampled, df_swapped_sentence, df_copied_sentence
)
def han_spell(text: str) -> str:
"""
ํน์๋ฌธ์ ์ ๊ฑฐ ๋ฐ hanspell ๋ง์ถค๋ฒ ๊ฒ์ฌ
Args :
text (str): ๊ต์ ํ sentence
Returns :
correct_text (str): ๊ต์ ํ sentence
"""
text = repeat_normalize(text, num_repeats=2)
text = text.lower()
text = re.sub("[^a-z๊ฐ-ํฃ0-9 ]", "", text)
text = text.strip()
correct_text = spell_checker.check(text).as_dict()["checked"]
return correct_text
def apply_hanspell(df: pd.DataFrame) -> pd.DataFrame:
"""
han_spell()์ ๋ฐ์ดํฐ์ ์ ์ฉ
Args :
data (DataFrame): ๋ง์ถค๋ฒ์ ๊ต์ ํ ๋ฐ์ดํฐ
Returns :
data (DataFrame): ๋ง์ถค๋ฒ์ ๊ต์ ํ ๋ฐ์ดํฐ
"""
tqdm.pandas()
df["sentence_1"] = df["sentence_1"].progress_map(han_spell)
df["sentence_2"] = df["sentence_2"].progress_map(han_spell)
df = df.dropna(subset=["sentence_1"])
df = df.dropna(subset=["sentence_2"])
return df
def check_end(noun: str) -> bool:
"""
ํ๊ธ์ ์ ๋์ฝ๋๊ฐ 28๋ก ๋๋์ด ๋จ์ด์ง๋ฉด ๋ฐ์นจ์ด ์์์ ํ๋จ
Args :
noun (str): ๋ฐ์นจ ์ ๋ฌด๋ฅผ ํ๋จํ ๋ช
์ฌ
Returns :
False (bool) : ๋ฐ์นจ์ด ์์
True (bool) : ๋ฐ์นจ์ด ์์
"""
if (ord(noun[-1]) - ord("๊ฐ")) % 28 == 0:
return False
else:
return True
def change_josa(noun: str, josa: str) -> str:
"""
๋ช
์ฌ์ ๋์์ ๋ฐ์นจ ์ฌ๋ถ์ ๋ฐ๋ผ์ ์กฐ์ฌ ๊ต์ฒด
Args :
none (str): ๋์์ ์ ๋ฐ์นจ ํ์ธํ ๋ช
์ฌ
josa (str): ๊ต์ ํ ์กฐ์ฌ
Returns :
josa (str): ๊ต์ ํ ์กฐ์ฌ
"""
if josa == "์ด" or josa == "๊ฐ":
return "์ด" if check_end(noun) else "๊ฐ"
elif josa == "์" or josa == "๋":
return "์" if check_end(noun) else "๋"
elif josa == "์" or josa == "๋ฅผ":
return "์" if check_end(noun) else "๋ฅผ"
elif josa == "๊ณผ" or josa == "์":
return "๊ณผ" if check_end(noun) else "์"
else:
return josa
def make_sentence(sentence: list, compare: str, sym: str) -> str:
"""
sentence_1, sentence_2์ ๋ชจ๋ ๋ฑ์ฅํ๋ ๋ช
์ฌ๋ฅผ ๊ต์ฒดํ๊ณ ์กฐ์ฌ๋ฅผ ๊ต์
Args :
sentence (list): ํํ์ ๋ถ์ํ ๋ฌธ์ฅ
compare (str): ๋ฌธ์ฅ์์ ๋ฐ๊ฟ ๋ช
์ฌ
sym (str): ๋ฌธ์ฅ ์ฝ์
๋๋ ๋์์ด
Returns :
replace_sentence (str): ๋์์ด๋ก ๊ต์ฒดํ ๋ฌธ์ฅ
"""
spacing = Spacing()
replace_sentence = []
check = set(["์ด", "๊ฐ", "์", "๋ฅผ", "๊ณผ", "์"])
for j in range(len(sentence)):
# ๋ฌธ์ฅ์์ ๋์์ด๋ฅผ ์ถ๊ฐํ๋ค.
if sentence[j][0] == compare:
replace_sentence.append(sym)
# ๋ท๋ง์ด ์กฐ์ฌ๋ฉด ์กฐ์ฌ๋ฅผ ํ์ธํ๊ณ ๋ฐ๊พผ๋ค.
if (
j + 1 < len(sentence)
and sentence[j + 1][1] == "Josa"
and sentence[j + 1][0] in check
):
# ๋ฐ๋ ๋ช
์ฌ ๋ง์ง๋ง ๋ฐ์นจ ํ์ธ ํ ์กฐ์ฌ ๋ณ๊ฒฝ
sentence[j + 1] = (
change_josa(replace_sentence[-1][0], sentence[j + 1][0]),
"Josa",
)
else:
replace_sentence.append(sentence[j][0])
replace_sentence = "".join(replace_sentence)
replace_sentence = spacing(replace_sentence)
return replace_sentence
def sr_noun_replace(data_path: str, wordnet_path: str) -> pd.DataFrame:
"""
๋ฐ์ดํฐ๋ฅผ ๋ง์ถค๋ฒ ๊ต์ ํ ๋ช
์ฌ์ ์กฐ์ฌ๋ฅผ ๊ต์ฒด ์ฆ๊ฐ
Args :
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
wordnet_path (str): ๋์์ด ์ฌ์ ๊ฒฝ๋ก
Returns :
sr_sentence (DataFrame): ์ฆ๊ฐ๋ ๋ฐ์ดํฐ
"""
with open(wordnet_path, "rb") as f:
wordnet = pickle.load(f)
data = pd.read_csv(data_path)
okt = Okt()
data = apply_hanspell(data)
n1, n2 = data["sentence_1"], data["sentence_2"]
sr_sentence = []
for i in tqdm(range(len(n1)), desc="SR Sentece"):
now_sentence1 = n1[i]
now_sentence2 = n2[i]
noun1 = okt.nouns(now_sentence1)
noun2 = okt.nouns(now_sentence2)
# ๋ ๋ฌธ์ฅ์์ ๊ณตํต๋ ๋ช
์ฌ๋ฅผ ์ถ์ถ
compare = set(noun1) & set(noun2)
for com in compare:
# ๊ธธ์ด๊ฐ 2์ด์์ธ์ง(์๋ชป ๊ณ ๋ฅผ ์ ์์), wordnet์ ์๋์ง ํ์ธ
if len(com) > 1 and com in wordnet and len(wordnet[com]) >= 2:
sym_list = wordnet[com][1:]
for sym in sym_list:
s1 = okt.pos(now_sentence1)
s2 = okt.pos(now_sentence2)
sr_sentence.append(
[
data["id"][i],
data["source"][i],
make_sentence(s1, com, sym),
make_sentence(s2, com, sym),
data["label"][i],
data["binary-label"][i],
]
)
sr_sentence = pd.DataFrame(
sr_sentence,
columns=["id", "source", "sentence_1", "sentence_2", "label", "binary-label"],
)
return sr_sentence
def sr_swap_sentence(df: pd.DataFrame) -> pd.DataFrame:
"""
sentence 1๊ณผ sentence 2์(1<= label <3) ์์น๋ฅผ ๋ฐ๊พธ์ด ์ฆ๊ฐํ๋ ํจ์
Args:
data_path (str): ์ฆ๊ฐํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก
sr (bool): swap ๋ฒ์
Returns:
df_swapped (DataFrame): ์ฆ๊ฐ๋ ๋ฐ์ดํฐ
"""
df_swapped = df.copy()
df_swapped["sentence_1"] = df["sentence_2"]
df_swapped["sentence_2"] = df["sentence_1"]
df_swapped = df_swapped[(df_swapped["label"] >= 1) & (df_swapped["label"] < 3)]
return df_swapped
def sr_augment(source_data_path, dest_data_path, wordnet_path):
df_source = pd.read_csv(source_data_path)
df_noun_replaced = sr_noun_replace(source_data_path, wordnet_path)
df_noun_replaced = df_noun_replaced[df_noun_replaced["label"] >= 1]
df_source_noun = pd.concat([df_source, df_noun_replaced])
df_swapped_sentence = sr_swap_sentence(df_source_noun)
df_copied_sentence = copy_sentence(source_data_path, index_min=250, index_max=1250)
concat_data(dest_data_path, df_source_noun, df_swapped_sentence, df_copied_sentence)
if __name__ == "__main__":
augment("./data/train.csv", "./data/aug_train.csv")
sr_augment("./data/train.csv", "./data/sr_augment.csv", "./wordnet/wordnet.pickle")