본문 바로가기
프로그래밍/Python

Python(파이썬) konlpy의 워드클라우드 불필요한 명사 제거(원리만)

by LiveData 2018. 12. 1.
반응형



전 포스팅


Python(파이썬) 특정 단어포함하는 신문기사 웹 크롤러 만들기(Web Cralwer) -1

Python(파이썬) 특정 단어포함하는 신문기사 웹 크롤러 만들기(Web Cralwer) -2

Python(파이썬)크롤링 한 파일에 불필요한 문자 제거(Web Cralwer) -3

Python(파이썬) 명사 분리 추출 후, 단어 사용 빈도 계산기(Web Cralwer)

Python(파이썬) 추출한 명사 빈도를 그림으로 시각화하기(Web Cralwer)





전 시간에 크롤링하여 문자로 되어있는 글들을


명사들만 추출하여 빈도수를 계산하고


konlpy의 워드 클라우드형태로 만들어보았습니다.





하지만 너무 불필요한 명사들이 많이 들어있었습니다.


"것", "안", "홍", "날", "분" 등등..


이건 konlpy가 정말 명사만 다 추출하는 것 같네요.





따로 한글 형태소 모듈이 없는이상 


강제로 빼주는 방법도 한 방법일 것 같네요


(아마 찾아보면 있을 것 같은데 다른거 하느라 ㅠ 

아시는 분은 댓글로 달아주세요!!)




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#! /usr/bin/python2.7
# -*- coding: utf-8 -*-
 
from collections import Counter
import urllib
import random
import webbrowser
 
from konlpy.tag import Twitter
from lxml import html
import pytagcloud # requires Korean font support
import sys
 
= lambda: random.randint(0,255)
# 글씨의 랜덤색깔
color = lambda: (r(), r(), r())
 
def get_tags(text, ntags=50, multiplier=3): #전에 했던 명사 탐색
    spliter = Twitter()
    nouns = spliter.nouns(text)
    count = Counter(nouns)
    
    value_to_remove = "것" #삭제할 명사
    
    return [{ 'color': color(), 'tag': n, 'size': (c*multiplier)/2 }\
                for n, c in count.most_common(ntags) if n != value_to_remove]
                               #삭제할 명사를 포함시키지 않는 dictionary List
def draw_cloud(tags, filename, fontname='Noto Sans CJK', size=(800600)):
    pytagcloud.create_tag_image(tags, filename, fontname=fontname, size=size)
    webbrowser.open(filename)
#그림 그리기
 
def main():
    text_file=open("out_cleand.txt",'r')
    text=text_file.read()
    tags = get_tags(text)
    draw_cloud(tags, 'wordcloud.png')
 
    text_file.close()
 
if __name__=="__main__":
    main()
 
cs




전 시간에 했던 코드에서 


중간에 value_to_remove 의 변수에 삭제할 변수를 넣은 후


tags dictionary List를 만들 때 포함 시키지 않는 원리입니다.





"것"이라는 글자가 포함되지 않는 걸 확인하실 수 있습니다.





python문법을 까먹어서... 


tags를 만들 때 포함시키지 않을 문자 리스트를 만들고


함수를 만들어 tags를 만들때 문자 리스트를


자동으로 빼는 함수를 구현하면 좋을 것 같네요.






다른 방법이나 좋은 코드 로직이 있으시면 댓글좀 달아주세요 ㅠ


반응형