2016-10-30 12 views
5

Tôi đang sử dụng Vader SentimentAnalyzer để có được điểm số phân cực. Tôi đã sử dụng điểm xác suất cho số dương/âm/trung tính trước đây, nhưng tôi mới nhận ra điểm "hợp chất", từ -1 (nhiều nhất) đến 1 (nhiều nhất) sẽ cung cấp một số đo cực. Tôi tự hỏi cách tính điểm "hợp chất". Điều đó có được tính từ vectơ [pos, neu, neg] không?Điểm số phân cực 'hợp chất' của Vader được tính toán bằng Python NLTK như thế nào?

+0

Mã này là tại địa chỉ: https://github.com/nltk/nltk/blob/develop/ nltk/sentiment/vader.py – alvas

Trả lời

24

Vader đầu ra thuật toán điểm tình cảm đến 4 loại tình cảm https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L441:

  • neg: Negative
  • neu: Neutral
  • pos: Positive
  • compound: Hợp chất (tức là số điểm tổng hợp)

Hãy xem qua mã, trường hợp đầu tiên của hợp chất là https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L421, nơi mà nó tính:

compound = normalize(sum_s) 

Chức năng normalize() được định nghĩa như vậy tại https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L107:

def normalize(score, alpha=15): 
    """ 
    Normalize the score to be between -1 and 1 using an alpha that 
    approximates the max expected value 
    """ 
    norm_score = score/math.sqrt((score*score) + alpha) 
    return norm_score 

Vì vậy, có một siêu tham số alpha.

Đối với sum_s, nó là một tổng của các đối số tình cảm truyền cho score_valence() chức năng https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L413

Và nếu chúng ta tìm lại sentiment lập luận này, chúng tôi thấy rằng nó tính khi gọi polarity_scores() chức năng tại https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L217:

def polarity_scores(self, text): 
    """ 
    Return a float for sentiment strength based on the input text. 
    Positive values are positive valence, negative value are negative 
    valence. 
    """ 
    sentitext = SentiText(text) 
    #text, words_and_emoticons, is_cap_diff = self.preprocess(text) 

    sentiments = [] 
    words_and_emoticons = sentitext.words_and_emoticons 
    for item in words_and_emoticons: 
     valence = 0 
     i = words_and_emoticons.index(item) 
     if (i < len(words_and_emoticons) - 1 and item.lower() == "kind" and \ 
      words_and_emoticons[i+1].lower() == "of") or \ 
      item.lower() in BOOSTER_DICT: 
      sentiments.append(valence) 
      continue 

     sentiments = self.sentiment_valence(valence, sentitext, item, i, sentiments) 

    sentiments = self._but_check(words_and_emoticons, sentiments) 

Nhìn vào polarity_scores chức năng, những gì nó làm là để lặp qua toàn bộ từ vựng SentiText và kiểm tra với sự cai trị dựa trên sentiment_valence() chức năng để gán thứ e hóa trị điểm đến tình cảm https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L243, xem Phần 2.1.1 của http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf

Quay trở lại với số điểm hợp chất, chúng ta thấy rằng:

  • số điểm compound là điểm bình thường của sum_s
  • sum_s là tổng của hóa trị dựa trên một số chẩn đoán và một từ vựng tình cảm (aka. Cường độ tâm lý) và
  • điểm chuẩn hóa chỉ đơn giản là số sum_s chia cho hình vuông của nó cộng với tham số alpha làm tăng mẫu số của hàm chuẩn hóa.

Có phải đó là tính từ [pos, neu, neg] vector?

Không thực sự =)

Nếu chúng ta có một cái nhìn tại score_valence chức năng https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L411, chúng ta thấy rằng tỷ số hợp chất được tính với sum_s trước khi pos, neg và neu điểm được tính bằng _sift_sentiment_scores() rằng tính toán điểm số pos, neg và neu invidiual sử dụng điểm thô từ sentiment_valence() mà không có tổng.


Nếu chúng ta hãy nhìn vào alpha mathemagic này, có vẻ như đầu ra của bình thường là khá ổn định (nếu không bị giới hạn), tùy thuộc vào giá trị của alpha:

alpha=0:

enter image description here

alpha=15:

enter image description here

alpha=50000:

enter image description here

alpha=0.001:

enter image description here

Nó được sôi nổi khi đó là tiêu cực:

alpha=-10:

enter image description here

alpha=-1,000,000:

enter image description here

alpha=-1,000,000,000:

enter image description here

+0

Giải thích rất tốt, có vẻ như bạn đang thiếu phần sqrt trong biểu đồ & equatio ns –

Các vấn đề liên quan