Sentiment Analysis of Text or Documents using a REST API in C#

entiment Analysis of Text or Documents using a REST API in C#

You can easily perform sentiment analysis for your documents or text programmatically. It is used to determine whether the text or data is positive, negative, or neutral. It also helps you to identify and extract opinions to use for the benefit of your business operations. In this article, you will learn how to perform sentiment analysis of the Text or Documents using a REST API in C#.

The following topics are discussed/covered in this article:

Sentiment Analysis REST API and .NET SDK

For sentiment analysis of text or documents, I will be using the .NET SDK of GroupDocs.Classification Cloud API. It allows you to classify your raw text as well as documents ‎into predefined categories. The SDK supports multiple taxonomy types such as IAB-2, Documents, and Sentiment taxonomy. It enables you to classify documents of supported file formats such as PDF, Word, OpenDocument, RTF, and TXT. The classification information shows the best class with its probability score.

You can install GroupDocs.Classification Cloud into your Visual Studio project from the NuGet Package Manager. You may install it by using the following command in the Package Manager console:

Install-Package GroupDocs.Classification-Cloud

Please get your Client ID and Secret from the dashboard before you start following the steps and available code examples. Once you have your client ID and Secret, add in the code as shown below:

Sentiment Analysis of Documents using a REST API in C#

Please follow the steps mentioned below to perform the sentiment analysis of your documents on the cloud.

Upload the Document

Firstly, upload the DOCX file on the cloud using the code sample given below:

As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.

Classify Documents with Sentiment Analysis using C#

You can easily perform sentiment analysis of your documents and classify them on the cloud by following the steps given below:

  • Create an instance of the ClassificationApi
  • Create an instance of the BaseRequest
  • Set the DOCX file path in the FileInfo model
  • Set the FileInfo to the BaseRequest document
  • Create a ClassifyRequest with the BaseRequest
  • Set the sentiment analysis taxonomy
  • Get results by calling the ClassificationApi.Classify() method with the ClassifyRequest

The following code sample shows how to perform sentiment analysis of a document using a REST API in C#.

ClassName :Positive
ClassProbability :83.35
--------------------------------
ClassName :Neutral
ClassProbability :9.69
--------------------------------
ClassName :Negative
ClassProbability :6.96
--------------------------------

Classify Text with Sentiment Analysis using a REST API in C#

You can perform sentiment analysis of raw text and classify it programmatically on the cloud by following the steps given below.

  • Create an instance of the ClassificationApi
  • Create an instance of the BaseRequest
  • Provide a raw text to the BaseRequest description
  • Create a ClassifyRequest with the BaseRequest
  • Set the sentiment analysis taxonomy
  • Get results by calling the ClassificationApi.Classify() method with ClassifyRequest

The following code sample shows how to perform sentiment analysis of a text using a REST API in C#.

ClassName : Positive
ClassProbability : 69.41
--------------------------------
ClassName : Neutral
ClassProbability : 22.08
--------------------------------
ClassName : Negative
ClassProbability : 8.51
--------------------------------

Sentiment Analysis of Multiple Sentences in C#

You can classify multiple sentences given in a batch of text and perform sentiment analysis programmatically on the cloud by following the steps given below:

  • Create an instance of the ClassificationApi
  • Create an instance of the BatchRequest
  • Provide multiple sentences in a batch of text to the BatchRequest
  • Set the sentiment analysis Taxonomy
  • Create a ClassifyBatchRequest with the BatchRequest and Taxonomy
  • Get results by calling the ClassificationApi.ClassifyBatch() method with ClassifyBatchRequest

The following code sample shows how to classify a batch of text with the sentiment analysis using a REST API in C#.

Text : Now that is out of the way, this thing is a beast. It is fast and runs cool.
ClassName : Positive
ClassProbability : 85.27
--------------------------------
Text : Experience is simply the name we give our mistakes
ClassName : Negative
ClassProbability : 72.56
--------------------------------
Text : When I used compressed air a cloud of dust bellowed out from the card (small scuffs and scratches).
ClassName : Negative
ClassProbability : 70.84
--------------------------------
Text : This is Pathetic.
ClassName : Negative
ClassProbability : 88.48
--------------------------------
Text : Excellent work done!
ClassName : Positive
ClassProbability : 90.41
--------------------------------

Try Online

Please try the following free online classification tool, which is developed using the above API. https://products.groupdocs.app/classification/

Conclusion

In this article, you have learned how to classify documents with sentiment analysis using a REST API. You have also learned how to perform sentiment analysis on the batch of text in C#. Moreover, you have learned how to programmatically upload the DOCX file on the cloud. You can learn more about GroupDocs.Classification Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also