一、課程介紹

史丹佛大學於2012年3月在Coursera啟動了線上自然語言處理課程,由NLP領域大牛Dan Jurafsky 和 Chirs Manning教授授課:
https://class.coursera.org/nlp/

以下是本課程的學習筆記,以課程PPT/PDF為主,其他參考資料為輔,融入個人擴充、註解,拋磚引玉,歡迎大家在“我愛公開課”上一起探討學習。

課件彙總下載地址:史丹佛大學自然語言處理公開課課件彙總

二、情感分析(Sentiment Analysis)

1)What is Sentiment Analysis?

情感分析(Sentiment analysis),又稱傾向性分析,意見抽取(Opinion extraction),意見挖掘(Opinion mining),情感挖掘(Sentiment mining),主觀分析(Subjectivity analysis),它是對帶有情感色彩的主觀性文字進行分析、處理、歸納和推理的過程,如從評論文字中分析使用者對“數位相機”的“變焦、價格、大小、重量、閃光、易用性”等屬性的情感傾向。

更多例子如下:

l  從電影評論中識別使用者對電影的褒貶評價:

sa1

Google Product Search識別使用者對產品各種屬性的評價,並從評論中選擇代表性評論展示給使用者:

sa2

Bing Shopping識別使用者對產品各種屬性的評價:

sa3

Twitter sentiment versus Gallup Poll of Consumer Confidence:挖掘Twitter(中文:微博)中的使用者情感發現,其與傳統的調查、投票等方法結果有高度的一致性(以消費者信心和政治選舉為例,corelation達80%),詳細見論文:Brendan O’Connor, Ramnath Balasubramanyan, Bryan R. Routledge, and Noah A. Smith. 2010. From Tweets to Polls: Linking Text Sentiment to Public Opinion Time Series. In ICWSM-2010。(注:下圖中2008年到2009年初,網民情緒低谷是金融危機導致,從2009年5月份開始慢慢恢復)

sa4

Twitter sentiment: 通過Twitter使用者情感預測股票走勢,2012年5月,世界首家基於社交媒體的對衝基金 Derwent Capital Markets 在屢次跳票後終於上線。它會即時關注Twitter 中的公眾情緒指導投資。正如基金創始人保羅•郝汀(Paul Hawtin)表示:“長期以來,投資者已經廣泛地認可金融市場由恐懼和貪婪驅使,但我們從未擁有一種技術或資料來量化人們的情感。”一直為金融市場非理性舉動所困惑的投資者,終於有了一扇可以瞭解心靈世界的窗戶——那便是 Twitter 每天浩如煙海的推文,在一份八月份的報導中顯示,利用 Twitter 的對衝基金 Derwent Capital Markets 在首月的交易中已經盈利,它以1.85%的收益率,讓平均數只有0.76%的其他對衝基金相形見絀。類似的工作還有預測電影票房、選舉結果等,均是將公眾情緒與社會事件對比,發現一致性,並用於預測,如將“冷靜CLAM”情緒指數後移3天后和道瓊斯工業平均指數DIJA驚人一致。詳細見論文: Johan Bollen, Huina Mao, Xiaojun Zeng. 2011. Twitter mood predicts the stock market, Journal of Computational Science 2:1, 1-8.(注:DIJA,全稱Dow Jones Industrial Average

sa5

sa6

 

Target Sentiment on TwitterTwitter Sentiment App):對Twitter中包含給定query的tweets進行情感分類。對於公司瞭解使用者對公司、產品的喜好,用於指導改善產品和服務,公司還可以據此發現競爭對手的優劣勢,使用者也可以根據網友甚至親友評價決定是否購買特定產品。詳細見論文:Alec Go, Richa Bhayani, Lei Huang. 2009. Twitter Sentiment Classification using Distant Supervision.

sa7

sa8

情感分析的意義何在?下面以實際應用為例進行直觀的闡述:

•  Movie:  is this review positive or negative?

•  Products:  what do people think about the new iPhone?

•  Public sentiment:  how is consumer confidence? Is despair increasing?

•  Politics:  what do people think about this candidate or issue?

•  Prediction:  predict election outcomes or market trends from sentiment

情感分析主要目的就是識別使用者對事物或人的看法、態度(attitudes:enduring, affectively colored beliefs, dispositions towards objects or persons),參與主體主要包括:

  1. Holder (source) of attitude:觀點持有者
  2. Target (aspect) of attitude:評價物件
  3. Type of attitude:評價觀點
    • ​ From a set of types:Like, love, hate, value, desire, etc.
    •  Or (more commonly) simple weighted polarity: positive, negative, neutral,together with strength
  4. Text containing the attitude:評價文字,一般是句子或整篇文件

