def analyze_sentiment(sentence):
    try:
        result = sentiment_analyzer.get_sentiment(sentence, classifier)
        print(f"sentiment: {result}")
        return jsonify({"sentiment": result})
    except Exception as e:
        print(e)
        return jsonify({"sentiment": "An error occured. Exception: " + e})
Beispiel #2
0
    def get_sentiment(self, query_article):
        """
        Processes query article for sentiment information.

        Args:
            query_article = string, article or words being queried
        Returns:
            sentiment information = dict, positive, neutral and
                                    negative sentence count.
        """
        return sentiment_analyzer.get_sentiment(query_article)
    def test_smoke(self):
        """
        Basic Smoke Test

        Args:
            self (object): Reference to the class

        Returns:
            null
        """
        my_sentiment = sentiment_analyzer.get_sentiment("This is a sentence.")
        self.assertTrue(my_sentiment is not None)
    def test_neutral_only(self):
        """
        Test a neutral sentence

        Args:
            self (object): Reference to the class

        Returns:
            null
        """
        my_sentiment = sentiment_analyzer.get_sentiment("The sky is blue.")
        self.assertTrue((my_sentiment["Overall_Sentiment"] == 'Neutral') &
                        (my_sentiment["Positive_Sentences"] == 0) &
                        (my_sentiment["Negative_Sentences"] == 0) &
                        (my_sentiment["Neutral_Sentences"] == 1) &
                        (my_sentiment["Total_Sentences"] == 1))
    def test_positive_only(self):
        """
        Test a positive sentence

        Args:
            self (object): Reference to the class

        Returns:
            null
        """
        my_sentiment = sentiment_analyzer.get_sentiment("I am happy.")
        self.assertTrue((my_sentiment["Overall_Sentiment"] == 'Positive') &
                        (my_sentiment["Positive_Sentences"] == 1) &
                        (my_sentiment["Negative_Sentences"] == 0) &
                        (my_sentiment["Neutral_Sentences"] == 0) &
                        (my_sentiment["Total_Sentences"] == 1))
    def test_mixed_sentiments(self):
        """
        Test a text with positive, negative and neutral sentences.

        Args:
            self (object): Reference to the class

        Returns:
            null
        """
        my_sentiment = sentiment_analyzer.get_sentiment(
            "I am happy. I am sad. The sky is blue.")
        self.assertTrue((my_sentiment["Overall_Sentiment"] == 'Neutral') &
                        (my_sentiment["Positive_Sentences"] == 1) &
                        (my_sentiment["Negative_Sentences"] == 1) &
                        (my_sentiment["Neutral_Sentences"] == 1) &
                        (my_sentiment["Total_Sentences"] == 3))