R을 활용한 텍스트마이닝_how to do Text mining?

in r •  7 years ago 

❚ 실습하기_ 시각화(워드 클라우드)_영어텍스트
☑1단계: 필요한 패키지 인스톨(요리 도구 셋팅)

일단, 코딩을 쳐보면서 따라해보자.

install.packages("tm") #텍스트 처리를 할 수 있게 도와주는 패키지

install.packages("stringr") #문자열 처리를 할 수 있게 도와주는 패키지

install.packages("wordcloud") # 문자들을 시각화할 수 있게 도와주는 패키지

install.packages("NLP") #영어 텍스트에 특화된 패키지

install.packages("RColorBrewer") #색상을 다양하게 표현할 수 있게 도와주는 패키지

☑2단계: 데이터 불러오기(요리재료 준비)

아직, 우리는 데이터가 없으니, 데이터부터 만들어 보자.

데이터는 .txt로 저장된 데이터를 불러와야 한다.

먼저, 영어로 된 데이터를 준비해보자.

library(sp)

getwd()

setwd("D:/자신의 경로/")#경로가 맞지 않을 경우 변경

readLines("rblog.txt") #텍스트문서를 읽는 함수

myEtext<-readLines("rblog.txt")

head(myEtext)

☑3단계: 데이터 전처리 하기(요리재료 다듬기)

paste(readLines("rblog.txt"),collapse = " ")#7개의 라인을 1개로 만듦

myEtext2<-paste(readLines("rblog.txt"),collapse = " ")

myEtext2

myEtext3<-gsub(pattern = "\W", replace=" ",myEtext2) #gsub(찾을것, 바꿀것, 해당열), \W은 영어구두점을 의미하는 것임

myEtext3

tolower(myEtext3) #모두 소문자로 처리

myEtext4<-tolower(myEtext3)

myEtext4

library(tm)

stopwords() #텍스트내의 불용어

removeWords(myEtext4,stopwords())

myEtext5<-removeWords(myEtext4,stopwords()) #불용어 삭제

stripWhitespace(myEtext5)

myEtext6<-stripWhitespace(myEtext5) #쓸데 없는 공란 제거

☑4단계: 단어 추출하기 (양념넣고 만들기)

library(stringr)

library(wordcloud)

myEtext6

str_split(myEtext6,pattern = "\s+") #문자열을 단어들의 조각으로 다시 찢어놓기, \s+ 은 "" 뒤에 공백하나 띄우기

myEtext7<-str_split(myEtext6,pattern = "\s+")

class(myEtext7) #어떤 유형의 객체인가를 알수 있다 결과는 list로 우리가 필요한 자료유형은 문자형 자료이다.

myEtextbag<-unlist(myEtext7) #리스트를 벡터로 바꿔주는 함수

class(myEtextbag)

myEtextbag

str(myEtextbag)

☑5단계: 워드클라우드 그리기 (테이블에 맛있게 셋팅하기)

wordcloud(myEtextbag)

wordcloud(myEtextbag,min.freq = 2) #최소 두번이상

wordcloud(myEtextbag,min.freq = 2,random.order = FALSE,scale = c(3,0.5),color=rainbow(3)) #scale최고 폰트에서 최저폰트

0704.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@jaballet, I gave you an upvote on your post! Please give me a follow and I will give you a follow in return and possible future votes!

Thank you in advance!

Congratulations @jaballet! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemitBoard - Witness Update
Vote for @Steemitboard as a witness to get one more award and increased upvotes!