JUST DO IT!

[TIL]KDT_20230421 ๋ณธ๋ฌธ

TIL

[TIL]KDT_20230421

sunhokimDev 2023. 4. 21. 17:03

๐Ÿ“š KDT WEEK 3 DAY 5 TIL

  • Seaborn
  • WordCloud

 


 

๐ŸŸฅ Seaborn

ํŒŒ์ด์ฌ์˜ ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, matplotlib ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

 

Lineplot๊ณผ Barplot์˜ ๊ตฌํ˜„์—๋Š” ๋‹ค์Œ์ด ํ•„์š”ํ•˜๋‹ค.

import seaborn as sns

๋ณดํ†ต sns๋กœ ๋งŽ์ด ์ค„์ธ๋‹ค๊ณ  ํ•œ๋‹ค.

 

Lineplot

ํ•œ ์ค„์ด๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

 

Barplot

๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋„ ๊ฐ„๋‹จํžˆ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , 

import matplotlib.pyplot as plt

์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ผ๋ฒจ๋„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

 

title, xlabel, ylabel ์„ ์ถ”๊ฐ€ํ•ด์ค€ ๋ชจ์Šต.
ํฌ๊ธฐ๋„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

๊ณต๊ณต์‚ฌ์ดํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ธฐ

 

์‚ฌ์ดํŠธ : https://www.weather.go.kr/w/weather/forecast/short-term.do

 

๋‹จ๊ธฐ์˜ˆ๋ณด - ๊ธฐ์ƒ์ฒญ ๋‚ ์”จ๋ˆ„๋ฆฌ

2023๋…„ 04์›” 21์ผ (๊ธˆ)์š”์ผ 11:00 ๋ฐœํ‘œ (์ด๊ด„์˜ˆ๋ณด๊ด€: ์ด์‹œ์šฐ) โ–ก (์ข…ํ•ฉ) ์˜ค๋Š˜ ์˜คํ›„~์ €๋… ์ „๋‚จ๊ถŒ ๋น„ ์กฐ๊ธˆ, ์˜ค๋Š˜๊ณผ ๋‚ด์ผ ํ•ด์•ˆ ์ค‘์‹ฌ ๊ฐ•ํ’ ์œ ์˜โ—‹ (์˜ค๋Š˜) ์ „๊ตญ ๊ฐ€๋” ๊ตฌ๋ฆ„๋งŽ๊ฒ ์œผ๋‚˜, ์ „๋ผ๊ถŒ ์˜คํ›„์— ๊ตฌ๋ฆ„๋งŽ์Œ,

www.weather.go.kr

 

์—ญ์‹œ ํ•ญ์ƒ ํ•ด์™”๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ์‚ฌ์ดํŠธ์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ.. ์–ด๋–ค ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•œ์ง€ ์กฐ์‚ฌํ•œ๋‹ค.

์ €๋ฒˆ์— ๋ช‡ ๋ฒˆํ•ด๋ดค์œผ๋‹ˆ, ๋ฐ”๋กœ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

from selenium import webdriver
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import seaborn as sns
import matplotlib.pyplot as plt

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.weather.go.kr/w/weather/forecast/short-term.do")

driver.implicitly_wait(2)

# ๋‚ ์งœ ๋ณ„ ๊ธฐ์˜จ์„ ๊ฐ€์ ธ์˜จ๋‹ค
temps = driver.find_element(By.ID, "my-tchart").text

# ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์„ ๋–ผ์–ด๋‚ด๊ณ , intํ˜• ๋ฐฐ์—ด๋กœ ์ €์žฅํ•ด์ค€๋‹ค.
tList = [int(i) for i in temps.replace("โ„ƒ","").split("\n")]

plt.ylim(min(tList) - 2, max(tList) + 2)
plt.title("Expected Temperature from now on")

# tList๋ฅผ ์•„๊นŒ ๋ฐฐ์› ๋˜ lineplot์œผ๋กœ ํ‘œํ˜„!
sns.lineplot(
    x = [i for i in range(len(tList))],
    y = tList
)

plt.show()

 

์—ฌ๊ธฐ์„œ id = "my-tchart" ์ธ ํƒœ๊ทธ๋ฅผ ๊ฐ€์ ธ์™€์„œ text๋ฅผ ๋ชจ๋‘ ์ฐ์–ด๋ƒˆ์Œ์—๋„ ์˜จ๋„๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ,

์ด๊ฑด ์‚ฌ์ดํŠธ์—์„œ ๋ณธ ํƒœ๊ทธ์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ด ๊ทธ๊ฒƒ ๋ฟ์ด๋ผ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ž„์œผ๋กœ, ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์—์„œ๋Š” ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค!

 

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ

 

