Skip to content

Sentiment Analysis

SentimentAnalysisFilter

Source code in engines/contentFilterEngine/other_approaches/sentiment_analysis.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class SentimentAnalysisFilter:
    def __init__(self, threshold=0.1):
        """
        Initializes the SentimentAnalysisFilter.

        Parameters:
        - threshold (float): The sentiment polarity threshold to trigger actions.
                             Positive values can indicate positive sentiment,
                             negative values indicate negative sentiment.
        """
        self.threshold = threshold

    def analyze_sentiment(self, content):
        """
        Analyzes the sentiment of the given content.

        Parameters:
        - content (str): The content to analyze.

        Returns:
        - float: The sentiment polarity score ranging from -1.0 to 1.0.
        """
        blob = TextBlob(content)
        return blob.sentiment.polarity

    def filter_content(self, content):
        """
        Filters the content based on its sentiment.

        Parameters:
        - content (str): The content to be filtered.

        Returns:
        - dict: A dictionary with 'status' and 'sentiment_score'.
        """
        sentiment_score = self.analyze_sentiment(content)

        if sentiment_score < -self.threshold:
            return {'status': 'negative', 'sentiment_score': sentiment_score}
        elif sentiment_score > self.threshold:
            return {'status': 'positive', 'sentiment_score': sentiment_score}
        else:
            return {'status': 'neutral', 'sentiment_score': sentiment_score}

__init__(threshold=0.1)

Initializes the SentimentAnalysisFilter.

  • threshold (float): The sentiment polarity threshold to trigger actions. Positive values can indicate positive sentiment, negative values indicate negative sentiment.
Source code in engines/contentFilterEngine/other_approaches/sentiment_analysis.py
 4
 5
 6
 7
 8
 9
10
11
12
13
def __init__(self, threshold=0.1):
    """
    Initializes the SentimentAnalysisFilter.

    Parameters:
    - threshold (float): The sentiment polarity threshold to trigger actions.
                         Positive values can indicate positive sentiment,
                         negative values indicate negative sentiment.
    """
    self.threshold = threshold

analyze_sentiment(content)

Analyzes the sentiment of the given content.

Parameters: - content (str): The content to analyze.

Returns: - float: The sentiment polarity score ranging from -1.0 to 1.0.

Source code in engines/contentFilterEngine/other_approaches/sentiment_analysis.py
15
16
17
18
19
20
21
22
23
24
25
26
def analyze_sentiment(self, content):
    """
    Analyzes the sentiment of the given content.

    Parameters:
    - content (str): The content to analyze.

    Returns:
    - float: The sentiment polarity score ranging from -1.0 to 1.0.
    """
    blob = TextBlob(content)
    return blob.sentiment.polarity

filter_content(content)

Filters the content based on its sentiment.

Parameters: - content (str): The content to be filtered.

Returns: - dict: A dictionary with 'status' and 'sentiment_score'.

Source code in engines/contentFilterEngine/other_approaches/sentiment_analysis.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def filter_content(self, content):
    """
    Filters the content based on its sentiment.

    Parameters:
    - content (str): The content to be filtered.

    Returns:
    - dict: A dictionary with 'status' and 'sentiment_score'.
    """
    sentiment_score = self.analyze_sentiment(content)

    if sentiment_score < -self.threshold:
        return {'status': 'negative', 'sentiment_score': sentiment_score}
    elif sentiment_score > self.threshold:
        return {'status': 'positive', 'sentiment_score': sentiment_score}
    else:
        return {'status': 'neutral', 'sentiment_score': sentiment_score}