def sentimentsAnalysisBasedOnTweetTopicUsingLogisticRegression(): #Get the tweet topic mdiProjectTweetTopic = request.form.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") mdiProjectTweetList = mdiProjectCleanTweet(mdiProjectTweetTextList) sample = mdiProjectTfidfvectorizer.transform(mdiProjectTweetList).toarray() sentiments = mdiProjectLogisticRegressionclassifierModel.predict(sample) 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
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