본문 바로가기
인공지능/R

R - 감성분석

by bibibig_data 2021. 6. 9.


 

트위터용 감성분석

install.packages("plyr")
install.packages("twitteR")
install.packages("stringr")

library(twitteR)
library(plyr)
library(stringr)

#감성 분석 함수
score.sentiment = function(sentences, pos.words, neg.words)
{
  scores = laply(sentences, 
                 function(sentence, pos.words, neg.words)
                 {
                   sentence = gsub("[[:punct:]]", "", sentence)
                   sentence = gsub("[[:cntrl:]]", "", sentence)
                   sentence = gsub('\\d+', '', sentence)
                   
                   tryTolower = function(x)
                   {
                     y = NA
                     try_error = tryCatch(tolower(x), error=function(e) e)
                     if (!inherits(try_error, "error"))
                       y = tolower(x)
                     return(y)
                   }
                   
                   sentence = sapply(sentence, tryTolower)
                   word.list = str_split(sentence, "\\s+")
                   words = unlist(word.list)
                   
                   pos.matches = match(words, pos.words)
                   neg.matches = match(words, neg.words)
                   
                   pos.matches = !is.na(pos.matches)
                   neg.matches = !is.na(neg.matches)
                   
                   score = sum(pos.matches) - sum(neg.matches)
                   return(score)
                 }, pos.words, neg.words)
  
  scores.df = data.frame(text=sentences, score=scores)
  return(scores.df)
}

#긍정어 단어와 부정어 단어 일기
pos.words = scan('positive-words.txt', what='character', comment.char=';')
neg.words = scan('negative-words.txt', what='character', comment.char=';')

#감성분석할 문장들 5개중 2개는 풍자
sample = c("You're awesome and I love you",
           "I hate and hate and hate. So angry. Die!",
           "Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity",
           "Oh how I love being ignored",
           "Absolutely adore it when my bus is late.")

#감성분석 점수
result = score.sentiment(sample, pos.words, neg.words)
result
class(result)
str(result)

#데이터 시각화
library(ggplot2)
hist(result$score)
qplot(result$score)

 


실습_1 감성분석_함수_사용

install.packages("plyr")
install.packages("twitteR")
install.packages("stringr")

library(twitteR)
library(plyr)
library(stringr)

#감성 분석 함수
score.sentiment = function(sentences, pos.words, neg.words)
{
  scores = laply(sentences, 
                 function(sentence, pos.words, neg.words)
                 {
                   sentence = gsub("[[:punct:]]", "", sentence)
                   sentence = gsub("[[:cntrl:]]", "", sentence)
                   sentence = gsub('\\d+', '', sentence)
                   
                   tryTolower = function(x)
                   {
                     y = NA
                     try_error = tryCatch(tolower(x), error=function(e) e)
                     if (!inherits(try_error, "error"))
                       y = tolower(x)
                     return(y)
                   }
                   
                   sentence = sapply(sentence, tryTolower)
                   word.list = str_split(sentence, "\\s+")
                   words = unlist(word.list)
                   
                   pos.matches = match(words, pos.words)
                   neg.matches = match(words, neg.words)
                   
                   pos.matches = !is.na(pos.matches)
                   neg.matches = !is.na(neg.matches)
                   
                   score = sum(pos.matches) - sum(neg.matches)
                   return(score)
                 }, pos.words, neg.words)
  
  scores.df = data.frame(text=sentences, score=scores)
  return(scores.df)
}

#긍정어 단어와 부정어 단어 일기
pos.words = scan('positive-words.txt', what='character', comment.char=';')
neg.words = scan('negative-words.txt', what='character', comment.char=';')

#감성분석할 문장들 5개중 2개는 풍자
sample = c("You're awesome and I love you",
           "I hate and hate and hate. So angry. Die!",
           "Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity",
           "Oh how I love being ignored",
           "Absolutely adore it when my bus is late.")

#감성분석 점수
result = score.sentiment(sample, pos.words, neg.words)
result
class(result)
str(result)

#데이터 시각화
library(ggplot2)
hist(result$score)
qplot(result$score)

'인공지능 > R' 카테고리의 다른 글

R - 마크다운  (0) 2021.06.10
R - open api  (0) 2021.06.10
R - 통계적 가설검정  (0) 2021.06.09
R - 텍스트마이닝  (0) 2021.06.08
R - 데이터 분석 프로젝트  (0) 2021.06.08