텍스트에 내포된 감성 분석하기
이 예제에서는 VADER(Valence Aware Dictionary and sEntiment Reasoner) 알고리즘을 사용하여 감성 분석을 수행하는 방법을 보여줍니다.
VADER 알고리즘은 주석이 있는 단어 목록(감성 어휘사전)을 사용하고, 이 목록의 각 단어에는 그에 상응하는 감성 점수가 있습니다. VADER 알고리즘은 텍스트에 있는 일련의 단어의 점수를 조정하는 단어 목록도 활용합니다.
증폭어 – 일련의 토큰의 감성을 증폭시키는 단어 또는 n-gram. 예: "absolutely", "amazingly" 같은 단어.
약화어 – 일련의 토큰의 감성을 약화시키는 단어 또는 n-gram. 예: "hardly", "somewhat" 같은 단어.
부정어 – 일련의 토큰의 감성을 부정하는 단어. 예: "not", "isn't" 같은 단어.
텍스트에 내포된 감성을 평가하려면 vaderSentimentScores 함수를 사용하십시오.
데이터 불러오기
readtable을 사용하여 파일 weekendUpdates.xlsx에서 텍스트 데이터를 추출합니다. 파일 weekendUpdates.xlsx에는 해시태그 "#weekend"와 "#vacation"이 포함된 상태 업데이트가 들어 있습니다.
filename = "weekendUpdates.xlsx"; tbl = readtable(filename,'TextType','string'); head(tbl)
ID TextData
__ _________________________________________________________________________________
1 "Happy anniversary! ❤ Next stop: Paris! ✈ #vacation"
2 "Haha, BBQ on the beach, engage smug mode! ❤ #vacation"
3 "getting ready for Saturday night #yum #weekend "
4 "Say it with me - I NEED A #VACATION!!! ☹"
5 " Chilling at home for the first time in ages…This is the life! #weekend"
6 "My last #weekend before the exam ."
7 "can’t believe my #vacation is over so unfair"
8 "Can’t wait for tennis this #weekend "
텍스트 데이터에서 토큰화된 문서로 구성된 배열을 만들고, 처음 몇 개 문서를 표시합니다.
str = tbl.TextData; documents = tokenizedDocument(str); documents(1:5)
ans =
5×1 tokenizedDocument:
11 tokens: Happy anniversary ! ❤ Next stop : Paris ! ✈ #vacation
16 tokens: Haha , BBQ on the beach , engage smug mode ! ❤ #vacation
9 tokens: getting ready for Saturday night #yum #weekend
13 tokens: Say it with me - I NEED A #VACATION ! ! ! ☹
19 tokens: Chilling at home for the first time in ages … This is the life ! #weekend
감성 평가하기
vaderSentimentLexicon 함수를 사용하여, 토큰화된 문서의 감성을 평가합니다. 1에 가까운 점수는 긍정 감성을, -1에 가까운 점수는 부정 감성을, 0에 가까운 점수는 중립 감성을 나타냅니다.
compoundScores = vaderSentimentScores(documents);
처음 몇 개 문서의 점수를 표시합니다.
compoundScores(1:5)
ans = 5×1
0.4738
0.9348
0.6705
-0.5067
0.7345
긍정 감성과 부정 감성을 지닌 텍스트를 워드 클라우드로 시각화합니다.
idx = compoundScores > 0; strPositive = str(idx); strNegative = str(~idx); figure subplot(1,2,1) wordcloud(strPositive); title("Positive Sentiment") subplot(1,2,2) wordcloud(strNegative); title("Negative Sentiment")

참고 항목
vaderSentimentScores | ratioSentimentScores | tokenizedDocument