/// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            DetectEntitiesResponse response = new DetectEntitiesResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("Entities", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <Entity, EntityUnmarshaller>(EntityUnmarshaller.Instance);
                    response.Entities = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("PaginationToken", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.PaginationToken = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("UnmappedAttributes", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <UnmappedAttribute, UnmappedAttributeUnmarshaller>(UnmappedAttributeUnmarshaller.Instance);
                    response.UnmappedAttributes = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
示例#2
0
        private async Task <string> GetComprehendData(string transcriptText, string key)
        {
            JObject transcriptJSON        = JObject.Parse(transcriptText);
            string  test                  = (string)transcriptJSON["Text"];
            AmazonComprehendClient client = new AmazonComprehendClient();


            DetectEntitiesRequest entitiesRequest = new DetectEntitiesRequest();

            entitiesRequest.LanguageCode = LanguageCode.En;
            entitiesRequest.Text         = test;



            DetectSentimentRequest sentimentRequest = new DetectSentimentRequest();

            sentimentRequest.LanguageCode = LanguageCode.En;
            sentimentRequest.Text         = test;

            DetectKeyPhrasesRequest keyPhrasesRequest = new DetectKeyPhrasesRequest();

            keyPhrasesRequest.LanguageCode = LanguageCode.En;
            keyPhrasesRequest.Text         = test;

            DetectEntitiesResponse entitiesResponse = await client.DetectEntitiesAsync(entitiesRequest);

            DetectSentimentResponse setimentResponse = await client.DetectSentimentAsync(sentimentRequest);

            DetectKeyPhrasesResponse keyPhrasesResponse = await client.DetectKeyPhrasesAsync(keyPhrasesRequest);


            CreateKeyPhraseCSV(key, keyPhrasesResponse);
            CreateSetimentJSON(key, setimentResponse);



            //now send the file to s3

            //we need to write two different files, one for setiment and one for Key Phrases.

            return(string.Empty);
        }
示例#3
0
        /// <summary>
        /// A function for responding to S3 create events. It uses Amazon Comprehend to detect entities, sentiment
        /// and save them to S3.
        /// </summary>
        /// <param name="s3Event"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(S3Event s3Event, ILambdaContext context)
        {
            foreach (var record in s3Event.Records)
            {
                var detectEntitiesResponse  = new DetectEntitiesResponse();
                var detectSentimentResponse = new DetectSentimentResponse();
                try
                {
                    using (GetObjectResponse response = await _s3Client.GetObjectAsync(new GetObjectRequest()
                    {
                        BucketName = record.S3.Bucket.Name,
                        Key = record.S3.Object.Key
                    }))
                    {
                        using (StreamReader reader = new StreamReader(response.ResponseStream))
                        {
                            string text = await reader.ReadToEndAsync();

                            //
                            // Detect entities
                            //
                            try
                            {
                                detectEntitiesResponse = await _comprehendClient.DetectEntitiesAsync(new DetectEntitiesRequest
                                {
                                    LanguageCode = LanguageCode.En,
                                    Text         = text
                                });
                            }
                            catch (AmazonComprehendException ex)
                            {
                                context.Logger.LogLine("Error in detecting entities.");
                                context.Logger.LogLine(ex.Message);
                                return;
                            }

                            //
                            // Detect sentiment
                            //
                            try
                            {
                                detectSentimentResponse = await _comprehendClient.DetectSentimentAsync(new DetectSentimentRequest
                                {
                                    LanguageCode = LanguageCode.En,
                                    Text         = text
                                });
                            }
                            catch (AmazonComprehendException ex)
                            {
                                context.Logger.LogLine("Error in detecting sentiment.");
                                context.Logger.LogLine(ex.Message);
                                return;
                            }
                        }
                    }
                }
                catch (AmazonS3Exception ex)
                {
                    context.Logger.LogLine(ex.Message);
                }

                //
                // save detections in S3 bucket
                //
                try
                {
                    var body = new TextDetail()
                    {
                        Id        = Guid.NewGuid().ToString(),
                        Entities  = detectEntitiesResponse.Entities,
                        Sentiment = detectSentimentResponse.Sentiment
                    };

                    await _s3Client.PutObjectAsync(new PutObjectRequest()
                    {
                        ContentBody = JsonConvert.SerializeObject(body),
                        BucketName  = TARGET_BUCKET,
                        Key         = Path.Combine("texts", Path.GetFileNameWithoutExtension(record.S3.Object.Key), "detections.json")
                    });
                }
                catch (AmazonS3Exception ex)
                {
                    context.Logger.LogLine(ex.Message);
                }
            }
        }