更細更深入的還包括評價屬性,情感詞/極性詞,評價搭配等、

通常,我們面臨的情感分析任務包括如下幾類:

  1. Simplest task: Is the attitude of this text positive or negative?
  2. More complex: Rank the attitude of this text from 1 to 5
  3. Advanced: Detect the target, source, or complex attitude types

後續章節將以Simplest task為例進行介紹。

2)A Baseline Algorithm

本小節對影評進行情感分析為例,向大家展示一個簡單、實用的情感分析系統。詳細見論文: Bo Pang, Lillian Lee, and Shivakumar Vaithyanathan.  2002.  Thumbs up? Sentiment Classification using Machine Learning Techniques. EMNLP-2002, 79—86.

Bo Pang and Lillian Lee. 2004. A Sentimental Education: Sentiment Analysis Using Subjectivity Summarization Based on Minimum Cuts.  ACL, 271-278

我們面臨的任務是Polarity detection: Is an IMDB movie review positive or negative?”,資料集為“Polrity Data 2.0: http://www.cs.cornell.edu/people/pabo/movie-review-data”.作者將情感分析當作分類任務,拆分成如下子任務:

  1. Tokenization:正文提取,過濾時間、電話號碼等,保留大寫字母開頭的字串,保留表情符號,切詞;
  2. Feature Extraction:直觀上,我們會認為形容詞直接決定文字的情感,而Pang和Lee的實驗表明,採用所有詞(unigram)作為特徵,可以達到更好的情感分類效果。

其中,需要對否定句進行特別的處理,如句子”I didn’t like this movie”vs “I really like this movie”,unigram只差一個詞,但是有著截然不同的含義。為了有效處理這種情況,Das and Chen (2001)提出了“Add NOT_ to every word between negation and following punctuation”,根據此規則可以將句子“didn’t like this movie , but I”轉換為“didn’t NOT_like NOT_this NOT_movie, but I”。

另外,在抽取特徵時,直觀的感覺“Word occurrence may matter more than word frequency”,這是因為最相關的情感詞在一些文字片段中僅僅出現一次,詞頻模型起得作用有限,甚至是負作用,則使用多重伯努利模型事件空間代替多項式事件空間,實驗也的確證明了這一點。所以,論文最終選擇二值特徵,即詞的出現與否,代替傳統的頻率特徵。log(freq(w))也是一種值得嘗試的降低頻率干擾的方法。

  1. Classification using different classifiers:Naïve BayesMaxEntSVM,以樸素貝葉斯分類器為例,訓練過程如下:

sa9

預測過程如下:

sa10

實驗表明,MaxEnt和SVM相比Naïve Bayes可以得到更好的效果。

最後,通過case review可以總結下,影評情感分類的難點是什麼?

  • 語言表達的含蓄微妙:“If you are reading this because it is your darling fragrance, please wear it at home exclusively, and tape the windows shut.”,“ She runs the gamut of emotions from A to B”。
  • 挫敗感表達方式:先描述開始的期待(不吝讚美之詞),後表達最後失望感受,如“This film should be brilliant.  It sounds like a great plot, the actors are first grade, and the supporting cast is good as well, and Stallone is attempting to deliver a good performance. However, it can’t hold up.”,“Well as usual Keanu Reeves is nothing special, but surprisingly, the very talented Laurence Fishbourne is not so good either, I was surprised.”。

3)Sentiment Lexicons

情感分析模型非常依賴於情感詞典抽取特徵或規則,以下羅列了較為流行且成熟的開放情感詞典資源:

  • GI(The General Inquirer):該詞典給出了每個詞條非常全面的資訊,如詞性,反義詞,褒貶,等,組織結構如下:

sa11

詳細見論文:Philip J. Stone, Dexter C Dunphy, Marshall S. Smith, Daniel M. Ogilvie. 1966.The General Inquirer: A Computer Approach to Content Analysis. MIT Press

sa12

詳細見論文:Pennebaker, J.W., Booth, R.J., & Francis, M.E. (2007). Linguistic Inquiry and Word Count: LIWC 2007. Austin, TX

sa13

詳細見論文:Theresa Wilson, Janyce Wiebe, and Paul Hoffmann (2005). Recognizing Contextual Polarity in Phrase-Level Sentiment Analysis. Proc. of HLT-EMNLP-2005.