์ž˜ ์ฐํ˜€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

 


 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํƒœ๊ทธ ๋นˆ๋„์ˆ˜ ์‹œ๊ฐํ™”ํ•˜๊ธฐ

 

ํ™œ์šฉํ•œ ์‚ฌ์ดํŠธ ์ฃผ์†Œ : https://qna.programmers.co.kr/

 

QnA | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ปค๋ฎค๋‹ˆํ‹ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค QnA๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œํ•ด๊ฒฐ์„ ์œ„ํ•œ QnA์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๊ด€๋ จํ•ด์„œ ๊ฐœ๋ฐœ์ž๋“ค๋ผ๋ฆฌ ๊ถ๊ธˆํ•œ๊ฑด ๋ฌผ์–ด๋ณด๊ณ  ์•„๋Š”๊ฑด ํ•จ๊ป˜ ๋‚˜๋ˆ ์š”. C, Java, Python, Ruby๋“ฑ์˜ ์ฝ”๋“œ๋ฅผ ์›น์—์„œ ์ง์ ‘ ์‹คํ–‰

qna.programmers.co.kr

 

์ด๋ฒˆ์—๋Š” ์‚ฌ์ดํŠธ ์•ˆ์˜ ์งˆ๋ฌธ ํƒœ๊ทธ๋ฅผ ๋ชจ์•„ ๋นˆ๋„์ˆ˜๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋Š” ์‹ค์Šต์„ ํ•ด๋ณด์•˜๋‹ค.

 

from bs4 import BeautifulSoup
import requests
import time

user_agent = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
frequency = {} # "ํƒœ๊ทธ์ด๋ฆ„" : "๋นˆ๋„์ˆ˜" ๋กœ ํ™œ์šฉ

# 1 ~ 10ํŽ˜์ด์ง€์•ˆ์—์„œ ํƒ์ƒ‰
for i in range(1,11):

    res = requests.get("https://qna.programmers.co.kr/?page={}".format(i), user_agent)
    soup = BeautifulSoup(res.text, "html.parser")

    ul_tags = soup.find_all("ul", "question-tags")

    for ul in ul_tags:
        li_tags = ul.find_all("li")
        for li in li_tags:
            tag = li.text.replace("\n","")
            
            # frequency์— ํƒœ๊ทธ ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€์•Š์œผ๋ฉด 1, ์กด์žฌํ•˜๋ฉด ๊ทธ ์ˆซ์ž์— + 1
            frequency[tag] = frequency.get(tag,0) + 1

    time.sleep(0.5)

print(frequency)

์งˆ๋ฌธ ํƒœ๊ทธ๊ฐ€ ul์— question-tags๋กœ ์กด์žฌํ•œ๋‹ค๋Š” ๊ฑธ ํŒŒ์•…ํ•˜๊ณ , frequency์— ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

์ฝ”๋“œ์˜ ์ดํ•ด์— ๋ช‡ ๊ฐ€์ง€ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ ์–ด๋ณด๋ฉด,

  • ul > li > text โžก ํƒœ๊ทธ
  • tag์•ˆ์— \n ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ ๋ฌธ๊ตฌ๊ฐ€ ๋งŽ์•„ replace๋กœ ์—†์• ๋‘๊ณ , ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋‚จ๊ฒจ๋‘์—ˆ๋‹ค.
  • ์‚ฌ์ดํŠธ ๊ณผ๋ถ€ํ•˜ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด time.sleep์„ ์ ์šฉํ•˜์˜€๋‹ค.

๊ฒฐ๊ณผ๋Š”..

์—„~์ฒญ ๊ธธ์—ˆ๋‹ค

์ด ์ค‘์—์„œ ๋นˆ๋„ ์ˆ˜๋ณ„๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•˜๊ณ  ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋กœ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.

 

# ์œ„ ์ฝ”๋“œ์—์„œ ์ด์–ด์ง„๋‹ค.

from collections import Counter
import seaborn as sns
import matplotlib.pyplot as plt

counter  = Counter(frequency)
commonList = counter.most_common(10) 

# commonList๊ฐ€ (์ด๋ฆ„, ๋นˆ๋„์ˆ˜)์˜ ์ด์ค‘ ๋ฐฐ์—ด ํ˜•ํƒœ์ด๋ฏ€๋กœ x, y๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋ˆˆ๋‹ค
x = [e[0] for e in commonList]
y = [e[1] for e in commonList]

plt.figure(figsize=(15,5))
plt.title("Frequency of question in Hashcode")
plt.xlabel("Tag")
plt.ylabel("Frequency")
sns.barplot(x=x, y=y)

plt.show()

collections์˜ Counter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด value์˜ ์ˆซ์ž๊ฐ€ ๋†’์€ ๊ฒƒ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

commonList = counter.most_common(10)์€ ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ 10๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

 

