- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不可變性
- Python - 排序行
- Python - 重新格式化段落
- Python - 計算段落中的標記
- Python - 二進位制 ASCII 轉換
- Python - 字串作為檔案
- Python - 向後檔案讀取
- Python - 篩選重複的單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取 URL
- Python - 美觀列印
- Python - 文字處理狀態機
- Python - 大寫和小寫轉換
- Python - 分詞
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 單詞替換
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 標記單詞
- Python - 塊和缺口
- Python - 塊分類
- Python - 文字分類
- Python - 二元片語
- Python - 處理 PDF
- Python - 處理 Word 文件
- Python - 閱讀 RSS 源
- Python - 情感分析
- Python - 查詢和匹配
- Python - 文字整理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹演算法
- Python - 受限搜尋
Python - 情感分析
語義分析是為了分析受眾的總體意見。它可能是對新聞、電影或關於某個討論問題的推文的反應。通常,此類反應取自社交媒體,並彙總到一個檔案中,以透過 NLP 進行分析。我們將首先定義正面和負面單詞的簡單案例。然後採用一種方法,將這些單詞作為包含這些單詞的句子的一部分進行分析。我們使用 nltk 中的 sentiment_analyzer 模組。我們首先對一個單詞進行分析,然後對成對的單詞(也稱為二元片語)進行分析。最後,我們將標記為負面情緒的單詞放入 mark_negation 函式中。
import nltk
import nltk.sentiment.sentiment_analyzer
# Analysing for single words
def OneWord():
positive_words = ['good', 'progress', 'luck']
text = 'Hard Work brings progress and good luck.'.split()
analysis = nltk.sentiment.util.extract_unigram_feats(text, positive_words)
print(' ** Sentiment with one word **\n')
print(analysis)
# Analysing for a pair of words
def WithBigrams():
word_sets = [('Regular', 'fit'), ('fit', 'fine')]
text = 'Regular excercise makes you fit and fine'.split()
analysis = nltk.sentiment.util.extract_bigram_feats(text, word_sets)
print('\n*** Sentiment with bigrams ***\n')
print analysis
# Analysing the negation words.
def NegativeWord():
text = 'Lack of good health can not bring success to students'.split()
analysis = nltk.sentiment.util.mark_negation(text)
print('\n**Sentiment with Negative words**\n')
print(analysis)
OneWord()
WithBigrams()
NegativeWord()
執行上述程式後,將得到以下輸出 -
** Sentiment with one word **
{'contains(luck)': False, 'contains(good)': True, 'contains(progress)': True}
*** Sentiment with bigrams ***
{'contains(fit - fine)': False, 'contains(Regular - fit)': False}
**Sentiment with Negative words**
['Lack', 'of', 'good', 'health', 'can', 'not', 'bring_NEG', 'success_NEG', 'to_NEG', 'students_NEG']
廣告