-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
57 lines (45 loc) · 1.27 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
import re
import unicodedata
def _normalize(s, form="NFD"):
return unicodedata.normalize(form, s)
def remove_controls(x):
x = re.sub('\s+', " ", x)
return x
def remove_bracket(x):
def _remove_punctuations(x):
def _is_punctuation(char):
cp = ord(char)
if (
(33 <= cp <= 47)
or (58 <= cp <= 64)
or (91 <= cp <= 96)
or (123 <= cp <= 126)
):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
x = _normalize(x, "NFD")
x = list(x)
x_ = []
for each in x:
if _is_punctuation(each) is True:
continue
x_.append(each)
x = _normalize(''.join(x_), "NFC")
return x
x = re.sub(r"\[[a-zA-Z0-9가-힣\s]*\]\s*", "", x)
x = re.sub(r"\([a-zA-Z0-9가-힣\s]*\)\s*", "", x)
x = _remove_punctuations(x)
return x
def replace_whitespace(x):
x = re.sub(r"\s+", " ", x)
return x
f_list = [
remove_bracket,
remove_controls,
replace_whitespace,
lambda x: x.lower(),
lambda x: re.sub(r"[^a-zA-Z가-힣ㄱ-ㅎ?.!,0-9]", " ", x),
]