[Python] 워드 클라우드(word cloud) 만들기

2023. 1. 3.·🤓 기술 학습 & 공부 기록/Python

📍개요

Take Me Home, Country Roads 노래 가사를 워드 클라우드로 만들어 어떤 단어가 자주 등장하는지 알아보기

 

📍전체 코드

import wordcloud
from matplotlib import pyplot as plt

sample_text = """Almost heaven, West Virginia
Blue Ridge Mountains, Shenandoah River
Life is old there, older than the trees
Younger than the mountains, growin' like a breeze
Country roads, take me home
To the place I belong
West Virginia, mountain mama
Take me home, country roads
All my memories gather 'round her
Miner's lady, stranger to blue water
Dark and dusty, painted on the sky
Misty taste of moonshine, teardrop in my eye
Country roads, take me home
To the place I belong
West Virginia, mountain mama
Take me home, country roads
I hear her voice in the mornin' hour, she calls me
The radio reminds me of my home far away
Drivin' down the road, I get a feelin'
That I should've been home yesterday, yesterday
Country roads, take me home
To the place I belong
West Virginia, mountain mama
Take me home, country roads
Country roads, take me home
To the place I belong
West Virginia, mountain mama
Take me home, country roads
Take me home, (down) country roads
Take me home, (down) country roads
"""


def calculate_frequencies(text):
    # 필터링할 문장 부호
    punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~\\'''
    # 필터링할 의미없는 명사
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", "we",
                           "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its",
                           "they", "them", "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was",
                           "were", "be", "been", "being", "have", "has", "had", "do", "does", "did", "but", "at", "by",
                           "with", "from", "here", "when", "where", "how", "all", "any", "both", "each", "few", "more",
                           "some", "such", "no", "nor",
                           "too", "very", "can", "will", "just"]
    result = {}
    word_arr = text.split()
    for word in word_arr:
        new_word = word.lower()
        for char in new_word:
            if char in punctuations:
                new_word = new_word.replace(char, "")

        if new_word in uninteresting_words:
            continue

        if new_word not in result:
            result[new_word] = 1
        else:
            result[new_word] = result[new_word] + 1

    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies(result)
    plt.imshow(cloud.to_array(), interpolation='nearest')
    plt.axis('off')
    plt.show()
    # cloud.to_file("myfile.jpg")


calculate_frequencies(sample_text)

 

📍결과

'🤓 기술 학습 & 공부 기록/Python' 카테고리의 다른 글
  • [Python] sys.stdin.readline() 으로 빠르게 입력받기
  • [Python] 10진수를 2진수로 바꾸기
  • [Python] class가 원소인 배열 데이터 처리
  • [Python] 정렬(sort, sorted) 차이
지식물원
지식물원
지식이 자라는 식물원!
  • 지식물원
    지식물원
    지식물원
  • 전체
    오늘
    어제
    • 분류 전체보기 (516)
      • 🎨 프론트엔드 공부 (253)
        • JS & TS (92)
        • HTML & CSS (22)
        • React & Next (49)
        • Vue & Nuxt (22)
        • 기타 (68)
      • 🤓 기술 학습 & 공부 기록 (116)
        • Node.js (0)
        • Python (37)
        • 백엔드 (0)
        • 딥러닝 (1)
        • 컴퓨터 일반 (72)
        • 개발 인프라 (6)
      • 👨‍💻 프로젝트 경험 (6)
        • Work (0)
        • Toy (6)
      • ⚙️ 개발 팁 & 노하우 (21)
        • 프론트엔드 (6)
        • 기타 (15)
      • ☕️ 커리어 & 인터뷰 준비 (88)
        • 코딩 테스트 (88)
      • 📰 기술 트렌드 & 생각 정리 (4)
      • 📚 기타 (25)
        • 마케팅 (15)
        • 비개발서적 (10)
  • 블로그 메뉴

    • 태그
  • 링크

  • 공지사항

    • 모바일 접속 시 코드 하이라이팅 깨질 때
  • 인기 글

  • hELLO· Designed By정상우.v4.10.3
지식물원
[Python] 워드 클라우드(word cloud) 만들기
상단으로

티스토리툴바