public void GetOccurences()
        {
            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.GetOccurences().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(2, data[3].Probability);
            Assert.AreEqual("Sadness", data[5].Data);
            Assert.AreEqual(2, data[5].Probability);
            Assert.AreEqual("Joy", data[4].Data);
            Assert.AreEqual(1, data[4].Probability);
        }
Beispiel #2
0
        private void PopulateAttributes(SentimentVector vector, WordEx word, IWordItem original)
        {
            var record = nrcDictionary.FindRecord(word);

            vector.ExtractData(record);
            word.Emotions   = record?.GetDefinedCategories().ToArray();
            word.Attributes = original.Inquirer.Records.SelectMany(item => item.Description.Attributes).ToArray();
        }
        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();
        }
        public TrainingClient(ILogger <TrainingClient> log, IClientContext clientContext)
        {
            this.clientContext = clientContext ?? throw new ArgumentNullException(nameof(clientContext));
            if (string.IsNullOrEmpty(clientContext.Context.SvmPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(clientContext.Context.SvmPath));
            }

            this.log         = log ?? throw new ArgumentNullException(nameof(log));
            SentimentVector  = new SentimentVector();
            analyze          = new AnalyseReviews();
            featureExtractor = new MainAspectHandler(new AspectContextFactory());
            sentimentVector  = new SentimentVector();
        }
Beispiel #5
0
        public void Construct()
        {
            SentimentVector vector = new SentimentVector();

            Assert.AreEqual(0, vector.Anger);
            Assert.AreEqual(0, vector.Anticipation);
            Assert.AreEqual(0, vector.Disgust);
            Assert.AreEqual(0, vector.Fear);
            Assert.AreEqual(0, vector.Joy);
            Assert.AreEqual(0, vector.Trust);
            Assert.AreEqual(0, vector.Sadness);
            Assert.AreEqual(0, vector.Surprise);
            Assert.AreEqual(0, vector.Total);
            Assert.AreEqual(0, vector.TotalSum);
        }
        public static SentimentVector Extract(this INRCDictionary dictionary, IEnumerable <IWordItem> words)
        {
            if (dictionary is null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            if (words is null)
            {
                throw new ArgumentNullException(nameof(words));
            }

            var vector = new SentimentVector();

            dictionary.ExtractToVector(vector, words);
            return(vector);
        }
        public TestingClient(ILogger <TestingClient> log, IClientContext clientContext)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            if (string.IsNullOrEmpty(clientContext.Context.SvmPath))
            {
                DisableSvm = true;
            }

            this.clientContext = clientContext ?? throw new ArgumentNullException(nameof(clientContext));
            this.log           = log;
            AspectSentiment    = new AspectSentimentTracker(new ContextSentimentFactory());
            SentimentVector    = new SentimentVector();
        }
        public void GetProbabilities()
        {
            var vector = new SentimentVector();

            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem("kill") });
            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem("kill") });
            dictionary.Object.ExtractToVector(vector, new[] { new TestWordItem("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));
        }
Beispiel #9
0
        public void Save(ProcessingContext context)
        {
            var vector = new SentimentVector();

            if (ExtractStyle)
            {
                foreach (var word in context.Processed.Words)
                {
                    vector.ExtractData(dictionary.FindRecord(word));
                }
            }

            lock (csvDataOut)
            {
                csvDataOut.WriteField(context.Original.Id);
                csvDataOut.WriteField(context.Original.DocumentTime);
                csvDataOut.WriteField(context.Original.Stars);
                csvDataOut.WriteField(context.Adjustment.Rating.StarsRating);
                csvDataOut.WriteField(context.Review.GetAllSentiments().Length);
                if (ExtractStyle)
                {
                    csvDataOut.WriteField(vector.Anger);
                    csvDataOut.WriteField(vector.Anticipation);
                    csvDataOut.WriteField(vector.Disgust);
                    csvDataOut.WriteField(vector.Fear);
                    csvDataOut.WriteField(vector.Joy);
                    csvDataOut.WriteField(vector.Sadness);
                    csvDataOut.WriteField(vector.Surprise);
                    csvDataOut.WriteField(vector.Trust);
                    csvDataOut.WriteField(vector.Total);
                }

                csvDataOut.NextRecord();
                csvDataOut.Flush();
            }

            if (Debug)
            {
                resultsWriter.WriteObject(context.Processed);
            }
        }
        public static SentimentVector Extract(this INRCDictionary dictionary, IEnumerable <WordEx> words)
        {
            if (dictionary is null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            if (words is null)
            {
                throw new ArgumentNullException(nameof(words));
            }

            var vector = new SentimentVector();

            foreach (var word in words)
            {
                vector.ExtractData(dictionary.FindRecord(word));
            }

            return(vector);
        }
        public static void ExtractToVector(this INRCDictionary dictionary, SentimentVector vector, IEnumerable <IWordItem> words)
        {
            if (dictionary is null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            if (vector is null)
            {
                throw new ArgumentNullException(nameof(vector));
            }

            if (words is null)
            {
                throw new ArgumentNullException(nameof(words));
            }

            foreach (var word in words)
            {
                vector.ExtractData(dictionary.FindRecord(word));
            }
        }
Beispiel #12
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);
        }
Beispiel #13
0
 public static VectorData GetVector(this SentimentVector vector, NormalizationType normalization)
 {
     return(vector.GetTree(false).CreateVector(normalization, false));
 }