Python-text-processing-python-sentiment-analysis

提供:Dev Guides
移動先:案内検索

Python-センチメント分析

セマンティック分析とは、視聴者の一般的な意見を分析することです。 それは、ニュース、映画、または議論中のいくつかの問題に関するツイートへの反応かもしれません。 通常、このような反応はソーシャルメディアから取得され、NLPで分析されるファイルにまとめられます。 最初に肯定的な単語と否定的な単語を定義する簡単なケースを取り上げます。 次に、これらの単語を使用して、それらの単語を文の一部として分析するアプローチを取ります。 nltkのsentiment_analyzerモジュールを使用します。 最初に1つの単語を使用して分析を実行し、次にバイグラムとも呼ばれるペアの単語を使用して分析を実行します。 最後に、 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']