I'm using Google's Natural Language API to generate the sentiment from a piece of text.
From Google's own docs:
Score of the sentiment ranges between -1.0 (negative) and 1.0 (positive) and corresponds to the overall emotional leaning of the text. Magnitude indicates the overall strength of emotion (both positive and negative) within the given text, between 0.0 and +inf. Unlike score, magnitude is not normalized; each expression of emotion within the text (both positive and negative) contributes to the text's magnitude (so longer text blocks may have greater magnitudes).
My goal is to get a single number of the "general sentiment" between 1 and 10. Google's old API used to return sentiment using a single number between -100 and +100 which was easy to map using something along the lines of.
function map_range(value, low1, high1, low2, high2) {
return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}
Math.round(map_range(SENTIMENT, -100, 100, 0, 10));
As Google have made the old API depreciated I've jumped to the new one, but in order to restore functionality to the rest of my app I'll need to map these two numbers back to between 1 and 10. Hopefully that makes sense!
via Aloogy
No comments:
Post a Comment