コード例 #1
0
        private void CreateSetimentJSON(string key, DetectSentimentResponse setimentResponse)
        {
            try
            {
                using (MemoryStream memStream = new MemoryStream())
                {
                    dynamic setiment = new JObject();
                    setiment.Id            = key.Replace(".json", "");
                    setiment.Setiment      = setimentResponse.Sentiment.ToString();
                    setiment.MixedScore    = setimentResponse.SentimentScore.Mixed;
                    setiment.NegativeScore = setimentResponse.SentimentScore.Negative;
                    setiment.NeutralScore  = setimentResponse.SentimentScore.Neutral;
                    setiment.PostiveScore  = setimentResponse.SentimentScore.Positive;

                    string json      = ((JObject)setiment).ToString(Formatting.None);
                    byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
                    memStream.Write(jsonBytes, 0, jsonBytes.Length);
                    SendFileToS3(memStream, @"athena-data/setiment", key);
                }
            }
            catch (Exception ex)
            {
                var sdf = "test";
            }
        }
コード例 #2
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            DetectSentimentResponse response = new DetectSentimentResponse();

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

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("Sentiment", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Sentiment = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("SentimentScore", targetDepth))
                {
                    var unmarshaller = SentimentScoreUnmarshaller.Instance;
                    response.SentimentScore = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
        private bool isHazard(List <DocumentClass> classes, DetectSentimentResponse sentiment)
        {
            bool hazardProbability = classes.Any(c =>
                                                 _hazardClasses.Contains(c.Name) && c.Score >= _classifierConfidenceLevel);

            return(hazardProbability);
        }
コード例 #4
0
        private async Task <Sentiment> GetSentimentFromAWSAsync(String customerContact)
        {
            Sentiment caseSentiment = new Sentiment();

            try
            {
                AmazonComprehendClient comprehendClient = new AmazonComprehendClient(RegionEndpoint.EUWest2);

                Console.WriteLine("Calling DetectSentiment");
                DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest()
                {
                    Text         = customerContact,
                    LanguageCode = "en"
                };
                DetectSentimentResponse detectSentimentResponse = await comprehendClient.DetectSentimentAsync(detectSentimentRequest);

                caseSentiment.success           = true;
                caseSentiment.sentimentRating   = detectSentimentResponse.Sentiment.ToString().ToLower();
                caseSentiment.sentimentMixed    = ((int)(detectSentimentResponse.SentimentScore.Mixed * 100)).ToString();
                caseSentiment.sentimentNegative = ((int)(detectSentimentResponse.SentimentScore.Negative * 100)).ToString();
                caseSentiment.sentimentNeutral  = ((int)(detectSentimentResponse.SentimentScore.Neutral * 100)).ToString();
                caseSentiment.sentimentPositive = ((int)(detectSentimentResponse.SentimentScore.Positive * 100)).ToString();
                return(caseSentiment);
            }
            catch (Exception error)
            {
                caseSentiment.success = false;
                await SendFailureAsync("Getting Sentiment", error.Message);

                Console.WriteLine("ERROR : GetSentimentFromAWSAsync : " + error.StackTrace);
                return(caseSentiment);
            }
        }
コード例 #5
0
        public async Task <DetectSentimentResponse> DetermineSentiment(string text)
        {
            // Call DetectKeyPhrases API
            DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest()
            {
                Text         = text,
                LanguageCode = "en"
            };
            DetectSentimentResponse detectSentimentResponse = await config.AzComprehendClient.DetectSentimentAsync(detectSentimentRequest);

            return(detectSentimentResponse);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: softfactories/ai_labs
        static void Main(string[] args)
        {
            String text = ExtFunc.Read("Input a text to Sentiment-Analysation i.E \"That was a nice trip. The Hotel was amazing!\"");

            AmazonComprehendClient comprehendClient = new AmazonComprehendClient(
                Amazon.RegionEndpoint.USEast1);

            // Call DetectKeyPhrases API
            Console.WriteLine("Calling DetectSentiment");
            DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest()
            {
                Text         = text,
                LanguageCode = "en"
            };
            DetectSentimentResponse detectSentimentResponse = comprehendClient.DetectSentiment(detectSentimentRequest);

            Console.WriteLine(detectSentimentResponse.Sentiment);
            Console.WriteLine("Done");
        }
コード例 #7
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);
        }
