private SentimentAnalysisResults ScoreValence(List <double> sentiments, SentiText text) { if (sentiments.Count == 0) { return(new SentimentAnalysisResults()); //will return with all 0 } var sum = sentiments.Sum(); var puncAmplifier = PunctuationEmphasis(text.Text); sum += Math.Sign(sum) * puncAmplifier; var compound = SentimentUtils.Normalize(sum); var sifted = SiftSentimentScores(sentiments); if (sifted.PosSum > Math.Abs(sifted.NegSum)) { sifted.PosSum += puncAmplifier; } else if (sifted.PosSum < Math.Abs(sifted.NegSum)) { sifted.NegSum -= puncAmplifier; } var total = sifted.PosSum + Math.Abs(sifted.NegSum) + sifted.NeuCount; return(new SentimentAnalysisResults { Compound = Math.Round(compound, 4), Positive = Math.Round(Math.Abs(sifted.PosSum / total), 3), Negative = Math.Round(Math.Abs(sifted.NegSum / total), 3), Neutral = Math.Round(Math.Abs(sifted.NeuCount / total), 3) }); }
// Return metrics for positive, negative and neutral sentiment based on the input text. public SentimentAnalysisResults PolarityScores(string input) { var sentiText = new SentiText(this, input); var sentiments = AnalyseBoosters(sentiText); sentiments = ButCheck(sentiText.WordsAndEmoticonsLowerCase, sentiments); return(ScoreValence(sentiments, sentiText)); }
private List <double> AnalyseBoosters(SentiText sentiText) { var size = sentiText.WordsAndEmoticonsLowerCase.Count; var sentiments = new List <double>(); for (var i = 0; i < size; i++) { var lowerCaseWord = sentiText.WordsAndEmoticonsLowerCase[i]; double valence = 0; if (i < size - 1 && lowerCaseWord == "kind" && sentiText.WordsAndEmoticonsLowerCase[i + 1] == "of" || SentimentUtils.BoosterDict.ContainsKey(lowerCaseWord)) { sentiments.Add(valence); continue; } sentiments = SentimentValence(valence, sentiText, sentiText.WordsAndEmoticons[i], lowerCaseWord, i, sentiments); } return(sentiments); }
private List <double> SentimentValence(double valence, SentiText sentiText, string word, string lowerCaseWord, int i, List <double> sentiments) { if (!Lexicon.ContainsKey(lowerCaseWord)) { sentiments.Add(valence); return(sentiments); } var isCapDiff = sentiText.IsCapDifferential; // adjust if shouting valence = Lexicon[lowerCaseWord]; if (isCapDiff && word.IsUpper()) { if (valence > 0) { valence += SentimentUtils.CIncr; } else { valence -= SentimentUtils.CIncr; } } for (var startI = 0; startI < 3 && i > startI; startI++) { var offset = i - (startI + 1); var lcWord = sentiText.WordsAndEmoticonsLowerCase[offset]; if (Lexicon.ContainsKey(lcWord)) { continue; } var s = SentimentUtils.ScalarIncDec(lcWord, sentiText.WordsAndEmoticons[offset], valence, isCapDiff); if (startI == 1 && !s.Equals(0)) { s = s * 0.95; } if (startI == 2 && !s.Equals(0)) { s = s * 0.9; } valence = valence + s; valence = NeverCheck(valence, sentiText.WordsAndEmoticonsLowerCase, startI, i); if (startI == 2) { valence = IdiomsCheck(valence, sentiText.WordsAndEmoticonsLowerCase, i); } } valence = LeastCheck(valence, sentiText.WordsAndEmoticonsLowerCase, i); sentiments.Add(valence); return(sentiments); }