-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.py
197 lines (161 loc) · 8.17 KB
/
detector.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
import json, math, itertools, re, hangul_utils
from numpy import dot
from numpy.linalg import norm
from CurseWordDetector.similars import *
from CurseWordDetector.char2vec import *
from CurseWordDetector.SoundBase import detector as SoundBase
def CosineSimilarity(vec1, vec2):
return dot(vec1, vec2)/((norm(vec1)*norm(vec2))+1e-30)
CHOSUNG_LIST = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
JUNGSUNG_LIST = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
JONGSUNG_LIST = [' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
sound = SoundBase()
def split(word):
return [char for char in word]
def ksplit(korean_word, convert=True):
r_lst = []
for w in split(korean_word):
if '가'<=w<='힣':
ch1 = (ord(w) - ord('가'))//588
ch2 = ((ord(w) - ord('가')) - (588*ch1)) // 28
ch3 = (ord(w) - ord('가')) - (588*ch1) - 28*ch2
if JONGSUNG_LIST[ch3] != ' ':
new_ = [CHOSUNG_LIST[ch1], JUNGSUNG_LIST[ch2], JONGSUNG_LIST[ch3]]
else:
new_ = [CHOSUNG_LIST[ch1], JUNGSUNG_LIST[ch2], ' ']
if new_[0] == "ㅇ": new_[0] = ' '
r_lst += new_
else:
r_lst.append(w)
if convert:
return [ConvertText(k) for k, g in itertools.groupby(r_lst)]
else:
return [k for k, g in itertools.groupby(r_lst)]
def ksplit2(korean_word, convert=True):
r_lst = []
for w in split(korean_word):
if '가'<=w<='힣':
ch1 = (ord(w) - ord('가'))//588
ch2 = ((ord(w) - ord('가')) - (588*ch1)) // 28
ch3 = (ord(w) - ord('가')) - (588*ch1) - 28*ch2
if JONGSUNG_LIST[ch3] != ' ':
new_ = [CHOSUNG_LIST[ch1], JUNGSUNG_LIST[ch2], JONGSUNG_LIST[ch3]]
else:
new_ = [CHOSUNG_LIST[ch1], JUNGSUNG_LIST[ch2]]
if new_[0] == "ㅇ": new_ = new_[1:]
r_lst += new_
else:
r_lst.append(w)
if convert:
return [ConvertText(k) for k, g in itertools.groupby(r_lst)]
else:
return [k for k, g in itertools.groupby(r_lst)]
def ConvertText(chr):
chr = seems.get(chr, chr)
# chr = en2kr.get(chr, chr)
return chr
def word2vec(text:str, convert=True, method=ksplit) -> list:
splited = method(text, convert)
vector = []
for c in splited:
vector += CHAR2VEC.get(c, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,])
return vector
def GenerateVector(word1, word2, convert=True, method=ksplit):
maxlen = max(len(method(word1, convert)), len(method(word2, convert))) * 8
vec1, vec2 = word2vec(word1, convert, method), word2vec(word2, convert, method)
vec1 = vec1 + [0] * (maxlen - len(vec1))
vec2 = vec2 + [0] * (maxlen - len(vec2))
return vec1, vec2
def similarity(word1, word2, convert=True, method=ksplit):
vec1, vec2 = GenerateVector(word1, word2, convert, method)
return CosineSimilarity(vec1, vec2)
def FindAll(text, item):
indices = []
for idx, value in enumerate(text):
if value == item:
indices.append(idx)
return indices
en = 'abcdefghijklmnopqrstuvwxyz'
def filtering(string):
print(hangul_utils.jamo_type('a'))
class detector:
def __init__(self, CursePath=".\\CurseWordDetector\\curse.json") -> None:
self.path = CursePath
self.curses = json.load(open(CursePath, 'r', encoding='utf8'))
def detect(self, sentence, RemoveSpecials=True):
try:
threshold = 0.7
detected = False
text = sentence
result = text.split(" ")
org = text.split(" ")
for i, token in enumerate(result):
specials = set(re.findall(r'[^A-Za-z가-ퟻㄱ-ㅣ]', token))
specialsmap = []
for s in specials:
specialsmap += [(i, s) for i in FindAll(token, s)]
specialsmap = sorted(specialsmap, key=lambda t: t[0])
for curse in self.curses['curse']:
changed = False
sim = similarity(token, curse)
sim_unconvert = similarity(token, curse, False)
sim_ksplit2 = similarity(token, curse, method=ksplit2)
sim_ksplit2 = -1
# print(sim, sim_unconvert, sim_ksplit2)
processed = token
if (sim >= threshold or sim_unconvert >= threshold or sim_ksplit2 >= threshold):
if (curse in self.curses['force'] or sim_unconvert >= threshold + .1 or sim_ksplit2 >= threshold + .1):
processed = "*"*len(token)
changed = True
print(f'[token] {curse}, {token}\n', sim, sim_unconvert, sim_ksplit2)
else:
soundpos = sound.detect(token)
if (soundpos > 0.8):
changed = True
processed = "*"*len(token)
print(f'[token+sound] {curse}, {token}\n', sim, sim_unconvert, sim_ksplit2, '\n', soundpos)
for index, special in specialsmap:
processed = processed[:index] + special + processed[index:]
if changed: result[i] = processed; detected = True
text = ' '.join(result)
sentence = text
if RemoveSpecials:
specials = set(re.findall(r'[^A-Za-z가-ퟻㄱ-ㅣ]', sentence))
specialsmap = []
for s in specials:
specialsmap += [(i, s) for i in FindAll(sentence, s)]
specialsmap = sorted(specialsmap, key=lambda t: t[0])
sentence = re.sub(r'[^ A-Za-z가-ퟻㄱ-ㅣ+]', "(REMOVE)", sentence)
sentence = sentence.replace("(REMOVE)", "")
text = sentence.replace(" ", "")
for curse in self.curses['curse']:
size = len(curse)
for i in range(len(text) - size + 1):
check = text[i:i+size]
sim = similarity(check, curse)
sim_unconvert = similarity(check, curse, False)
sim_ksplit2 = similarity(check, curse, method=ksplit2)
sim_ksplit2 = -1
if (sim >= threshold or sim_unconvert >=threshold or sim_ksplit2 >= threshold):
cleaned = hangul_utils.join_jamos(''.join(ksplit2(check)))
if (curse in self.curses['force'] or sim >= threshold +.1 or sim_unconvert >=threshold+.1 or sim_ksplit2 >= threshold+.1):
text = text.replace(check, "*"*len(check))
print(f'[token] {curse}, {check}\n', sim, sim_unconvert, sim_ksplit2)
detected = True
continue
soundpos = sound.detect(check)
print(soundpos)
if (soundpos >= 0.8):
text = text.replace(check, "*"*len(check))
print(f'[token] {curse}, {check}\n', sim, sim_unconvert, sim_ksplit2, '\n', soundpos)
detected = True
#restore spacing
for index, special in specialsmap:
text = text[:index] + special + text[index:]
#
return text, detected
except Exception as e:
print(f"[Detector Error] {e}\n\nSentence: {sentence}")
return sentence
def reload(self):
self.curses = json.load(open(self.path, 'r', encoding='utf8'))