Пример #1
0
        /// <summary>
        /// Processes the model asynchronous.
        /// </summary>
        /// <param name="activityContext">The activity context.</param>
        /// <returns>
        /// The output model.
        /// </returns>
        protected override Task <CustomerReviewSentencesModel> ProcessModelAsync(ActivityContext activityContext)
        {
            var inputModel = activityContext.GetInputModel <CustomerReviewModel>();

            var sentences =
                SentenceRegex.Matches(inputModel.Comment)
                .Cast <Match>()
                .Select(m => m.Groups[SentenceRegexGroupName].Value)
                .ToList();

            return(Task.FromResult(new CustomerReviewSentencesModel(inputModel, sentences)));
        }
Пример #2
0
        /// <summary>
        /// Processes the model asynchronous.
        /// </summary>
        /// <param name="activityContext">The activity context.</param>
        /// <returns>
        /// The output model.
        /// </returns>
        protected override Task <EmptyModel> ProcessModelAsync(ActivityContext activityContext)
        {
            var inputModel = activityContext.GetInputModel <CustomerReviewSentencesTagWeightModel>();

            var entities = CustomerReviewSentencesTagWeightTranslator.ToEntities(inputModel);

            using (var dbContext = new CustomerReviewDbContext(this.databaseConnectionString))
            {
                dbContext.Configuration.AutoDetectChangesEnabled = false;

                dbContext.ProductReviewSentenceTags.AddRange(entities);
                dbContext.SaveChanges();
            }

            return(Task.FromResult(new EmptyModel(inputModel.CorrelationId)));
        }
Пример #3
0
        /// <summary>
        /// Processes the model asynchronous.
        /// </summary>
        /// <param name="activityContext">The activity context.</param>
        /// <returns>
        /// The output model.
        /// </returns>
        protected override async Task <CustomerReviewSentencesSentimentModel> ProcessModelAsync(
            ActivityContext activityContext)
        {
            Logger.Info(
                $"Analyzing sentiment: {activityContext.InputModel.CorrelationId}");

            try
            {
                var inputModel = activityContext.GetInputModel <CustomerReviewSentencesModel>();

                var sentences = inputModel.Sentences as IList <string> ?? inputModel.Sentences.ToList();

                var textDictionary =
                    sentences.Select((s, idx) => new KeyValuePair <int, string>(idx, s))
                    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                var sentimentResult = await SentimentClient.BatchAnalyzeAsync(textDictionary);

                var sentenceSentimentResults =
                    sentimentResult.Select(
                        kvp =>
                        new SentenceSentimentResult(
                            kvp.Key,
                            textDictionary[kvp.Key],
                            kvp.Value.SentimentType.ToString("G"),
                            kvp.Value.Score));

                return(new CustomerReviewSentencesSentimentModel(
                           inputModel.CustomerReviewModel,
                           sentenceSentimentResults));
            }
            catch (Exception ex)
            {
                Logger.Error(
                    $"Analyze sentiment failed, exception detail: {ex.GetDetailMessage()}, context: {activityContext.ToJsonIndented()}",
                    ex);

                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// Processes the model asynchronous.
        /// </summary>
        /// <param name="activityContext">The activity context.</param>
        /// <returns>
        /// The output model.
        /// </returns>
        protected override Task <CustomerReviewSentencesTagWeightModel> ProcessModelAsync(ActivityContext activityContext)
        {
            Logger.Info(
                $"Extracting tag weight: {activityContext.InputModel.CorrelationId}");

            try
            {
                var inputModel = activityContext.GetInputModel <CustomerReviewSentencesModel>();

                var sentences = inputModel.Sentences as IList <string> ?? inputModel.Sentences.ToList();

                var sentenceTagWeightResults = new List <SentenceTagWeightResult>();

                for (var sentenceIndex = 0; sentenceIndex < sentences.Count; sentenceIndex++)
                {
                    var sentence = sentences[sentenceIndex];

                    var tagWeightResult = TagWeightService.Extract(sentence);

                    sentenceTagWeightResults.AddRange(
                        tagWeightResult.Select(r =>
                                               new SentenceTagWeightResult(sentenceIndex, sentence, r.Word, r.Weight)));
                }

                return(Task.FromResult(
                           new CustomerReviewSentencesTagWeightModel(inputModel.CustomerReviewModel, sentenceTagWeightResults)));
            }
            catch (Exception ex)
            {
                Logger.Error(
                    $"Extracting tag weight failed, exception detail: {ex.GetDetailMessage()}, context: {activityContext.ToJsonIndented()}",
                    ex);

                throw;
            }
        }