Riloff and Wiebe (2003). Learning extraction patterns for subjective expressions. EMNLP-2003.

  • Bing Liu Opinion Lexicon:其中包含Positive words: 2006,Negative words: 4783,需要特別說明的是,詞典不但包含正常的用詞,還包含了拼寫錯誤、語法變形,俚語以及社交媒體標記等,詳細見論文:Minqing Hu and Bing Liu. Mining and Summarizing Customer Reviews. ACM SIGKDD-2004.
  • SentiWordNet:其通過對WordNet中的詞條進行情感分類,並標註出每個詞條屬於positive和negative類別的權重大小,組織結構如下:

sa13

詳細見論文:Stefano Baccianella, Andrea Esuli, and Fabrizio Sebastiani. 2010SENTIWORDNET 3.0: An Enhanced Lexical Resource for Sentiment Analysis and Opinion Mining. LREC-2010

以上給出了一系列可用的情感詞典資源,但是,如何選擇一個合適的為我所用呢?這裡,通過對比同一詞條在不同詞典之間的分類,衡量詞典資源的不一致程度,如下:

sa14

對於在不同詞典中表現不一致的詞條,我們至少可以做兩件事情。第一,review這些詞條,通過少量人工加以糾正;第二,可以得到一些存在褒貶歧義的詞條。

給定一個詞,如何確定其以多大概率出現在某種情感類別文字中呢?以IMDB下不同打分下影評為例,最簡單的方法就是計算每個分數(星的個數)對應的文字中詞條出現的頻率,如下圖所示為Count(“bad”)分佈情況:

sa15

使用更多的是likelihood公式:

sa16

為了使得不同詞條在不同類別下的概率可比,通常使用Scaled likelihood公式代替,如下:

sa17

如下圖所示,列出了部分詞條在不同類別下的Scaled likelihood,據此可以判斷每個詞條的傾向性。

sa18

另外,我們通常會有這麼一個疑問:否定詞(如not, n’t, no, never)是否更容易出現在negative情感文字中?Potts, Christopher(2011)等通過實驗給出了答案:More negation in negative sentiment,如下圖所示:

sa19

4)Learning Sentiment Lexicons

我們在慶幸和讚揚眾多公開情感詞典為我所用的同時,我們不免還想了解構建情感詞典的方法,正所謂知其然知其所以然。一方面在面臨新的情感分析問題,解決新的情感分析任務時,難免會需要結合實際需求構建或完善情感詞典,另一方面,可以將成熟的詞典構建方法應用於其他領域,知識無邊界,許多方法都是相通的。

常見的情感詞典構建方法是基於半指導的bootstrapping學習方法,主要包括兩步:

  1. Use a small amount of information(Seed)
    1. A few labeled examples
    2. A few hand-built patterns
  2. To bootstrap a lexicon

接下來,通過相關的幾篇論文,詳細闡述下構建情感詞典的方法。具體如下:

     1. Hatzivassiloglou & McKeown:論文見Vasileios Hatzivassiloglou and Kathleen R. McKeown. 1997. Predicting the Semantic Orientation of Adjectives. ACL, 174–181,基於這樣的一種語言現象:“Adjectives conjoined by ‘and’ have same polarity;Adjectives conjoined by ‘but ‘ do not”,如下示例:

  • Fair and legitimate, corrupt and brutal
  • *fair and brutal, *corrupt and legitimate
  • fair but brutal

Hatzivassiloglou & McKeown(1997)提出了基於bootstrapping的學習方法,主要包括四步:

  • Step 1:Label seed set of 1336 adjectives (all >20 in 21 million word WSJ corpus)

初始種子集包括657個 positive words(如adequate central clever famous intelligent remarkable reputed sensitive slender thriving…)和679個 negative words(如contagious drunken ignorant lanky listless primitive strident troublesome unresolved unsuspecting…)

  • Step 2:Expand seed set to conjoined adjectives,如下圖所示:

sa20

  • Step 3:Supervised classifier assigns “polarity similarity” to each word pair, resulting in graph,如下圖所示:
    sa21
  • Step 4:Clustering for partitioning the graph into two

sa22

