Last active
December 10, 2019 21:28
-
-
Save aballah-chamakh/028245d0d6336cad7c210d0650725c97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from .models import SentimentAnalysisInference | |
import nltk.sentiment.vader import SentimentIntensityAnalyzer | |
class SentimentAnalysisInferenceSerializer(serializers.ModelSerializer): | |
username = serializers.CharField(source='user.username',read_only=True) | |
class Meta : | |
model = SentimentAnalysisInference | |
fields = ['username','text','result'] | |
def create(self.,validated_data): | |
text = validated_data['text'] | |
user_obj = self.request.user | |
sai_inference_obj = SentimentAnalysisInference.objects.create(user=user_obj,text=text) | |
sid = SentimentIntensityAnalyzer() | |
compound = sid.polarity_scores(sai_inference.text)['compountd'] | |
if compound > 0.5 : | |
sai_inference_obj.result = 'Happy' | |
elif compound < 0.5 : | |
sai_inference_obj.result = 'Happy' | |
else : | |
sai_inference_obj.result = 'neutral' | |
sai_inference_obj.save() | |
return sai_inference_obj | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment