Example #1
0
def sentimentsAnalysisSingleTweetUsingNaiveBayes():

    #Get the tweet from UI
    mdiProjectTweet = request.form.get("tweet")

    #Process the tweet
    porcessedTweet = mdiProjectProcessTweet(mdiProjectTweet)

    #Get all the stop words
    stopWordList = mdiProjectStopWordList(
        mdiProjectReadFiles("mdiProjectFiles", "StopWords.txt"))

    #Get the features vector for a single tweet
    featureVector = mdiProjectGetFeatureVectorForSingleTweet(
        porcessedTweet, stopWordList)

    #Get the feacture words
    featureWords = mdiProjectExtractFeatures(featureVector)

    #Get sentiments based on the feature words
    tweetSentiment = NaiveBayesClassifierModel.classify(featureWords)

    return render_template(
        "naiveByesSingleTweet.html",
        prediction_text="Sentiment of the tweet is {}".format(tweetSentiment))
Example #2
0
def sentimentsAnalysisSingleTweetUsingNaiveBayes():
    """ Predict sentiments of a tweet
    ---
    produces:
        - "application/xml"
        - "application/json"
    parameters:
        - name: tweet
          in: query
          type: string
          required: true
    responses:
        content:
            application/json:
                schema:
                    type: object
    """

    #Open NaiveBayesClassifier picke file
    with open("NaiveBayesClassifierModel.pkl", "rb") as NaiveBayesClassifier:
        NaiveBayesClassifierModel = pickle.load(NaiveBayesClassifier)

    #Get the tweet from UI
    mdiProjectTweet = request.args.get("tweet")

    #Process the tweet
    porcessedTweet = mdiProjectProcessTweet(mdiProjectTweet)

    #Get all the stop words
    stopWordList = mdiProjectStopWordList(
        mdiProjectReadFiles("mdiProjectFiles", "StopWords.txt"))

    #Get the features vector for a single tweet
    featureVector = mdiProjectGetFeatureVectorForSingleTweet(
        porcessedTweet, stopWordList)

    #Get the feacture words
    featureWords = mdiProjectExtractFeatures(featureVector)

    #Get sentiments based on the feature words
    tweetSentiment = NaiveBayesClassifierModel.classify(featureWords)

    return tweetSentiment
def naiveBayesClassifierTestModel():

    mdiProjectSampleTweetList = mdiProjectReadSampleFile(
        mdiProjectReadFiles("mdiProjectFiles", "SampleTrainingData.csv"))
    mdiProjectTrainingSet, mdiProjectTestingSet = mdiProjectSplitTrainTest(
        mdiProjectSampleTweetList)
    mdiProjectStopWordsList = mdiProjectStopWordList(
        mdiProjectReadFiles("mdiProjectFiles", "StopWords.txt"))

    #mdiProjectTestingSet = mdiProjectTestingSet[1:10]

    mdiProjectActualSentiments = []
    mdiProjectPredictedSentiments = []

    #Open NaiveBayesClassifier picke file
    with open("NaiveBayesClassifierModel.pkl", "rb") as NaiveBayesClassifier:
        NaiveBayesClassifierModel = pickle.load(NaiveBayesClassifier)

    for mdiProjectTestingTweetRow in mdiProjectTestingSet:

        mdiProjectSentiment = mdiProjectTestingTweetRow[0]
        mdiProjectTweet = mdiProjectTestingTweetRow[1]

        #Process the tweet
        porcessedTweet = mdiProjectProcessTweet(mdiProjectTweet)

        #Get the features vector for a single tweet
        featureVector = mdiProjectGetFeatureVectorForSingleTweet(
            porcessedTweet, mdiProjectStopWordsList)

        #Get the feacture words
        featureWords = mdiProjectExtractFeatures(featureVector)

        #Get sentiments based on the feature words
        tweetSentiment = NaiveBayesClassifierModel.classify(featureWords)

        mdiProjectActualSentiments.append(mdiProjectSentiment)
        mdiProjectPredictedSentiments.append(tweetSentiment)

    return mdiProjectActualSentiments, mdiProjectPredictedSentiments
