Esempio n. 1
0
        /// <summary>
        /// Analyzes the specified phrase.
        /// </summary>
        /// <param name="phrase">The phrase.</param>
        /// <returns></returns>
        public SentimentalInfo Analyze(string phrase)
        {
            string str = Util.ToUnicode(phrase);

            SentenceInfo pos = this.Positivity(str);
            SentenceInfo neg = this.Negativity(str);

            SentimentalInfo result = new SentimentalInfo();

            result.Positives = pos;
            result.Negatives = neg;

            result.Score       = decimal.Divide((pos.Score - neg.Score), 10);
            result.Comparative = decimal.Divide(pos.Comparative - neg.Comparative, 10);
            result.Comparative = Math.Round(result.Comparative, 2, MidpointRounding.ToEven);

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Negativities the specified phrase.
        /// </summary>
        /// <param name="phrase">The phrase.</param>
        /// <returns></returns>
        private SentenceInfo Negativity(string phrase)
        {
            string strNormalized = Normalize(phrase); // Create a temp copy
            int    hits          = 0;

            List <string> tokens     = strNormalized.ToLowerInvariant().Split(' ').ToList <string>();
            List <string> words      = new List <string>();
            List <string> adjectives = new List <string>();

            for (int i = 0; i < tokens.Count; i++)
            {
                if (this.IsWord(tokens[i]) == true)
                {
                    hits += 1;
                    words.Add(tokens[i]);
                }

                if (this.IsAdjective(tokens[i], negatives) == true)
                {
                    if (i != 0)
                    {
                        if (this.IsAdjective(tokens[i - 1], positives) == true)
                        {
                            continue;
                        }
                    }

                    hits *= 2;
                    adjectives.Add(tokens[i]);
                }
            }

            SentenceInfo sentenceInfo = new SentenceInfo();

            sentenceInfo.Score       = hits;
            sentenceInfo.Comparative = decimal.Divide(hits, tokens.Count);
            sentenceInfo.Words       = words.ToArray();
            sentenceInfo.Adjectives  = adjectives.ToArray();

            return(sentenceInfo);
        }
        /// <summary>
        /// Positivities the specified phrase.
        /// </summary>
        /// <param name="phrase">The phrase.</param>
        /// <returns></returns>
        private SentenceInfo Positivity(string phrase)
        {
            string strNormalized = Normalize(phrase); // Create a temp copy
            int hits = 0;

            List<string> tokens = strNormalized.ToLowerInvariant().Split(' ').ToList<string>();
            List<string> words = new List<string>();
            List<string> adjectives = new List<string>();

            for (int i = 0; i < tokens.Count; i++)
            {
                if (this.IsWord(tokens[i]) == true)
                {
                    hits += 1;
                    words.Add(tokens[i]);
                }

                if (this.IsAdjective(tokens[i], positives) == true)
                {
                    if (i != 0)
                    {
                        if (this.IsAdjective(tokens[i - 1], negatives) == true)
                        {
                            continue;
                        }
                    }

                    hits *= 2;
                    adjectives.Add(tokens[i]);
                }
            }

            SentenceInfo sentenceInfo = new SentenceInfo();
            sentenceInfo.Score = hits;
            sentenceInfo.Comparative = decimal.Divide(hits, tokens.Count);
            sentenceInfo.Words = words.ToArray();
            sentenceInfo.Adjectives = adjectives.ToArray();

            return sentenceInfo;
        }