빅데이터/R studio

R Studio를 사용한 Text mining analysis - 01

갓상 2021. 4. 21. 23:19

R 설치

1. 공식 사이트 접속 후 Download -> CRAN

   - http://www.r-project.org/

 

2. Korea를 찾아서 그 중 아무 링크나 눌러서 본인의 OS에 맞게 설치

 

3. 첫 설치인 경우 "base"를 눌러서 설치한다.

 

R Studio 설치

1. 공식 사이트 접속 후 우측 상단의 "Download"로 들어간 후 Free 선택

   - https://www.rstudio.com/

 

Text mining 실습

Text mining 실습을 위한 dataset은 Web Hacking Incident Database(WHID) 사용

   - sample dataset projects.webappsec.org/w/page/13246995/Web-Hacking-Incident-Database

 

The Web Application Security Consortium / Web-Hacking-Incident-Database

  Project Overview The Web Hacking Incident Database, or WHID for short, is a Web Application Security Consortium project dedicated to maintaining a list of web applications related security incidents.  WHID's goal is to serve as a tool for raising aware

projects.webappsec.org

Text mining을 할 때, 데이터 사이즈가 작을 경우에 R, Python, Weka를 사용하고,

데이터 사이즈가 클 경우에는 Hadoop / MapReduce, Hive를 사용한다.

 

rm(list=ls()) # 실행 전 모든 objects 삭제 (초기화 작업)

#Load libraries
library(tm) # text mining을 위한 함수
library(corrplot) # correlation matrices의 plot 생성을 위한 함수
library(randomForest) # random forest decision tree 분석을 위한 함수
library(lubridate) # month 와 year을 사용하기 위한 함수

rawData <- read.csv("DefaultWHIDView2.csv",header=TRUE, encoding = "UTF-8")
head(rawData) #처음 몇개 데이터의 row를 보여준다
colnames(rawData) # column 이름을 보여준다

data <- Corpus(VectorSource(rawData[,"IncidentDescription"])) # column에서 text data 추출

read.csv를 사용할 때, "UTF-8" 로 encoding 하지 않았을 때는, 'subscript out of bounds' 에러가 발생하였다.

인코딩을 해주지 않으면 특수문자 처럼 글자가 깨져서 생기는 오류인 것 같다.

 

#Cleanup text
data2 = tm_map(data, stripWhitespace)	# text 사이 공백 여러개를 한개로 변경
data2 = tm_map(data2, stemDocument)	# study studing studied를 같은 단어로 생각한다
data2 = tm_map(data2, tolower)	# 소문자로 바꿔준다
stopWords = c(stopwords("english"))	
data2 = tm_map(data2, removeWords, stopWords)	# stopWords를 삭제한다
data2 = tm_map(data2, removePunctuation)	# 기호를 삭제한다
data2 = tm_map(data2, removeNumbers)	# 숫자를 삭제한다

stopwords란 "a", "the", "for" 등등 중요하지 않은 전치사 들을 일컫는다.