Example #4
0
def sentimentsAnalysisBasedOnTweetTopicUsingNaiveBayes():
    """ Predict sentiments of a tweet
    ---
    parameters:
        - name: tweetTopic
          in: query
          type: string
          required: true
    responses:
        content:
            application/json:
                schema:
                    type: object
    """
    #Open NaiveBayesClassifier picke file
    with open("NaiveBayesClassifierModel.pkl", "rb") as NaiveBayesClassifier:
        NaiveBayesClassifierModel = pickle.load(NaiveBayesClassifier)

    #Get the tweet topic
    mdiProjectTweetTopic = request.args.get("tweetTopic")

    #Get the tweets based on the tweet topic and save in file
    mdiProjectSearchTweetBasedOnSearchTerm(
        mdiProjectTweeterAuthHandler(
            mdiProjectGetNormalizedTweeterConfig(
                "mdiProjectFiles/tweeterConfig.json")), mdiProjectTweetTopic)

    #Process saves tweets json file
    mdiProjectTweetTextList, mdiProjectTweetText = mdiProjectProcessTweetJsonFile(
        "mdiProjectTweets.json")

    #Get all the stop words
    stopWordList = mdiProjectStopWordList(
        mdiProjectReadFiles("mdiProjectFiles", "StopWords.txt"))

    sentiments = []

    for mdiProjectTweet in mdiProjectTweetTextList:

        #Process the tweet
        porcessedTweet = mdiProjectProcessTweet(mdiProjectTweet)

        #Get the features vector for a single tweet
        featureVector = mdiProjectGetFeatureVectorForSingleTweet(
            porcessedTweet, stopWordList)

        #Get the feacture words
        featureWords = mdiProjectExtractFeatures(featureVector)

        #Get sentiments based on the feature words
        tweetSentiment = NaiveBayesClassifierModel.classify(featureWords)
        tweetSentiment = tweetSentiment.replace("\"", "")

        sentiments.append(tweetSentiment)

    mdiProjectTweetText["sentiments"] = sentiments

    #Makes an excel file with sentimets and make it downloadable
    mdiProjectOutput = BytesIO()
    mdiProjectOutputWriter = pd.ExcelWriter(mdiProjectOutput,
                                            engine="xlsxwriter")
    mdiProjectTweetText.to_excel(mdiProjectOutputWriter,
                                 sheet_name="twitter_sentiments",
                                 encoding="utf-8",
                                 index=False)
    mdiProjectOutputWriter.save()

    memory_file = BytesIO()
    with zipfile.ZipFile(memory_file, 'w') as zf:
        names = ['twitter_sentiments.xlsx']
        files = [mdiProjectOutput]
        for i in range(len(files)):
            data = zipfile.ZipInfo(names[i])
            data.date_time = time.localtime(time.time())[:6]
            data.compress_type = zipfile.ZIP_DEFLATED
            zf.writestr(data, files[i].getvalue())
    memory_file.seek(0)
    response = make_response(
        send_file(memory_file,
                  attachment_filename='twitter_sentiments.zip',
                  as_attachment=True))
    response.headers['Access-Control-Allow-Origin'] = '*'

    return response
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 21 12:33:25 2019

@author: Santosh Sah
"""
import nltk
from MDIProjectSentimentAnalysis import (mdiProjectExtractFeatures, mdiProjectGetFeatureVectorForSingleTweet, mdiProjectGetFeatureListAndTweetListForTrainingSet, 
                                         mdiProjectProcessTweet, mdiProjectReplaceTwoOrMore, mdiProjectStopWordList)

testTweet = "Old Quarter is the best place to visit in Hanoi.. excellent and awesome ambience"

mdiProjectStopWordsList = mdiProjectStopWordList("files/StopWords.txt")
mdiProcessTweet = mdiProjectProcessTweet(testTweet)


mdiProjectGetFeatureVectorList = mdiProjectGetFeatureVectorForSingleTweet(mdiProcessTweet,mdiProjectStopWordsList)

feature_words = mdiProjectExtractFeatures(mdiProjectGetFeatureVectorList)