예제 #1
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;
            }
        }
예제 #2
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;
            }
        }