figure๋กœ ํฌ๊ธฐ ์กฐ์ ˆ์ด ์—†์œผ๋ฉด, x์˜ ์ด๋ฆ„์ด ์„œ๋กœ ๊ฒน์ณ๋ณด์ธ๋‹ค.

 

๊น”๋”ํ•˜๊ฒŒ ์ž˜ ๋‚˜์˜จ๋‹ค.

 


 

๐ŸŸฆ Wordcloud

ํŒŒ์ด์ฌ์˜ ํ…์ŠคํŠธ ํด๋ผ์šฐ๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ํ…์ŠคํŠธ ๊ตฌ๋ฆ„์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค

 

์ง„ํ–‰ํ•˜๊ธฐ์ „์— pip install wordcloud, pip install konlpy, java๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์œˆ๋„์šฐ์˜ ๊ฒฝ์šฐ ์„ค์น˜ํ›„ JAVA_HOME์˜ Path ๊ฒฝ๋กœ๋„ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•ด์ค˜์•ผ wordcloud๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

์ฐธ๊ณ ํ–ˆ๋˜ ๋ธ”๋กœ๊ทธ : https://default-womyn.tistory.com/entry/konlpy-Okt-%EC%98%A4%EB%A5%98-No-JVM-shared-library-file-jvmdll-found

 

WordCloud๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•œ๋‹ค

  1. KoNLPy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ „์ฒ˜๋ฆฌ
  2. Counter๋ฅผ ์ด์šฉํ•ด ๋นˆ๋„์ˆ˜ ์ธก์ •
  3. WordCloud๋ฅผ ์ด์šฉํ•ด ์‹œ๊ฐํ™”

WordCloud๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์š”์†Œ..

๋”๋ณด๊ธฐ

# ์‹œ๊ฐํ™”์— ์“ฐ์ด๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
from collections import Counter

# ๋ฌธ์žฅ์—์„œ ๋ช…์‚ฌ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
from konlpy.tag import Hannanum

#WordCloud์— ์‚ฌ์šฉํ•  ์• ๊ตญ๊ฐ€ ๊ฐ€์‚ฌ
national_anthem = """
๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก
ํ•˜๋Š๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ ๋งŒ์„ธ
๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ
๋Œ€ํ•œ ์‚ฌ๋žŒ ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ
๋‚จ์‚ฐ ์œ„์— ์ € ์†Œ๋‚˜๋ฌด ์ฒ ๊ฐ‘์„ ๋‘๋ฅธ ๋“ฏ
๋ฐ”๋žŒ ์„œ๋ฆฌ ๋ถˆ๋ณ€ํ•จ์€ ์šฐ๋ฆฌ ๊ธฐ์ƒ์ผ์„ธ
๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ
๋Œ€ํ•œ ์‚ฌ๋žŒ ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ
๊ฐ€์„ ํ•˜๋Š˜ ๊ณตํ™œํ•œ๋ฐ ๋†’๊ณ  ๊ตฌ๋ฆ„ ์—†์ด
๋ฐ์€ ๋‹ฌ์€ ์šฐ๋ฆฌ ๊ฐ€์Šด ์ผํŽธ๋‹จ์‹ฌ์ผ์„ธ
๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ
๋Œ€ํ•œ ์‚ฌ๋žŒ ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ
์ด ๊ธฐ์ƒ๊ณผ ์ด ๋ง˜์œผ๋กœ ์ถฉ์„ฑ์„ ๋‹คํ•˜์—ฌ
๊ดด๋กœ์šฐ๋‚˜ ์ฆ๊ฑฐ์šฐ๋‚˜ ๋‚˜๋ผ ์‚ฌ๋ž‘ํ•˜์„ธ
๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ
๋Œ€ํ•œ ์‚ฌ๋žŒ ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ
"""

 

์• ๊ตญ๊ฐ€ ๊ฐ€์‚ฌ์•ˆ์—์„œ ํ˜•ํƒœ์†Œ ๋ถ„์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Hannanum์„ ํ†ตํ•ด ๋ช…์‚ฌ๋ฅผ ์ถ”์ถœํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

hannanum = Hannanum()
nouns = hannanum.nouns(national_anthem)
words = [noun for noun in nouns if len(noun) > 1] #์ด, ํ™”, ๋“ฏ ๋“ฑ ํ•œ ๊ธ€์ž ๋‹จ์–ด๋Š” ์ œ์™ธ

 

์ด๋ ‡๊ฒŒํ•˜๋ฉด words์— ๋ฐฐ์—ดํ˜•ํƒœ๋กœ ๋™ํ•ด๋ฌผ, ๋ฐฑ๋‘์‚ฐ, ํ•˜๋Š๋‹˜๊ฐ™์€ ๋‹จ์–ด๋“ค์ด ๋‹ด๊ธฐ๊ฒŒ ๋œ๋‹ค.

 

