public void GetProbabilities()
        {
            var vector = new SentimentVector();

            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem {
                                                                  Text = "kill"
                                                              } });
            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem {
                                                                  Text = "kill"
                                                              } });
            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem {
                                                                  Text = "love"
                                                              } });
            var data = vector.GetProbabilities().ToArray();

            Assert.AreEqual(8, data.Length);
            Assert.AreEqual("Anger", data[0].Data);
            Assert.AreEqual(0, data[0].Probability);
            Assert.AreEqual("Fear", data[3].Data);
            Assert.AreEqual(0.67, Math.Round(data[3].Probability, 2));
            Assert.AreEqual("Sadness", data[5].Data);
            Assert.AreEqual(0.67, Math.Round(data[5].Probability, 2));
            Assert.AreEqual("Joy", data[4].Data);
            Assert.AreEqual(0.33, Math.Round(data[4].Probability, 2));
        }
        protected override void Additional()
        {
            SentimentVector vector = dictionary.Extract(review.ImportantWords);

            foreach (var probability in vector.GetProbabilities().Where(item => item.Probability > 0))
            {
                AddItem(null, $"DIMENSION_{probability.Data}", probability.Probability);
            }

            base.Additional();
        }
Exemple #3
0
        public Document ReparseDocument(IRatingAdjustment adjustment)
        {
            if (adjustment?.Review?.Document == null)
            {
                throw new ArgumentNullException(nameof(adjustment));
            }

            adjustment.CalculateRating();
            var document = new Document();

            document.DocumentTime = adjustment.Review.Document.DocumentTime;
            document.Stars        = adjustment.Review.Document.Stars;
            document.Id           = adjustment.Review.Document.Id;
            document.Author       = adjustment.Review.Document.Author;

            if (adjustment.Rating != null)
            {
                document.Stars = adjustment.Rating.StarsRating;
            }

            bool buildText = false;

            if (adjustment.Review.Text != null)
            {
                document.Text = adjustment.Review.Text;
                buildText     = true;
            }

            var vector = new SentimentVector();

            foreach (var sentence in adjustment.Review.Sentences)
            {
                if (string.IsNullOrWhiteSpace(sentence.Text))
                {
                    continue;
                }

                var sentenceItem = new SentenceItem(sentence.Text);
                document.Add(sentenceItem, buildText);
                foreach (var wordItem in sentence.Occurrences)
                {
                    var word = WordExFactory.Construct(wordItem);
                    word.IsStop           = wordItem.IsStopWord;
                    word.Phrase           = wordItem.Parent?.Text;
                    word.NormalizedEntity = wordItem.NormalizedEntity;
                    word.Span             = word.Text;
                    var sentiment = wordItem.Relationship.Sentiment?.DataValue;
                    if (sentiment != null)
                    {
                        word.Value = sentiment.Value;
                        word.Span  = wordItem.Relationship.Sentiment.Span;
                    }

                    word.IsAspect = wordItem.IsFeature;
                    SentimentValue value = adjustment.GetSentiment(wordItem);
                    if (value != null)
                    {
                        word.CalculatedValue = value.DataValue.Value;
                    }
                    else if (word.Value != null)
                    {
                        word.CalculatedValue = 0;
                    }

                    if (adjustment.Review.Context.ExtractAttributes)
                    {
                        PopulateAttributes(vector, word, wordItem);
                    }

                    sentenceItem.Add(word);
                }
            }

            document.Text = adjustment.Review.Document.Text;
            if (adjustment.Review.Context.ExtractAttributes)
            {
                document.Attributes = vector.GetProbabilities().ToDictionary(item => item.Data, item => item.Probability.ToString());
            }

            return(document);
        }