Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Log into client with credentials
            AmazonComprehendClient client = new AmazonComprehendClient(Amazon.RegionEndpoint.USEast1);

            // Load XML doc
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load("phrases_old.xml");
            XmlElement  rootElement   = xmlDoc.DocumentElement;
            XmlNodeList rootsChildren = rootElement.ChildNodes;

            // List of language codes
            List <string> languageCodes = new List <string>();

            // Iterate over each child and send request to detect language for inner text of each node
            foreach (XmlNode node in rootsChildren)
            {
                DetectDominantLanguageRequest request = new DetectDominantLanguageRequest
                {
                    Text = node.InnerText
                };

                DetectDominantLanguageResponse response = client.DetectDominantLanguage(request);

                string test = string.Empty;

                // Add language found for each child statement to list
                foreach (DominantLanguage language in response.Languages)
                {
                    test += language.LanguageCode + ", ";
                }
                languageCodes.Add(test);
            }

            // print results of language found
            int i = 0;

            foreach (XmlNode node in rootsChildren)
            {
                Console.Write("Phrase " + node.Attributes[0].Name + ": " + node.Attributes[0].Value + "      ");
                Console.WriteLine(node.Attributes[1].Value + " : " + languageCodes[i].Substring(0, languageCodes[i].Length - 2));
                i++;
            }

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

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

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("Languages", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <DominantLanguage, DominantLanguageUnmarshaller>(DominantLanguageUnmarshaller.Instance);
                    response.Languages = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
Exemplo n.º 3
0
        public async Task ProcessMessageAsync(SQSEvent.SQSMessage message)
        {
            //_logger.LogLine("Processing message \n" + message.Body);

            ConvertTextToSpeechContract contract =
                JsonConvert.DeserializeObject <ConvertTextToSpeechContract>(message.Body);

            DetectDominantLanguageResponse dominantLanguageResponse = AwsService.Comprehend
                                                                      .DetectDominantLanguage(contract.TextContent);

            string dominantLanguageCode = dominantLanguageResponse
                                          .Languages
                                          .OrderByDescending(l => l.Score)
                                          .First()
                                          .LanguageCode;

            Amazon.Polly.LanguageCode languageCode = null;

            switch (dominantLanguageCode)
            {
            case "en":
                languageCode = Amazon.Polly.LanguageCode.EnUS;
                break;

            //TODO: Handle other languages
            default:
                throw new Exception("Could not find the language specified");
            }

            SynthesizeSpeechResponse systhesisResponse = AwsService
                                                         .Polly.SynthesizeSpeech(contract.TextContent, languageCode);

            MemoryStream inputStream = new MemoryStream(ReadToEnd(systhesisResponse.AudioStream));

            string contentType = "audio/mpeg";
            string bucketName  = Environment.GetEnvironmentVariable("ALEXA_READER_BUCKET");

            AwsService.S3.PutObject(inputStream, contract.AudioFilePathToSave,
                                    bucketName, contentType);

            //_logger.LogLine("Processed message \n" + message.Body);
        }