Nltk nativebayes classification by using Bigrams - sapnilcsecu/Nltk-sentiment-analysis GitHub Wiki

Sentiment Analysis with Nltk nativebayes classification by using Bigrams

We will use Python's Nltk library for machine learning to train a text classification model.

Following are the steps required to create a text classification model in Python:

  1. Import the library
  2. Importing The movie_reviews dataset 
  3. Training and Test Sets
  4. Training Text Classification Model and Evaluating The Model

Importing Libraries

Execute the following script to import the required libraries:

import nltk
from nltk.corpus import movie_reviews
from nltk.classify import NaiveBayesClassifier
import nltk.classify.util as util
from nltk.collocations import BigramCollocationFinder as BCF
from nltk.metrics import BigramAssocMeasures
import itertools

Importing The movie_reviews dataset 

Execute the following script for importing movie_reviews dataset.

    pid = movie_reviews.fileids('neg')
    nid = movie_reviews.fileids('pos')

 next code segment return bigram feature 

    prev = [(features(movie_reviews.words(fileids = id)), 'positive') for id in pid]
    nrev = [(features(movie_reviews.words(fileids = id)), 'negative') for id in nid]

Training and Testing Sets

 following script return the training and testing set

     train_set = nrev[:ncutoff] + prev[:pcutoff]
     test_set = nrev[ncutoff:] + prev[pcutoff:]

Training Text Classification Model and Evaluating The Model

 following script train the Text Classification Model and Evaluating The Model

   classifier = NaiveBayesClassifier.train(train_set)

    # Accuracy
    print ("Accuracy is : ", util.accuracy(classifier, test_set) * 100)

 

complere source code you can get in this link movie_review_using_bigram

⚠️ **GitHub.com Fallback** ⚠️