Nltk nativebayes classification without Bigrams - sapnilcsecu/Nltk-sentiment-analysis GitHub Wiki
Following are the steps required to create a text classification model without Bigrams in Python:
- Import the library
- load the positive and negative review and create the feature
- Prepare train and test dataset
- Training Text Classification Model and Evaluating The Model
Execute the following script to import the required libraries:
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk
Execute the following script load the positive and negative review and create the feature then add to the feature dictionary
for fileid in movie_reviews.fileids('pos'):
words = movie_reviews.words(fileid)
pos_reviews.append((create_word_features(words), "positive"))
for fileid in movie_reviews.fileids('neg'):
words1 = movie_reviews.words(fileid)
neg_reviews.append((create_word_features(words1), "negative"))
Execute the following script for preparing train and test dataset
train_set = neg_reviews[:750] + pos_reviews[:750]
test_set = neg_reviews[750:] + pos_reviews[750:]
Execute the following script for Training Text Classification Model and Evaluating The Model
classifier = NaiveBayesClassifier.train(train_set)
accuracy = nltk.classify.util.accuracy(classifier, test_set)
print(accuracy * 100)
complere source code you can get in this link Senti_model