'UTF-8'로저장
!python -m pip install konlpy
import konlpy
print('KoNLPy version...:', konlpy.__version__)
# 한국어 텍스트 분석에 필요한 모듈(konlpy)의 Open Korean Text 형태소 분석기를 불러온다.
from konlpy.tag import Okt
# Open Korean Text 형태소 분석기 객체를 생성한다.
okt = Okt()
text = open("/content/drive/MyDrive/colab/04_워드클라우드/2022.7. 워드클라우드/education.txt",'rt', encoding='UTF-8').read()
line =[]
line = okt.pos(text) # 각 품사를 태깅하는 역할.
# 주어진 텍스트를 형태소 단위로 나누고, 나눠진 각 형태소를 그에 해당하는 품사와 함께 리스트화 하는 것을 의미한다.
print(line)
morphs =[]
# 명사 또는 형용사인 단어만 morphs에 넣어주기
for word, tag in line:
if tag in ['Noun','Adjective']:
morphs.append(word)
print(morphs)
#제외할 단어 추가
stop_words = "시행 제 호 일부 개정 장 총칙 이 법 관 및 등 그 정 법률 함" #추가할 때 띄어쓰기로 추가해주기
stop_words = set(stop_words.split(' '))
# 불용어를 제외한 단어만 남기기
kor_text = [word for word in morphs if not word in stop_words]
print(kor_text)
from collections import Counter
counts = Counter(kor_text)
print(counts)
# 강제로 텍스트 넣기 강제추가 하지 않으려면 그냥 넘어가세요.
for i in range(100) :
kor_text.append('화이팅!!')
counts = Counter(kor_text)
print(counts)
#가장 많이 나온 단어 100개 저장
finish = counts.most_common(100)
print(finish)
그림서식 > 밝기 -100%
그림서식 > 단색 채우기(힌색)
그림으로 저장 > .PNG로 저장
# 이미지 추가(워드크라우드 모양 설정)
from PIL import Image # 워드클라우드를 원하는 형태로 그리기 위해 그림을 불러오느 패키지
import numpy as np # 불러온 그림을 배열로 나타내어 쉽게 처리할 수 있도록 도와주는 패키지
image = Image.open('/content/drive/MyDrive/colab/04_워드클라우드/2022.7. 워드클라우드/mask(학교).png')
img_array = np.array(image)\
from wordcloud import WordCloud
###WordCloud(워드크라우드) 만들기###
#폰트지정
font = '/content/drive/MyDrive/colab/04_워드클라우드/2022.7. 워드클라우드/Katuri.ttf'
word_cloud = WordCloud(font_path=font, width=1500, height=500, mask=img_array,
max_words=200, min_font_size=5, random_state=30, background_color='black', max_font_size=150,
colormap = "Accent")
# background_color='white' 'black'
# colormap = "Accent" "spring", "summer", "seismic","PuBu", ....
# counter에 있는 명사의 빈도수에 따라 워드클라우드를 생성하기
word_cloud.generate_from_frequencies(dict(finish)) #딕셔너리 타입을 받는다.
import matplotlib.pyplot as plt # 생성한 워드클라우드 데이터를 시각화하여 그리기 위해 불러옵니다.
plt.figure(figsize=(10, 10)) # width, height in inches
plt.imshow(word_cloud)
plt.axis('off')
plt.show()
# colormap = ['Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spectral', 'spectral_r', 'spring', 'spring_r', 'summer', 'summer_r', 'terrain', 'terrain_r', 'viridis', 'viridis_r', 'winter', 'winter_r']