티스토리 뷰

#비정형 데이터

  • 비정형 데이터- 구조화 되지 않은 데이터
  • 텍스트(웹사이트 글, 신문기사 등), 이미지, 음성 데이터

데이터의 유형

#1. 데이터 수집

  • 빅카인즈(https://www.bigkinds.or.kr) 
  • 한국언론진흥재단에서 관리하는 신문기사 빅데이터 서비스 플랫폼
  • 우리나라 신문 기사들로부터 주요인물, 주요 키워드, 주요 이슈 등을 볼 수 있음.

 

실습 : 코로나 관련 뉴스 검색

데이터불러오기

?readlines()
data <- readLines("covid19_news_202001.csv")
str(data)
class(data)
data[2] #벡터타입 인덱싱
head(data)
2. 데이터 정제하기
data<-data[-1]

?gsub()
data <- gsub(pattern = ",", replacement = " ", x=data)

#특정문자 삭제
data<-gsub(pattern = "[[:digit:]]",replacement =" ", x = data)
data<-gsub(pattern = "[[:upper:]]",replacement =" ", x = data)

3.텍스트 정형화 하기

 
#tm 패키지 사용 (p300) 텍스트 분석을 위해 코퍼스라는 문서 집합을 다룸
#비정형 텍스트 데이터를 정형화된 구조로 변환시키는 함수 제공함
install.packages("tm")
library(tm)

getSources() #문서에 따른 데이터불러오기
#현재 텍스트가 벡터형이기에 VectorSource()함수사용
docs <- Corpus(VectorSource(data))

#문서접근 [[]] 인덱싱 이용

doc[[1]]$content
#첫번째 content보기

doc[[2]]$content

#문서-단어 행렬로 변환 (p302)

dtm <- DocumentTermMatrix(docs)
inspect(dtm[1:5,1:10])
#처음~5개 행, 1~10개 열로 출력력
#문서 집합에서 등장한 단어의 빈도수를 합산함
terms <- colSums(as.matrix(dtm))
terms <-terms[order(terms, decreasing = TRUE)]
terms[1:10]
#문서 집합 내에 등장 빈도수가 상위 200위 출력
terms.freq <- terms[1:200]
#한글자 단어 삭제
terms.freq <-terms.freq[!(nchar(names(terms.freq))==1)] 

3. 데이터 시각화

wordcloud2패키지는 단어와 빈도를 담은 데이터프레임을 기반으로 워드클라우드를 표현해주는 패키지

install.packages("wordcloud2")
library(wordcloud2)

?wordcloud2

간단예제

wc <- data.frame(words = names(terms), freq = terms)

wordcloud2(wc)
wordcloud2(wc, size = 0.7) #사이즈
wordcloud2(wc, size = 0.5, shape = "star") #사이즈, shape
# 새 데이터프레임 생성(첫열 이름, 2열 빈도수)(
terms.freq.new <- data.frame(terms=names(terms.freq),freq=terms.freq)
rownames(terms.freq.new)<-c(1:nrow(terms.freq.new))

View(terms.freq.new)
#워드클라우드
wordcloud2(terms.freq.new, fontFamily = "맑은 고딕",
           color = "random-light",
           backgroundColor = "grey",
           size=0.5)

wordcloud2(terms.freq.new, fontFamily = "맑은 고딕",
           shape="circle",
           color = "random-light",
           size=0.5)

wordcloud2(terms.freq.new, fontFamily = "맑은 고딕",
           shape="circle",
           color = "random-dark",
           size=0.5)

wordcloud2(terms.freq.new, fontFamily = "맑은 고딕",
           shape="star",
           size=0.5)

wordcloud2(terms.freq.new,  
         fontFamily = "맑은 고딕",
         shape="diamond",
         size=0.5)

 

728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
반응형