์—ฌ๊ธฐ์„œ counter๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด๋‘๊ณ , wordCloud๋กœ ํ‘œํ˜„ํ•œ๋‹ค!

import matplotlib as mpl
from matplotlib import font_manager, rc

counter = Counter(words) # ๋‹จ์–ด ์ˆ˜ ์„ธ๊ธฐ

font_path = r"C:\Users\asd\Desktop\malgun.ttf" # ํ•œ๊ธ€ ๊นจ์ง ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ํฐํŠธ ์ถ”๊ฐ€
wordcolud = WordCloud(font_path= font_path) # ํฐํŠธ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ๊ณ  wordCloud ๋™์ž‘!

img = wordcolud.generate_from_frequencies(counter)
plt.imshow(img)

๋ณธ๋ž˜ ํ•œ๊ธ€์ด ๋‚˜์˜ค์ง€ ์•Š์œผ๋‹ˆ ํฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ฉด..

์ž˜ ๋™์ž‘ํ•œ๋‹ค!

๋ฐฐ๊ฒฝํ™”๋ฉด ์ƒ‰๊น”๋„ WordCloud ๋ฉ”์†Œ๋“œ์— background ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

 

+ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์งˆ๋ฌธ์˜ ๋‹จ์–ด ๋นˆ๋„์ˆ˜๋กœ WordColud ๋งŒ๋“ค๊ธฐ

๋”๋ณด๊ธฐ

import time
import requests
from bs4 import BeautifulSoup

#์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from collections import Counter
from konlpy.tag import Hannanum

user_agent = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
questions = [] # ์งˆ๋ฌธ๋“ค์ด ๋‹ด๊ธธ ๋ฐฐ์—ด

# 1~5ํŽ˜์ด์ง€ ํƒ์ƒ‰
for i in range(1, 6):
    res = requests.get("https://hashcode.co.kr/?page={}".format(i), {"User-Agent": user_agent})
    soup = BeautifulSoup(res.text, "html.parser")

    parsed_datas = soup.find_all("li", "question-list-item")

    for data in parsed_datas:
        questions.append(data.h4.text.strip())

    time.sleep(0.5)
    
# ์œ„์—์„œ ๋งŒ๋“  questions ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ WordCloud ๋งŒ๋“ค๊ธฐ
words = []
hannanum = Hannanum()

# questions ๋ฐฐ์—ด์•ˆ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ words ๋ฐฐ์—ด์— ์ถ”๊ฐ€
for question in questions:
    nouns = hannanum.nouns(question)
    words += nouns
    
counter = Counter(words) # ๋‹จ์–ด์ด๋ฆ„ : ๊ฐœ์ˆ˜ ๋งŒ๋“ค๊ธฐ
wordcloud = WordCloud(
    font_path= r"C:/Users/asd/Desktop/malgun.ttf",
    background_color="white"
)

img = wordcloud.generate_from_frequencies(counter)

plt.imshow(img)

 

๊ฒฐ๊ณผ



 

๐Ÿค” ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์–ด๋ ค์› ๋˜ ๋‚ด์šฉ

์—ฌ๋Ÿฌ ๊ตฐ๋ฐ์—์„œ ๋ถ€๋”ช์ณค๋‹ค..

์œˆ๋„์šฐ๋ผ์„œ ๊ทธ๋Ÿฐ์ง€ Path ์„ค์ •์—๋„ ๊ผฌ์ด๊ณ , ํ•œ๊ธ€ ์ถœ๋ ฅ์—๋„ ๊ผฌ์ด๊ณ ..

๊ฐ•์˜ ๋“ฃ๋Š” ์‹œ๊ฐ„๋ณด๋‹ค ์ด๊ฑธ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์„ ๋” ๋งŽ์ด ์“ด๊ฒƒ๊ฐ™๋‹คใ…‹ใ…‹ใ…‹

๊ฒฐ๊ตญ ๋‘˜๋‹ค ํ•ด๊ฒฐ๋ฒ•์€ ๊ฐ„๋‹จํ–ˆ๋‹ค.. ๋‚ด๊ฐ€ ์ž๊พธ ํ—ค๋ฉ”์—ˆ์„ ๋ฟใ… 

๊ทธ๋ž˜๋„ ์˜๋ฏธ์žˆ๋Š” ์ง€ํ‘œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋˜ ์‹œ๊ฐ„์ด๋ผ ์žฌ๋ฐŒ์—ˆ๋‹ค! ๐Ÿคญ

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TIL]KDT_20230425  (0) 2023.04.25
[TIL]KDT_20230424  (0) 2023.04.24
[TIL]KDT_20230420  (0) 2023.04.20
[TIL]KDT_20230419  (0) 2023.04.19
[TIL]KDT_20230418  (1) 2023.04.18