Spaces:
Runtime error
Runtime error
Create postprocess.py
Browse files- postprocess.py +76 -0
postprocess.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def softmax(x: np.ndarray, axis=1) -> np.ndarray:
|
| 5 |
+
"""
|
| 6 |
+
Computes softmax array along the specified axis.
|
| 7 |
+
"""
|
| 8 |
+
e_x = np.exp(x)
|
| 9 |
+
return e_x / e_x.sum(axis=axis, keepdims=True)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def calibrate_sentiment_score(
|
| 13 |
+
sentiment: float,
|
| 14 |
+
thresh_neg: float,
|
| 15 |
+
thresh_pos: float,
|
| 16 |
+
zero: float = 0,
|
| 17 |
+
) -> float:
|
| 18 |
+
if thresh_neg != (zero - 1) / 2:
|
| 19 |
+
alpha_neg = -(3 * zero - 1 - 4 * thresh_neg) / (2 * zero - 2 - 4 * thresh_neg) / 2
|
| 20 |
+
if -1 < alpha_neg and alpha_neg < 0:
|
| 21 |
+
raise ValueError(f"Incorrect value: {thresh_neg=} is too far from -0.5!")
|
| 22 |
+
if thresh_pos != (zero + 1) / 2:
|
| 23 |
+
alpha_pos = -(4 * thresh_pos - 1 - 3 * zero) / (2 + 2 * zero - 4 * thresh_pos) / 2
|
| 24 |
+
if 0 < alpha_pos and alpha_pos < 1:
|
| 25 |
+
raise ValueError(f"Incorrect value: {thresh_pos=} is too far from 0.5!")
|
| 26 |
+
if sentiment < 0:
|
| 27 |
+
return (2 * zero - 2 - 4 * thresh_neg) * sentiment**2 + (3 * zero - 1 - 4 * thresh_neg) * sentiment + zero
|
| 28 |
+
elif sentiment > 0:
|
| 29 |
+
return (2 + 2 * zero - 4 * thresh_pos) * sentiment**2 + (4 * thresh_pos - 1 - 3 * zero) * sentiment + zero
|
| 30 |
+
return zero
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def calibrate_sentiment(
|
| 34 |
+
sentiments: np.ndarray[float],
|
| 35 |
+
thresh_neg: float,
|
| 36 |
+
thresh_pos: float,
|
| 37 |
+
zero: float,
|
| 38 |
+
) -> np.ndarray[np.float64]:
|
| 39 |
+
result = np.array(
|
| 40 |
+
[
|
| 41 |
+
calibrate_sentiment_score(sentiment, thresh_neg=thresh_neg, thresh_pos=thresh_pos, zero=zero)
|
| 42 |
+
for sentiment in sentiments
|
| 43 |
+
]
|
| 44 |
+
)
|
| 45 |
+
return result.astype(np.float64)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def scale_value(value, in_min, in_max, out_min, out_max):
|
| 49 |
+
if in_min <= value <= in_max:
|
| 50 |
+
scaled_value = (value - in_min) / (in_max - in_min) * (out_max - out_min) + out_min
|
| 51 |
+
return scaled_value.round(3)
|
| 52 |
+
else:
|
| 53 |
+
raise ValueError(f"Input value must be in the range [{in_min}, {in_max}]")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_sentiment(
|
| 58 |
+
logits: np.ndarray,
|
| 59 |
+
thresh_neg: float,
|
| 60 |
+
thresh_pos: float,
|
| 61 |
+
zero: float,
|
| 62 |
+
):
|
| 63 |
+
probabilities = softmax(logits, axis=1)
|
| 64 |
+
sentiments = np.matmul(probabilities, np.arange(5)) / 2 - 1
|
| 65 |
+
score = calibrate_sentiment(
|
| 66 |
+
sentiments=sentiments,
|
| 67 |
+
thresh_neg=thresh_neg,
|
| 68 |
+
thresh_pos=thresh_pos,
|
| 69 |
+
zero=zero,
|
| 70 |
+
)[0]
|
| 71 |
+
if score < -0.33:
|
| 72 |
+
return scale_value(score, -1, -0.33, 0, 1), "NEGATIVE"
|
| 73 |
+
elif score < 0.33:
|
| 74 |
+
return scale_value(score, -0.33, 0.33, 0, 1), "NEUTRAL"
|
| 75 |
+
else:
|
| 76 |
+
return scale_value(score, 0.33, 1, 0, 1), "POSITIVE"
|