- import
from wordcloud import WordCloud
- text 워드클라우드
import matplotlib.pyplot as plt
from collections import Counter
# wordcloud로 시각화할 단어들
text = "파이썬 명지대학교 워드클라우드 ICT 파이썬 명지대학교 정보통신공학과 정보통신 워드클라우드 파이썬 라이브러리 ICT 파이썬 워드클라우드 ICT 명지대학교 워드클라우드 명지대학교 데이터 분석 워드클라우드 "
# 단어들을 ' '에 따라 토큰화
split_text = text.split()
# Counter로 단어들 빈도 세기
word_count = Counter(split_text)
# AppleGothic 폰트를 사용하기 위해 폰트 경로를 지정합니다.
wordcloud = WordCloud(font_path='AppleGothic.ttf', background_color='white').generate(text)
# 워드클라우드 시각화
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
# plt.imshow(wordcloud, interpolation='lanczos') 이미지의 부드럽기 정도
plt.axis('off') # 축 숨기기
plt.show()
- stopwords
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 영어의 stopwords들이 들어가있는 형태
stopwords = set(STOPWORDS)
# stopwords 추가
stopwords.add('워드클라우드')
stopwords.add('ICT')
stopwords.add('분석')
#wordcloud = WordCloud(font_path='C:\Windows\Fonts\\HMFMMUEX.ttc',stopwords=stopwords,background_color='white').generate(text)
wordcloud = WordCloud(font_path='AppleGothic.ttf',stopwords=stopwords, background_color='white').generate(text)
plt.figure(figsize=(11,11)) #이미지 사이즈 지정
plt.imshow(wordcloud, interpolation='lanczos') #이미지의 부드럽기 정도
plt.axis('off') #x y 축 숫자 제거
plt.show()
- 파일 불러들여서 wordcloud 하기
# 파일을 text로 불러들이기
text = open('my_path/constitution.txt').read()
#wordcloud변수에 WordCloud().generate()함수를 이용하여 저장
wordcloud = WordCloud().generate(text)
# 시각화
plt.figure(figsize=(12,12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show
-BeautifulSoup 크롤링 이용하여 원하는 값 크롤링 하기
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from collections import Counter
import requests
import numpy as np
from bs4 import BeautifulSoup
from PIL import Image
# 네이버 검색창 url
query ="명지대"
url = "https://m.search.naver.com/search.naver?where=m_news&sm=mtb_jum&query="+query
# header 설정 및 html 받아오기
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
html = requests.get(url,headers=headers)
# Send a GET request to the URL
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# class 부분에 원하고자 하는 부분을 선택하여 해당 클래스의 test를 크롤링 한다.
title_text = soup.find_all(class_='news_dsc')
concatStr=''
# Extract and print the text inside the anchor tags within the dsc_area class
for dsc_area_element in title_text:
title_text = dsc_area_element.text
concatStr += dsc_area_element.text.strip()
print(concatStr)
wc = WordCloud(font_path='AppleGothic.ttf',background_color='white',max_words=2000)
wc = wc.generate(concatStr)
plt.figure(figsize=(11,11)) #이미지 사이즈 지정
plt.imshow(wc)
plt.axis('off') #x y 축 숫자 제거
plt.show()
import numpy as np
from PIL import Image
#text = open('./data/alice.txt').read()
alice_coloring= np.array(Image.open('mypath/alice_color.png'))
stopword=set(STOPWORDS)
stopword.add('said')
wc = WordCloud(background_color='white',max_words=2000,mask=alice_coloring, stopwords= stopwords)
wc = wc.generate(text)
# 이부분이 컬러 추가하는 부분
from wordcloud import ImageColorGenerator
image_colors=ImageColorGenerator(alice_coloring)
plt.figure(figsize=(12,12))
plt.imshow(wc.recolor(color_func = image_colors), interpolation='bilinear')
plt.axis('off')
plt.show()
- mask를 씌우고 싶다면
cloud_mask= np.array(Image.open('mypath/cloud.png'))
# mask 매개변수 추가
wc = WordCloud(font_path='AppleGothic.ttf', background_color='white',max_words=2000,mask=cloud_mask)
wc = wc.generate(concatStr)
plt.figure(figsize=(11,11)) #이미지 사이즈 지정
plt.imshow(wc)
plt.axis('off') #x y 축 숫자 제거
plt.show()
- 해당 이미지의 coloring까지 하고싶다면
import numpy as np
from PIL import Image
#text = open('./data/alice.txt').read()
alice_coloring= np.array(Image.open('mypath/alice_color.png'))
stopword=set(STOPWORDS)
stopword.add('said')
wc = WordCloud(background_color='white',max_words=2000,mask=alice_coloring, stopwords= stopwords)
wc = wc.generate(text)
# 컬러 추가하는 부분
from wordcloud import ImageColorGenerator
image_colors=ImageColorGenerator(alice_coloring)
plt.figure(figsize=(12,12))
plt.imshow(wc.recolor(color_func = image_colors), interpolation='bilinear')
plt.axis('off')
plt.show()