Skip to content

Score Processors

NumberNormalizer(scale, decimal_rounding=None)

Bases: ScoreProcessor

Class that normalizes numeric scores to a scale in the range \([-1.0, 1.0]\)

PARAMETER DESCRIPTION
scale

Tuple where the first value is the minimum of the actual scale, second value is the maximum of the actual scale (e.g. (1, 5) represents an actual scale of scores from 1 (included) to 5 (included))

TYPE: Tuple[float, float]

decimal_rounding

If set, the normalized score will be rounded to the chosen decimal digit

TYPE: int DEFAULT: None

Source code in clayrs/content_analyzer/ratings_manager/score_processor.py
48
49
50
51
52
53
54
55
56
def __init__(self, scale: Tuple[float, float], decimal_rounding: int = None):
    super().__init__(decimal_rounding)

    if len(scale) != 2:
        raise ValueError("The voting scale should be a tuple containing exactly two values,"
                         "the minimum of the scale and the maximum!")

    self._old_min = scale[0]
    self._old_max = scale[1]

fit(score_data)

Method which will normalize the given score

PARAMETER DESCRIPTION
score_data

score that will be normalized

TYPE: float

RETURNS DESCRIPTION
float

score normalized in the interval \([-1, 1]\)

Source code in clayrs/content_analyzer/ratings_manager/score_processor.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def fit(self, score_data: float) -> float:
    """
    Method which will normalize the given score

    Args:
        score_data: score that will be normalized

    Returns:
        score normalized in the interval $[-1, 1]$
    """
    def convert_into_range(value: float, old_min: float, old_max: float, new_min: int = -1, new_max: int = 1):
        new_value = ((value - old_min) / (old_max - old_min)) * (new_max - new_min) + new_min
        if self.decimal_rounding:
            new_value = np.round(new_value, self.decimal_rounding)

        return new_value

    return convert_into_range(float(score_data), self._old_min, self._old_max)

TextBlobSentimentAnalysis(decimal_rounding=None)

Bases: SentimentAnalysis

Class that compute sentiment polarity on a textual field using TextBlob library.

The given score will be in the \([-1.0, 1.0]\) range

Source code in clayrs/content_analyzer/ratings_manager/sentiment_analysis.py
12
13
def __init__(self, decimal_rounding: int = None):
    super().__init__(decimal_rounding)

fit(score_data)

This method calculates the sentiment polarity score on textual reviews

PARAMETER DESCRIPTION
score_data

text for which sentiment polarity must be computed and considered as score

TYPE: str

RETURNS DESCRIPTION
float

The sentiment polarity of the textual data in range \([-1.0, 1.0]\)

Source code in clayrs/content_analyzer/ratings_manager/sentiment_analysis.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def fit(self, score_data: str) -> float:
    """
    This method calculates the sentiment polarity score on textual reviews

    Args:
        score_data: text for which sentiment polarity must be computed and considered as score

    Returns:
        The sentiment polarity of the textual data in range $[-1.0, 1.0]$
    """
    polarity_score = TextBlob(score_data).sentiment.polarity

    if self.decimal_rounding:
        polarity_score = round(polarity_score, self.decimal_rounding)

    return polarity_score