DEV Community

loading...

Doing Google's Natural Language ClassifyText() in ASP.NET

bugmagnet profile image Bruce Axtens ・2 min read

I had a profoundly difficult time installing Google.Cloud.Language.V1 into our ASP.NET site yesterday and today. I used the NuGet route. It didn't work. A DLL, I think it was Grpc.Code.Api refused to install properly.

So I wrote a wrapper DLL and installed it as a Reference. In that process all the relevant Google DLLs were copied in. I just needed to manually include grpc_csharp_ext.x64.dll and grpc_csharp_ext.x86.dll

A JSON credentials file is provided by Google when you request the right to do Natural Language calls. I copied that to the server. Also an environment variable (GOOGLE_APPLICATION_CREDENTIALS) needs to be created on the server, its value being the path to the JSON credentials file. Google.Cloud.Language.V1 does all the authorization automagically in the background.

This is the wrapper, called Classification (now available on Github).

using Google.Cloud.Language.V1;

using Newtonsoft.Json;

using System;

namespace Classification
{
    internal class JsonResponse
    {
        public string Error { get; set; }
        public object Cargo { get; set; }
    }

    public class Classifier
    {
        public static string Classify(string html)
        {
            var document = new Document
            {
                Type = Document.Types.Type.Html,
                Content = html
            };
            string error;
            object cargo;
            try
            {
                var client = LanguageServiceClient.Create();
                ClassifyTextResponse response = client.ClassifyText(document);
                cargo = response.Categories;
                error = null;
            }
            catch (Exception e)
            {
                error = e.Message;
                cargo = string.Empty;
            }
            return JsonConvert.SerializeObject(new JsonResponse()
            {
                Error = error,
                Cargo = cargo
            });
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

I hand over the HTML of the website to that, and I get back a JSON string. This is what I get back from the HTML of SIL

{"Error":null,"Cargo":[{"Name":"/Reference/Language Resources","Confidence":0.56}]}
Enter fullscreen mode Exit fullscreen mode

This works for most sites, except one (so far) that seems to dislike me reading the site source with RestSharp. I'm going to have a go with BrowseSharp.

PLEASE NOTE
I haven't included the source to the ASHX handler file that uses the wrapper. That's left as an exercise for the reader.

Discussion (2)

Collapse
bassonrichard profile image
Richard Basson

Looks very cool ,
is the source for the wrapper available somewhere perhaps like on GitHub ?

I am curious to see how the wrapper was done since it seems to be the big drive behind this.

Collapse
bugmagnet profile image
Bruce Axtens Author • Edited

On Github. Alternatively, copy it off the page.

Forem Open with the Forem app