# 워드 클라우드 설치

conda install -c conda-forge wordcloud

# 워드 클라우드 라이브러리 가져오기 

import numpy as np
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 텍스트 불러와서 줄바꿈 없애기

text = open('./이승환 6집.txt')
text = text.read()
text = text.replace('\n', " ")
text

 

# 여러개의 파일 불러와서 붙이기

result = " "

for number in range(1,3):
    index = '{:01}'.format(number)
    filename = "이승환" + index + "집" + ".txt"
    text = open('./'+filename, 'r', encoding='utf-8-sig')
    result += text.read().replace("\n", " ")
result

# 정규식으로 만들기

import re

pattern = '[^\w\s]'
text = re.sub(pattern=pattern, repl='', string=result)
text 

#한글 설정(워드 클라우드 만들기)

import matplotlib.font_manager as fm

for f in fm.fontManager.ttflist:
    if 'Gothic' in f.name:
        print(f.fname)

font_path = '/System/Library/Fonts/Supplemental/AppleGothic.ttf'

wc = WordCloud(font_path = font_path, background_color = "white")
wc.generate(text)

plt.figure(figsize=(50,50))
plt.axis("off")
plt.imshow(wc)
plt.show()

+ Recent posts