DEV Community

Bruce Axtens
Bruce Axtens

Posted on

1 1

Google's Natural Language API, Content Classification, in C#

I've written about this before, in the context of ASP.NET. Here's a link to the revised project on Github from which a DLL may be derived.

We started off with classifying Html and went on to classifying Text. Thus we have

  • Classify()
  • ClassifyHtml()
  • ClassifyText() which emit JSON.

Recently we added XML serialization and the functions

  • ClassifyHtmlAsXml()
  • ClassifyTextAsXml()

Both serialization methods emit an Error/Cargo tuple. If Error is null, then there's no error and Cargo contains the result of the classification. If Error is a string, then there is an error (in Error) and Cargo is null/indefinite.

Below is from a REPL session in one of our javascript-on-steroids tools, demonstrating the outputs of the DLL:

>attach("Classification.DLL","C")
Attached C
Microsoft.ClearScript.VoidResult

>C.Classification.Classifier.ClassifyHtml(CS.System.IO.File.ReadAllText("https_www.sil.org.html"))
{"Error":null,"Cargo":[{"Name":"/Reference/Language Resources","Confidence":0.6}]}

>C.Classification.Classifier.ClassifyHtmlAsXml(CS.System.IO.File.ReadAllText("https_www.sil.org.html"))
<?xml version="1.0" encoding="utf-16"?>
<Classification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Cargo>
    <Cargo>
      <Name>/Reference/Language Resources</Name>
      <Confidence>0.6</Confidence>
    </Cargo>
  </Cargo>
</Classification>
Enter fullscreen mode Exit fullscreen mode

Acknowledgements: Google Cloud Language v1, Newtonsoft.Json

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay