Exemplo n.º 1
0
        // Calls the LUIS service
        private static async Task <LUISResult> CallLUISAsync(string content,
                                                             // Your app appID
                                                             string appID = "af554cfd-af68-4c5d-b1b0-154e5e1e668d",
                                                             // Your subscription key
                                                             string subscriptionKey = "5d0d0e0f9572469ca415f52d332fa1f7")
        {
            string     uri    = string.Format(LUISEndpoint, appID, subscriptionKey, content);
            string     json   = null;
            HttpClient client = new HttpClient();
            LUISResult result = new LUISResult();

            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage message = await client.GetAsync(uri);

            if (message.IsSuccessStatusCode)
            {
                json = await message.Content.ReadAsStringAsync();

                JsonTextReader reader = new JsonTextReader(new StringReader(json));
                while (reader.Read())
                {
                    // Gets the top scoring intent
                    if (reader.Path == "topScoringIntent.intent" && reader.TokenType.ToString() == "String")
                    {
                        result.Intent = reader.Value.ToString();
                    }
                    // Gets the top scoring intent score
                    else if (reader.Path == "topScoringIntent.score" && reader.TokenType.ToString() == "Float")
                    {
                        result.Score = float.Parse(reader.Value.ToString());
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        // Analysis of tweet for all other attributes
        private static async Task <TwitterResult> ProcessTwitter(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return(null);
            }
            var result   = new TwitterResult();
            var matchURL = urlRegex.Match(content);

            if (matchURL.Success)//determine if tweet contains a URL
            {
                result.ContainsURL = true;

                Uri uri = null;
                Uri.TryCreate(matchURL.Value, UriKind.Absolute, out uri);
                Uri sourceUri = null;
                if (uri != null)
                {
                    if (uri.Host.Equals("t.co"))//shortened URL version in twitter
                    {
                        sourceUri = new Uri(GetSourceUri(uri.AbsoluteUri));
                    }
                    else
                    {
                        sourceUri = uri;
                    }

                    if (microsoftUrlKeywords.Any(m => sourceUri.Host.ToLower().Contains(m)))
                    {
                        result.IsMicrosoftURL = true;
                    }//consider scenario when URL is azure-specific but doesn't contain azure keywords
                    else
                    {
                        string title = GetTitleFromUri(sourceUri.AbsoluteUri);//looks if webpage title contains "Microsoft" or "Azure" and not a competitor
                        if (microsoftUrlKeywords.Any(m => title.ToLower().Contains(m)) && !competitorRegex.Match(title).Success)
                        {
                            result.IsMicrosoftURL = true;
                        }
                    }
                }
            }

            // Determines presence of other attributes
            if (azureRegex.Match(content).Success)
            {
                result.ContainsAzure = true;
            }

            if (promotionRegex.Match(content).Success)
            {
                result.ContainsKeyword = true;
            }

            if (competitorRegex.Match(content).Success)
            {
                result.MentionsCompetitors = true;
            }

            if (benefitRegex.Match(content).Success)
            {
                result.ContainsBenefit = true;
            }

            if (content.Contains("?"))
            {
                result.ContainsQuestionMark = true;
            }

            if (content.Contains(":"))
            {
                result.ContainsColon = true;
            }

            if (content.Contains("!"))
            {
                result.ContainsExclamation = true;
            }

            LUISResult lresult = await CallLUISAsync(content);

            result.Intent      = lresult.Intent;
            result.IntentScore = lresult.Score;

            return(result);
        }