最終,輸出新的情感詞典,如下(加粗詞條為自動挖掘出的詞條):

  • Positive: bold decisive disturbing generous good honest important large mature patient peaceful positive proud sound stimulating straightforwardstrange talented vigorous witty…
  • Negative: ambiguous cautious cynical evasive harmful hypocritical inefficient insecure irrational irresponsible minor outspoken pleasant reckless risky selfish tedious unsupported vulnerable wasteful…

     2.  Turney Algorithm:論文見Turney (2002):  Thumbs Up or Thumbs Down? Semantic Orientation Applied to Unsupervised Classification of Reviews,具體步驟如下:

  • Step 1:Extract a phrasal lexicon from reviews,通過規則抽取的phrasal如下圖所示:

sa23

 

  • Step 2:Learn polarity of each phrase,那麼,如何評價phrase的polarity呢?直觀上,有這樣的結論:“Positive phrases co-occur more with ‘excellent’,Negative phrases co-occur more with ’poor’”,這時,將問題轉換成如何衡量詞條之間的共現關係?於是,學者們引入了點互資訊(Pointwise mutual information,PMI),它經常被用於度量兩個具體事件的相關程度,公式為:

sa24

兩個詞條的PMI公式為:

sa25常用的計算PMI(word1, word2)方法是分別以”word1”,”word2”和”word1 NEAR word2”為query,根據搜尋引擎檢索結果,得到P(word)和P(word1, word2),如下:

P(word) = hits(word)/N

P(word1,word2) = hits(word1 NEAR word2)/N2

則有:

sa26

那麼,計算一個phrase的polarity公式為(excellent和poor也可以使用其它已知極性詞代替):

sa27

Turney Algorithm在410 reviews(from Epinions)的資料集上,其中170 (41%) negative,240 (59%) positive,取得了74%的準確率(baseline為59%,均標註為positive)。

  • Step 3:Rate a review by the average polarity of its phrases

     3. Using WordNet to learn polarity:論文見S.M. Kim and E. Hovy. 2004.Determining the sentiment of opinions. COLING 2004,M. Hu and B. Liu. Mining and summarizing customer reviews. In Proceedings of KDD, 2004.該方法步驟如下:

  • Create positive (“good”) and negative seed-words (“terrible”)
  • Find Synonyms and Antonyms

Positive Set:  Add  synonyms of positive words (“well”) and antonyms of negative words

Negative Set: Add synonyms of negative words (“awful”)  and antonyms of positive words (”evil”)

  • Repeat, following chains of synonyms
  • Filter

以上幾個方法都有較好的領域適應性和魯棒性,基本思想可以概括為“Use seeds and semi-supervised learning to induce lexicons”,即:

  • Start with a seed set of words (‘good’, ‘poor’)
  • Find other words that have similar polarity:
    • Using “and” and “but”
    • Using words that occur nearby in the same document
    • Using WordNet synonyms and antonyms
    • Use seeds and semi-supervised learning to induce lexicons

5)Other Sentiment Tasks

上面介紹了文件級或句子級情感分析,但是,實際中,一篇文件(評論)中往往會提及不同的方面/屬性/物件(以下統稱屬性),且可能對不同的屬性持有不同的傾向性,如“The food wasgreat but the service was awful”。一般通過Frequent phrases + rules的方法抽取評價屬性,如下:

  • Find all highly frequent phrases across reviews (“fish tacos”)
  • Filter by rules like “occurs right after sentiment word”:“…great fish tacos” means fish tacos a likely aspect

通常,我們還會面臨一種問題:評價屬性缺失,準確的講,評價屬性不在句子中。這是很常見的現象,此時就需要結合上下文環境,如來自某電影的評論缺失的評價屬性基本上就是電影名或演員,可以基於已知評價屬性的句子訓練分類器,然後對評價屬性缺失的句子進行屬性預測。

Blair-Goldensohn et al.提出了一套通用的aspect-based summarization models,如下圖所示:

sa28

詳細見論文:S. Blair-Goldensohn, K. Hannan, R. McDonald, T. Neylon, G. Reis, and J. Reynar. 2008.  Building a Sentiment Summarizer for Local Service Reviews.  WWW Workshop

另外,其他的一些情感分析的相關任務有:

  • Emotion: 個人情緒
    • Detecting annoyed callers to dialogue system
    • Detecting confused/frustrated  versus confident students
  • Mood: 個人情緒
    • Finding traumatized or depressed writers
  • Interpersonal stances: 人際關係中的談話方式
    • Detection of flirtation or friendliness in conversations
  • Personality traits: 性格
    • Detection of extroverts

三、參考資料

  1. Lecture Slides:Sentiment Analysis
  2. Sentiment tutorial
  3. 趙妍研,文字情感分析綜述v

    ia:52opencourse