Пример #1
0
        public static string getKeyPhrases(string mailbody)
        {
            var client = AuthenticateClient();
            var result = client.KeyPhrases(mailbody);

            List <string> lkeyPhrases   = new List <string>();
            string        strKeyPhrases = string.Empty;

            foreach (string kp in result.KeyPhrases)
            {
                lkeyPhrases.Add(kp);
            }

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

            DetectSyntaxRequest dsr = new DetectSyntaxRequest()
            {
                Text         = mailbody,
                LanguageCode = "en"
            };
            DetectSyntaxResponse dsrsp           = comprehendClient.DetectSyntax(dsr);
            List <string>        adjectives      = new List <string>();
            List <string>        finalKeyPhrases = new List <string>();

            foreach (SyntaxToken kp in dsrsp.SyntaxTokens)
            {
                if (kp.PartOfSpeech.Tag.Equals("ADJ"))
                {
                    adjectives.Add(kp.Text);
                }
            }
            foreach (var adj in adjectives)
            {
                foreach (var phs in lkeyPhrases)
                {
                    if (phs.Contains(adj))
                    {
                        finalKeyPhrases.Add(phs);
                    }
                }
            }
            string str = "";

            foreach (var fnlKeyPhr in finalKeyPhrases)
            {
                str = str + ' ' + fnlKeyPhr + ' ' + '/';
            }



            var values = new object[2];

            values[0] = "Key Phrases";
            values[1] = str.Substring(0, str.Length - 1);
            return(str.Substring(0, str.Length - 1));
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            DetectSyntaxResponse response = new DetectSyntaxResponse();

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

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

            return(response);
        }
        /// <summary>
        /// This method calls DetectSynaxAsync to identify the syntax elements
        /// in the sample text.
        /// </summary>
        public static async Task Main()
        {
            string text = "It is raining today in Seattle";

            var comprehendClient = new AmazonComprehendClient();

            // Call DetectSyntax API
            Console.WriteLine("Calling DetectSyntaxAsync\n");
            var detectSyntaxRequest = new DetectSyntaxRequest()
            {
                Text         = text,
                LanguageCode = "en",
            };
            DetectSyntaxResponse detectSyntaxResponse = await comprehendClient.DetectSyntaxAsync(detectSyntaxRequest);

            foreach (SyntaxToken s in detectSyntaxResponse.SyntaxTokens)
            {
                Console.WriteLine($"Text: {s.Text}, PartOfSpeech: {s.PartOfSpeech.Tag}, BeginOffset: {s.BeginOffset}, EndOffset: {s.EndOffset}");
            }

            Console.WriteLine("Done");
        }