コード例 #8
0
    protected override async Task <bool> HandleMessage(DiscordMessage discordMessage)
    {
        logger.LogInformation($"Getting 10 messages before {discordMessage.Id}");
        IReadOnlyList <DiscordMessage> messages = await discordMessage.Channel.GetMessagesAsync(10, before : discordMessage.Id);

        logger.LogInformation($"Got {messages.Count} messages before {discordMessage.Id}");

        string text = messages
                      .Select(m => m.Content)
                      .Aggregate("", (acc, value) => acc + "\n" + value);

        DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest();

        detectSentimentRequest.LanguageCode = "en";
        detectSentimentRequest.Text         = text;

        logger.LogInformation($"Calling AWS::Comprehend with {messages.Count} messages and total text length {text.Length}");
        DetectSentimentResponse detectSentimentResponse = await AmazonComprehendClient.DetectSentimentAsync(detectSentimentRequest);

        logger.LogInformation($"Called AWS::Comprehend with {messages.Count} messages");

        if (detectSentimentResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
        {
            return(false);
        }

        await metrics.AddCounter("Discord.SentimentScore.Positive", detectSentimentResponse.SentimentScore.Positive);

        await metrics.AddCounter("Discord.SentimentScore.Negative", detectSentimentResponse.SentimentScore.Negative);

        await metrics.AddCounter("Discord.SentimentScore.Mixed", detectSentimentResponse.SentimentScore.Mixed);

        await metrics.AddCounter("Discord.SentimentScore.Neutral", detectSentimentResponse.SentimentScore.Neutral);

        await metrics.AddCounter($"Discord.Sentiment.{detectSentimentResponse.Sentiment.Value}", 1);

        return(true);
    }
コード例 #9
0
        public async Task ArchiveMessageAsync(MessageRecord record)
        {
            var languageResponse = await this.comprehend.DetectDominantLanguageAsync(new DetectDominantLanguageRequest()
            {
                Text = record.Message
            });

            var    languages    = languageResponse.Languages;
            string languageCode = "en";

            if ((languages?.Count ?? 0) > 0)
            {
                languageCode = languages.OrderByDescending(lang => lang.Score).First().LanguageCode;
            }

            DetectSentimentResponse sentimentResponse;

            if (languageCode == "id" && record.SourceUsername == "chelsiemonica")
            {
                sentimentResponse = new DetectSentimentResponse()
                {
                    Sentiment      = SentimentType.NEGATIVE,
                    SentimentScore = new SentimentScore()
                    {
                        Positive = 0.0f,
                        Mixed    = 0.0f,
                        Neutral  = 0.0f,
                        Negative = 1.0f
                    }
                };
            }
            else
            {
                sentimentResponse = await this.comprehend.DetectSentimentAsync(new DetectSentimentRequest()
                {
                    Text         = record.Message,
                    LanguageCode = languageCode
                });
            }
            var row = await this.chatMessageStorage.GetChatMessageRowByIdAsync(record.SourceMessageId);

            row.Sentiment = new MessageSentiment()
            {
                Sentiment  = sentimentResponse.Sentiment.Value,
                Positivity = sentimentResponse.SentimentScore.Positive,
                Negativity = sentimentResponse.SentimentScore.Negative,
                Neutrality = sentimentResponse.SentimentScore.Neutral,
                Mixed      = sentimentResponse.SentimentScore.Mixed
            };
            await this.chatMessageStorage.UpdateChatMessageAsync(row);

            UserIdRow userIdRow = null;

            try
            {
                userIdRow = await this.userIdsStorage.GetUserIdsFromTwitchAsync(row.Sender);
            }
            catch
            {
            }

            List <SentimentSnapshotRow> snapshotRows = new List <SentimentSnapshotRow>();

            if (userIdRow != null)
            {
                try
                {
                    snapshotRows = await this.sentimentSnapshotsStorage.GetUserSnapshotsAsync(userIdRow.CognitoUsername);
                }
                catch
                {
                }
            }

            var snapshotRow = snapshotRows?.FirstOrDefault();

            await this.broadcaster.BroadcastAsync(new Alert()
            {
                Type      = AlertType.Sentiment,
                Sentiment = new SentimentAlert()
                {
                    TwitchUsername        = row.Sender,
                    Score                 = row.Sentiment,
                    Message               = row.MessageContents,
                    TimestampEpochSeconds = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
                    Summary               = new UserSnapshotSummary()
                    {
                        MessageCount = snapshotRow?.MessageCount ?? 0,
                        Positivity   = snapshotRow?.Positivity,
                        Negativity   = snapshotRow?.Negativity,
                        Mixed        = snapshotRow?.Mixed,
                        Neutrality   = snapshotRow?.Neutrality
                    }
                }
            });

            var sentimentJson      = JObject.Parse(JsonConvert.SerializeObject(sentimentResponse.Sentiment));
            var sentimentScoreJson = JObject.Parse(JsonConvert.SerializeObject(sentimentResponse.SentimentScore));
            var messageJson        = JObject.Parse(JsonConvert.SerializeObject(record));

            foreach (var kvp in sentimentJson)
            {
                string newKey = "sentiment" + kvp.Key;
                messageJson[newKey] = kvp.Value;
            }
            foreach (var kvp in sentimentScoreJson)
            {
                string newKey = "sentiment" + kvp.Key;
                messageJson[newKey] = kvp.Value;
            }

            string json = JsonConvert.SerializeObject(messageJson, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Include,
                ContractResolver  = new CamelCasePropertyNamesContractResolver()
            }) + "\n";

            using var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            var sendRequest = new PutRecordRequest()
            {
                DeliveryStreamName = this.deliveryStreamName,
                Record             = new Record()
                {
                    Data = ms
                }
            };

            await this.firehose.PutRecordAsync(sendRequest);

            this.logger.LogInformation($"Archived message {record.SourceMessageId}:\nMessage: \"{record.Message}\" from {record.SourceUsername} with sentiment:" +
                                       $"\n{JsonConvert.SerializeObject(sentimentScoreJson)}");
        }
コード例 #10
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);
                }
            }
        }
コード例 #11
0
 private static void LogSentimate(DetectSentimentResponse speakerSentimate, int speaker, string text)
 {
     Console.WriteLine($"Speaker: spk_{speaker}");
     Console.WriteLine($"text: {text}");
     Console.WriteLine($"sentiment: { speakerSentimate.Sentiment.Value }");
 }
コード例 #12
0
 private int ParseSentimentToInt(DetectSentimentResponse awsSentiment)
 => _sentimentToIntMapper.GetValueOrDefault(awsSentiment.Sentiment, 0);