I wanted to play with “Cognetive Services” and thought best to use Episerver for  this reason. I wanted to have below functionality:

  1. Mark “Content” properties I want as “Tageable” content.
  2. Using Azure Congetice Text Analytics key  phraser to detect tag a content
  3. Save key phrses into “Tag” property of the content

Quite simple. To achieve this need to:

  1. Signup for “Free”  Azure Cognitive as below: (got from here)
    1. Navigate to Cognitive Services in the Azure Portal and ensure Text Analytics is selected as the ‘API type’.
    2. Select a plan. You may select the free tier for 5,000 transactions/month. As is a free plan, you will not be charged for using the service. You will need to login to your Azure subscription.
    3. Complete the other fields and create your account.
    4. After you sign up for Text Analytics, find your API Key. Copy the primary key, as you will need it when using the API services.
  2. Write a code to gather all contents on publishing as below
    string apiUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases";
    string apiSubscriptionID = "value from Azure Cognetive API";
    string language = "en";
    IEnumerable<string> contents = new string[] { "first content to detect", "second  content to detect" };
    
     using (var webClient = new WebClient())
                {
                    webClient.Headers.Add("Ocp-Apim-Subscription-Key", apiSubscriptionID);
                    webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                    webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
    
                    var document = new Request { Documents = contents.Select((item, index) => new Content { Id = index, Language = language, Text = item }).ToArray() };
                    var requestContent = JsonConvert.SerializeObject(document);
    
                    var result = webClient.UploadString(apiUrl, requestContent);
    
                    return JsonConvert.DeserializeObject<Response>(result).Documents.SelectMany(a => a.keyPhrases);
                }

    As you can see you need “API Subscription ID” which  you can get it from Azure Portal same API Url.

    As you  can see the concept is quite simple, you pass your “Content(s)” and it returns back key phrases.

  3. Now what you need to  do is to change your Page, Block, .. (Content) to inherit “ITageablePage” and apply “[TextAnalysisRequire]” attribute to any content type property which you want to consider as a part of text analysis. Sample as below:
        [SiteContentType(
            GUID = "17583DCD-3C11-49DD-A66D-0DEF0DD601FC",
            GroupName = Global.GroupNames.Products)]
        public class ProductPage : PageData, ITageablePage
        {
            [Display(
        GroupName = SystemTabNames.Content,
        Order = 310)]
            [CultureSpecific]
            [TextAnalysisRequire]
            public virtual XhtmlString MainBody { get; set; }
    
            [Editable(false)]
            [UIHint(UIHint.Textarea)]
            public virtual string Tags { get; set; }
        }

You are now all done! When you publish a “ProductPage” instant it will automatically read “MainBody” property, strip out HTML and pass “Text” to Azure Text Analytics service, ask for “Key Phrases” and store them into “Tag” property! Cool and easy, yeah!

This can become a game changer for the concept of CMS, so now  you can leverage “Personalization” and “Tagged Content” to present your customer what they are looking for. I’m going to write a roadmap for this and make it work with  new personalization concept.  I will push this as NuGet package!

You  can  find the code here

Please bare in mind the API is still in “Preview” version!

Leave a Reply

Your email address will not be published. Required fields are marked *