-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLP.py
160 lines (103 loc) · 2.95 KB
/
NLP.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import konlpy
import pandas as pd
import re
import csv
from konlpy.tag import Kkma
from konlpy.tag import Okt
from konlpy.utils import pprint
from konlpy.tag import Hannanum
from collections import Counter
kkma = Kkma()
hannanum = Hannanum()
# In[2]:
kkmaRes = []
oktRes = []
with open('allSong.csv') as data:
csv_reader = csv.reader(data)
next(csv_reader) # 헤더 읽음 처리
for song in csv_reader:
for line in song[2].split("\n"):
# pprint(kkma.pos(line))
if len(line.strip())==0:
continue
print(line)
# pprint(kkma.morphs(line))
# pprint(okt.morphs(line))
# kkmaRes.extend(kkma.morphs(line))
# oktRes.extend(okt.morphs(line))
# pprint(hannanum.nouns(line))
# pprint(hannanum.pos(line))
break
# In[3]:
kVocab = Counter(kkmaRes)
print(kVocab)
# In[4]:
oVocab = Counter(oktRes)
print(oVocab)
# In[14]:
dic = {}
regex = re.compile('[0-9]') # 가다01 가다02 이런식으로 되어있는 것들 제거하기 위함
with open('words.csv') as data:
csv_reader = csv.reader(data)
next(csv_reader) # 헤더 읽음 처리
for word_list in csv_reader:
word = word_list[1]
num = regex.search(word)
if num : #num이 들어가있으면 제거
word = word[0:num.start()]
dic[word] = word_list[4]
print(word_list[4])
# In[9]:
song_list = {}
song_lyrics = {}
kor_rex = re.compile('[가-힣]+') #한글만 볼 것
from termcolor import colored
okt = Okt()
with open('allSong.csv') as data:
csv_reader = csv.reader(data)
next(csv_reader) # 헤더 읽음 처리
for song in csv_reader:
words = []
lyric = []
for line in kkma.sentences(song[2]):
# pprint(kkma.pos(line))
for word in okt.morphs(line):
if bool(re.search(kor_rex, word)):
lyric.append(word)
if word in dic:
words.append(word)
print(colored(word,'red'), end=' ')
continue
print(word, end=' ')
print()
song_lyrics[song[0]+"-"+song[1]] = lyric
song_list[song[0]+"-"+song[1]] = words
# In[10]:
for k in song_list:
print(k)
for word in song_list[k]:
if word in dic:
print(word, end=' ')
print()
# In[11]:
stopwords = []
with open('stopwords.csv') as data:
csv_reader = csv.reader(data)
next(csv_reader)
for stopword in csv_reader:
stopwords.extend(stopword)
print(stopword)
# In[12]:
print(stopwords)
# In[13]:
from sklearn.feature_extraction.text import CountVectorizer
vect = CountVectorizer(stop_words = stopwords)
# In[ ]:
for l in song_lyrics:
print()
vect.fit_transform(song_lyrics[l])
print(vect.vocabulary_)
# In[ ]: