DEV Community

Bruce Axtens
Bruce Axtens

Posted on

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

Top comments (0)