forked from robbiebarrat/rapping-neural-network
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_dict.py
More file actions
89 lines (72 loc) · 3.12 KB
/
Copy pathmake_dict.py
File metadata and controls
89 lines (72 loc) · 3.12 KB
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
import codecs
import re
import datetime
from pronouncing_kr import sorting_rhyme
# 유니코드 한글 시작 : 44032, 끝 : 55199
BASE_CODE, CHOSUNG, JUNGSUNG = 44032, 588, 28
# 초성 리스트. 00 ~ 18
CHOSUNG_LIST = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
# 중성 리스트. 00 ~ 20
JUNGSUNG_LIST = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
# 종성 리스트. 00 ~ 27 + 1(1개 없음)
JONGSUNG_LIST = [' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
def make_dict() :
start = datetime.datetime.now()
print("사전 만드는 중... ( "+str(start)+" )")
input_file = "lyrics.txt"
output_file = "korean.dict"
fp = codecs.open(input_file, 'r', encoding='utf-8')
wp = codecs.open(output_file,"w",encoding='utf-8')
text = fp.read()
text = text.replace('\ufeff','')
text = text.strip()
lines = text.split("\n")
word_list = []
while "\r" in lines:
lines.remove("\r")
for line in lines:
line = line.replace(' \r','')
line = line.replace('.','')
line = line.replace(',', '')
line = line.replace('?', '')
line = line.replace('!','')
line = line.replace('~', '')
# => 한문장으로는 안되는지...
inputs = line.split(" ")
for input in inputs:
if re.match('.*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*', input[:1]) is None:
continue
charac = ()
test_keyword = input
split_keyword_list = list(test_keyword)
#print(split_keyword_list)
cha = []
for keyword in split_keyword_list:
print(keyword)
# 한글 여부 check 후 분리
if re.match('.*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*', keyword) is not None:
char_code = ord(keyword) - BASE_CODE
char1 = int(char_code / CHOSUNG)
char2 = int((char_code - (CHOSUNG * char1)) / JUNGSUNG)
char3 = int((char_code - (CHOSUNG * char1) - (JUNGSUNG * char2)))
if JONGSUNG_LIST[char3] == ' ':
cha.append(CHOSUNG_LIST[char1] + JUNGSUNG_LIST[char2] + "P")
else:
cha.append(CHOSUNG_LIST[char1]+JUNGSUNG_LIST[char2]+JONGSUNG_LIST[char3])
input = input.replace(" ","")
input = input.replace("\r", "")
input = input+" "+" ".join(cha)
word_list.append(input)
## 중복된 값 없애기
word_list = set(word_list)
word_list = tuple(word_list)
word_list = list(word_list)
wp.write("\n".join(word_list))
fp.close()
wp.close()
end = datetime.datetime.now()
print("사전 구축 완료 ( " + str(end) + " )")
print("===> 총 걸린 시간 : "+str(end-start))
def main():
make_dict()
main()