예제 #1
0
        /// <summary>
        /// Analyze sentiment of English input.
        /// The results of analysis are accumulated until the next call of GetSentimentAndFlush().
        /// </summary>
        /// <param name="input">English string</param>
        public override void Analyze(string input)
        {
            if (!HasStart)
            {
                return;
            }

            // attach spelling correction engine
            var suggestion = symSpell.WordSegmentation(input.ToLowerInvariant(), 2);

            Console.WriteLine(suggestion.correctedString);

            List <string> tokens = suggestion.correctedString.Split(' ').ToList();

            foreach (string word in tokens)
            {
                if (englishSentimentDictionary.ContainsKey(word))
                {
                    void UpdateAggregate(object[] args)
                    {
                        EnglishWordSentiment sentiment = args[0] as EnglishWordSentiment;
                        int weight = 1;

                        if (sentiment.GetValence() != WordSentiment.Valence.NULL)
                        {
                            aggregateValence[(int)sentiment.GetValence()] += weight;
                        }
                        if (sentiment.GetStateIntensity() != WordSentiment.StateIntensity.NULL)
                        {
                            aggregateStateIntensity[(int)sentiment.GetStateIntensity()] += weight;
                        }
                        if (sentiment.GetEmotion() != WordSentiment.Emotion.NULL)
                        {
                            aggregateEmotion[(int)sentiment.GetEmotion()] += weight;
                        }
                        if (sentiment.GetJudgment() != WordSentiment.Judgment.NULL)
                        {
                            aggregateJudgment[(int)sentiment.GetJudgment()] += weight;
                        }
                        if (sentiment.GetAgreement() != WordSentiment.Agreement.NULL)
                        {
                            aggregateAgreement[(int)sentiment.GetAgreement()] += weight;
                        }
                        if (sentiment.GetIntention() != WordSentiment.Intention.NULL)
                        {
                            aggregateIntention[(int)sentiment.GetIntention()] += weight;
                        }
                    }

                    Util.TaskQueue.Add("aggregateEnglishSentiment", UpdateAggregate,
                                       englishSentimentDictionary[word]);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Returns accumulated sentiment analysis results and reset them.
        /// </summary>
        /// <returns>The results of sentiment analysis</returns>
        public override WordSentiment GetSentimentAndFlush()
        {
            WordSentiment ret = new EnglishWordSentiment("", "", "", "", "", "", "");

            if (!HasStart)
            {
                return(ret);
            }

            void GetAggregate(object[] args)
            {
                Random r = new Random();

                WordSentiment.Valence        v;
                WordSentiment.StateIntensity si;
                WordSentiment.Emotion        e;
                WordSentiment.Judgment       j;
                WordSentiment.Agreement      a;
                WordSentiment.Intention      i;

                if (aggregateValence.Sum() <= 0)
                {
                    v = WordSentiment.Valence.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateValence.Length; k++)
                    {
                        if (aggregateValence[k] > max)
                        {
                            max    = aggregateValence[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateValence[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    v = (WordSentiment.Valence)argmax[r.Next(argmax.Count)];
                }

                if (aggregateStateIntensity.Sum() <= 0)
                {
                    si = WordSentiment.StateIntensity.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateStateIntensity.Length; k++)
                    {
                        if (aggregateStateIntensity[k] > max)
                        {
                            max    = aggregateStateIntensity[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateStateIntensity[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    si = (WordSentiment.StateIntensity)argmax[r.Next(argmax.Count)];
                }

                if (aggregateEmotion.Sum() <= 0)
                {
                    e = WordSentiment.Emotion.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateEmotion.Length; k++)
                    {
                        if (aggregateEmotion[k] > max)
                        {
                            max    = aggregateEmotion[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateEmotion[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    e = (WordSentiment.Emotion)argmax[r.Next(argmax.Count)];
                }

                if (aggregateJudgment.Sum() <= 0)
                {
                    j = WordSentiment.Judgment.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateJudgment.Length; k++)
                    {
                        if (aggregateJudgment[k] > max)
                        {
                            max    = aggregateJudgment[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateJudgment[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    j = (WordSentiment.Judgment)argmax[r.Next(argmax.Count)];
                }

                if (aggregateAgreement.Sum() <= 0)
                {
                    a = WordSentiment.Agreement.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateAgreement.Length; k++)
                    {
                        if (aggregateAgreement[k] > max)
                        {
                            max    = aggregateAgreement[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateAgreement[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    a = (WordSentiment.Agreement)argmax[r.Next(argmax.Count)];
                }

                if (aggregateIntention.Sum() <= 0)
                {
                    i = WordSentiment.Intention.NULL;
                }
                else
                {
                    int        max    = 0;
                    List <int> argmax = new List <int>();
                    for (int k = 0; k < aggregateIntention.Length; k++)
                    {
                        if (aggregateIntention[k] > max)
                        {
                            max    = aggregateIntention[k];
                            argmax = new List <int>()
                            {
                                k
                            };
                        }
                        else if (aggregateIntention[k] == max)
                        {
                            argmax.Add(k);
                        }
                    }
                    i = (WordSentiment.Intention)argmax[r.Next(argmax.Count)];
                }

                ret = new EnglishWordSentiment("", v, si, e, j, a, i);
            }

            Util.TaskQueue.Add("aggregateEnglishSentiment", GetAggregate);
            Util.TaskQueue.Add("aggregateEnglishSentiment", InitializeAggregate);

            return(ret);
        }