본문 바로가기

NPL with ML

감성 분석 (Sentiment Analysis) - 비지도학습 기반, VADER

반응형

비지도 감성 분석은 Lexicon을 기반으로 합니다. 많은 감성 분석용 데이터는 이러한 결정된 레이블 값을 가지고 있지 않습니다. 이러한 경우에 Lexicon은 유용하게 사용될 수 있습니다. (검색을 통해 여러가지 한글 Lexicon을 찾아볼 수 있습니다.)

 

텍스트 분석(Text Analytics)을 하다보면 시맨틱(Semantic)이라는 용어를 자주 접하게 될 것입니다. '문맥상 의미'입니다. 이 문맥상 의미에 따라 동일한 단어라도 완전하게 달라지기 떄문에 제공되는 감성사전은 이 문맥을 파악하는데 가장 중점을 두고 있습니다. 

 

감성사전을 위한 몇가지 사전은 하기와 같습니다.

1. NLTK: 시맨틱을 프로그램적으로 인터페이스화 할 수 있게 제공합니다. 제공되는 WordNet은 다양한 상황에서 같은 어휘라도 다르게 사용되는 어휘의 시맨틴 정보를 제공하며, 이를 위해 각각의 품사(명사, 동사, 형용사, 부사 등)로 구성된 개별 단어를 Synset이라는 개념을 이용해 표현합니다. 하지만 아쉽게도 예측성능은 그리 좋지 못합니다.

2. SentiWordNet: NLTK패키지의 WordNet과 유사하게 감성 단어 전용의 WordNet을 구현한 것입니다. WordNet의 Synset별로 3가지 감성 점수를 할당합니다.

3. VADER: 주로 소설 미디어의 텍스트에 대한 감성 분석을 제공하기 위한 패키지입니다. 뛰어난 감성 분석 결과를 제공하며, 비교적 빠른 수행 시간을 보장해 대용량 텍스트 데이터에 잘 사용되는 패키지입니다.

4.Pattern: 예측 성능 측면에서 가장 주목받는 패키지입니다. 파이썬 3.X버전에서 호환되지 않고 2.X버전에서만 동작하는 아쉬움이 있습니다.

 

1. VADER 실습

from nltk.sentiment.vader import SentimentIntensityAnalyzer
import pandas as pd
import re
import nltk

# 첫 1회만 실행
# nltk.download('all')


review_df = pd.read_csv('C:\\Users\\HANA\\PycharmProjects\\HANATOUR\\NLP\\TEXT_Example\\labeledTrainData.tsv', header=0, sep="\t", quoting=3)
# print(review_df.head())
review_df['review'] = review_df['review'].str.replace('<br />', ' ')
review_df['review'] = review_df['review'].apply(lambda x : re.sub("[^a-zA-Z]"," ", x))
print(review_df['review'][0])

senti_analyzer = SentimentIntensityAnalyzer()
senti_scores = senti_analyzer.polarity_scores(review_df['review'][0])
print(senti_scores)

 

polarity_scores() 메소드를 통해 감정에 대한 평가를 확인해 봅니다. 'neg'는 부정 감성 지수, 'neu'는 중립적인 감성 지수, 'pos'는 긍정 감성 지수, 그리고 compound는 neg, neu, pos score를 적절히 조합해 -1에서 1사이의 감성 지수를 표현한 값입니다. compound score를 기반으로 부정 감성 또는 긍정 감성 여부를 결정합니다. 보통 0.1 이상이면 긍정 감성, 그 이하이면 부정감성으로 판단하나 상황에 따라 이 임계값을 적절히 조정해 예측 성능을 조절합니다.

{'neg': 0.119, 'neu': 0.755, 'pos': 0.126, 'compound': -0.0678}

 

-0.0678이 나와 부정감성인데요. 진짜 그런지 review_df['review'][0]의 review를 확인해보시죠.

"With all this stuff going down at the moment with MJ i've started listening to his music, watching the odd documentary here and there, watched The Wiz and watched Moonwalker again. Maybe i just want to get a certain insight into this guy who i thought was really cool in the eighties just to maybe make up my mind whether he is guilty or innocent. Moonwalker is part biography, part feature film which i remember going to see at the cinema when it was originally released. Some of it has subtle messages about MJ's feeling towards the press and also the obvious message of drugs are bad m'kay.<br /><br />Visually impressive but of course this is all about Michael Jackson so unless you remotely like MJ in anyway then you are going to hate this and find it boring. Some may call MJ an egotist for consenting to the making of this movie BUT MJ and most of his fans would say that he made it for the fans which if true is really nice of him.<br /><br />The actual feature film bit when it finally starts is only on for 20 minutes or so excluding the Smooth Criminal sequence and Joe Pesci is convincing as a psychopathic all powerful drug lord. Why he wants MJ dead so bad is beyond me. Because MJ overheard his plans? Nah, Joe Pesci's character ranted that he wanted people to know it is he who is supplying drugs etc so i dunno, maybe he just hates MJ's music.<br /><br />Lots of cool things in this like MJ turning into a car and a robot and the whole Speed Demon sequence. Also, the director must have had the patience of a saint when it came to filming the kiddy Bad sequence as usually directors hate working with one kid let alone a whole bunch of them performing a complex dance scene.<br /><br />Bottom line, this movie is for people who like MJ on one level or another (which i think is most people). If not, then stay away. It does try and give off a wholesome message and ironically MJ's bestest buddy in this movie is a girl! Michael Jackson is truly one of the most talented people ever to grace this planet but is he guilty? Well, with all the attention i've gave this subject....hmmm well i don't know because people can be different behind closed doors, i know this for a fact. He is either an extremely nice but stupid guy or one of the most sickest liars. I hope ."

 

반응형