-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
173 lines (139 loc) · 4.87 KB
/
search.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
# This source code is a part of Project Violet.
# Copyright (C) 2021. violet-team. Licensed under the Apache-2.0 License.
import sys
import os
import math
from os import listdir
import os.path
from os.path import isfile, join
import re
import json
# https://lovit.github.io/nlp/2018/08/28/levenshtein_hangle/
kor_begin = 44032
kor_end = 55203
chosung_base = 588
jungsung_base = 28
jaum_begin = 12593
jaum_end = 12622
moum_begin = 12623
moum_end = 12643
chosung_list = [ 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ',
'ㅅ', 'ㅆ', 'ㅇ' , 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
jungsung_list = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ',
'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ',
'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ',
'ㅡ', 'ㅢ', 'ㅣ']
jongsung_list = [
' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ',
'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ',
'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ',
'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
jaum_list = ['ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄸ', 'ㄹ',
'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ',
'ㅃ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
moum_list = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ',
'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
def compose(chosung, jungsung, jongsung):
char = chr(
kor_begin +
chosung_base * chosung_list.index(chosung) +
jungsung_base * jungsung_list.index(jungsung) +
jongsung_list.index(jongsung)
)
return char
def decompose(c):
if not character_is_korean(c):
return None
i = ord(c)
if (jaum_begin <= i <= jaum_end):
return (c, ' ', ' ')
if (moum_begin <= i <= moum_end):
return (' ', c, ' ')
# decomposition rule
i -= kor_begin
cho = i // chosung_base
jung = ( i - cho * chosung_base ) // jungsung_base
jong = ( i - cho * chosung_base - jung * jungsung_base )
return (chosung_list[cho], jungsung_list[jung], jongsung_list[jong])
def character_is_korean(c):
i = ord(c)
return ((kor_begin <= i <= kor_end) or
(jaum_begin <= i <= jaum_end) or
(moum_begin <= i <= moum_end))
def levenshtein(s1, s2, cost=None, debug=False):
if len(s1) < len(s2):
return levenshtein(s2, s1, debug=debug)
if len(s2) == 0:
return len(s1)
if cost is None:
cost = {}
# changed
def substitution_cost(c1, c2):
if c1 == c2:
return 0
return cost.get((c1, c2), 1)
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
# Changed
substitutions = previous_row[j] + substitution_cost(c1, c2)
current_row.append(min(insertions, deletions, substitutions))
if debug:
print(current_row[1:])
previous_row = current_row
return previous_row[-1]
def jamo_levenshtein(s1, s2, debug=False):
if len(s1) < len(s2):
return jamo_levenshtein(s2, s1, debug)
if len(s2) == 0:
return len(s1)
def substitution_cost(c1, c2):
if c1 == c2:
return 0
return levenshtein(decompose(c1), decompose(c2))/3
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
# Changed
substitutions = previous_row[j] + substitution_cost(c1, c2)
current_row.append(min(insertions, deletions, substitutions))
if debug:
print(['%.3f'%v for v in current_row[1:]])
previous_row = current_row
return previous_row[-1]
def test():
search_target = '큰 침대 덕분에 큰 몸을 신경쓰지 않아도 돼서 마하 군의 진자 진심교미 이이'
ex_search_article = '1996802-merge.txt'
s = json.loads(open(ex_search_article, 'r').read())
def preprocess_s(s):
sr = ''
for i in range(len(s)):
if character_is_korean(s[i]):
sr += s[i]
return sr
stp = preprocess_s(search_target)
mp = 99999
mi = ''
mpage = ''
for i in s:
if len(i['content']) == 0:
continue
for c in i['content']:
ttp = preprocess_s(c[0]).strip()
if ttp == '':
continue
l = jamo_levenshtein(stp, ttp)
if l < mp:
mp = l
mi = c
print(c)
mpage = i['page']
# print (s)
print(mi)
test()