<commit_before>﻿using Glav.CognitiveServices.FluentApi.Core;
using Glav.CognitiveServices.FluentApi.Core.Communication;
using Glav.CognitiveServices.FluentApi.TextAnalytic.Domain.ApiResponses;
using System;

namespace Glav.CognitiveServices.FluentApi.TextAnalytic.Domain
{
    public sealed class SentimentResult : BaseResponseResult<SentimentResultResponseRoot>
    {
        public SentimentResult(ICommunicationResult apiCallResult)
        {
            ApiCallResult = apiCallResult;
            AddResultToCollection();
        }

        private void AddResultToCollection()
        {
            if (ApiCallResult == null)
            {
                ResponseData = new SentimentResultResponseRoot { errors = new ApiErrorResponse[] { new ApiErrorResponse { id = 1, message = "No data returned." } } };
                ActionSubmittedSuccessfully = false;
                return;
            }

            try
            {
                ResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<SentimentResultResponseRoot>(ApiCallResult.Data);
                if (ResponseData != null && ResponseData.errors != null && ResponseData.errors.Length > 0)
                {
                    // If all that failed, we try just parsing into the error structure
                    ActionSubmittedSuccessfully = false;<commit_msg>Added better failed call detection where a call can partially succeed<commit_after>﻿using Glav.CognitiveServices.FluentApi.Core;
using Glav.CognitiveServices.FluentApi.Core.Communication;
using Glav.CognitiveServices.FluentApi.TextAnalytic.Domain.ApiResponses;
using System;

namespace Glav.CognitiveServices.FluentApi.TextAnalytic.Domain
{
    public sealed class SentimentResult : BaseResponseResult<SentimentResultResponseRoot>
    {
        public SentimentResult(ICommunicationResult apiCallResult)
        {
            ApiCallResult = apiCallResult;
            AddResultToCollection();
        }

        private void AddResultToCollection()
        {
            if (ApiCallResult == null)
            {
                ResponseData = new SentimentResultResponseRoot { errors = new ApiErrorResponse[] { new ApiErrorResponse { id = 1, message = "No data returned." } } };
                ActionSubmittedSuccessfully = false;
                return;
            }

            try
            {
                ResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<SentimentResultResponseRoot>(ApiCallResult.Data);
                // If we only have errors, then the call was not successfull. However we can have a situation where multiple
                // documents are submitted and some were processed ok but some were not. This indicates the action was successful
                // but some some documents were not processed or valid.
                if (ResponseData != null 
                        && ResponseData.errors != null 
                        && ResponseData.errors.Length > 0 
                        && (ResponseData.documents == null || ResponseData.documents.Length == 0))
                {
                    // If all that failed, we try just parsing into the error structure
                    ActionSubmittedSuccessfully = false;
{'fraction_non_alphanumeric': 0.06809701492537314, 'fraction_numerical': 0.001554726368159204, 'mean_word_length': 2.2396777442094664, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '17818', 'n_tokens_mistral': 794, 'n_tokens_neox': 734, 'n_words': 195}