Sentiment analysis R app using MicrosoftML in SQL Server big data cluster
Contents
About this sample
Before you begin
Run this sample
Sample details
Related links
About this sample
This is a sample R app, which does sentiment analysis on review text using the MicrosoftML package. This sample creates an app in SQL Server big data cluster that accepts a reviewText text input and returns the estimate sentiment for it. The scoring uses a pre-trained model, stored in sentiment.rds. The code for this sample is in sentiment.R. The model file sentiment.rds was generated using the model-training.R script. You don't need to run the model training again, unless you want to retrain with other data. Also, this sample shows how to pass commands to execute when setting up the container using the pre-package-install.sh file which runs apt install to install the MicrosoftML package.
The inputs and outputs for this sample are shown below.
Inputs
| Parameter | Description |
|---|---|
reviewText |
The text to score for sentiment |
Outputs
| Parameter | Description |
|---|---|
out |
A data frame detailing the sentiment score for the reviewText |
Before you begin
To run this sample, you need the following prerequisites.
Software prerequisites:
- SQL Server big data cluster CTP 2.3 or later.
azdata. Refer to installing azdata document on setting up theazdataand connecting to a SQL Server 2019 big data cluster.
Run this sample
-
Clone or download this sample on your computer.
-
Log in to the SQL Server big data cluster using the command below using the IP address of the
controller-svc-externalin your cluster. If you are not familiar withazdatayou can refer to the documentation and then return to this sample.azdata login -e https://<ip-address-of-controller-svc-external>:30080 -u <user-name> -
Deploy the application by running the following command, specifying the folder where your
spec.yaml,sentiment.rdsandsentiment.Rfiles are located:azdata app create --spec ./sentiment-analysis -
Check the deployment by running the following command:
azdata app list -n sentiment-r -v [version]Once the app is listed as
Readyyou can continue to the next step. -
Test the app by running the following command:
azdata app run -n sentiment-r -v [version] --input reviewText="Absolutely the best movie experience I have ever had!"You should get output like the example below. The result of the sentiment analysis scoring is returned as a data frame in
out. APredictedLabelequal to1indicates the sentiment is deemed positive, whereas aPredictedLabelof0indicates a negative sentiment. TheProbability.1indicates the level of certainty for thePredictedLabelto be the true sentiment.{ "changedFiles": [], "consoleOutput": "Beginning processing data.\nRows Read: 1, Read Time: 8.51154e-05, Transform Time: 1.90735e-06\nBeginning processing data.\nElapsed time: 00:00:00.0364881\nFinished writing 1 rows.\nWriting completed.\n", "errorMessage": "", "outputFiles": {}, "outputParameters": { "out": { "PredictedLabel": [ "1" ], "Probability.1": [ 0.6523407697677612 ], "Score.1": [ 0.6293442845344543 ] } }, "success": true }RESTful web service. Note that any app you create is also accessible using a RESTful web service that is Swagger compliant. See step 6 in the Addpy sample for detailed instructions on how to call the web service.
-
You can clean up the sample by running the following commands:
# delete app azdata app delete --name sentiment-r --version [version]
Sample details
Please refer to sentiment.R for the code that does loads the pre-trained model and scores the reviewText. If you would like to explore the code that trains the model and saves it, see model-training.R.
Spec file
Here is the spec file for this application. As you can see the sample uses the R runtime and calls the handler method in the sentiment.R file, accepting a text input named reviewText and returning a data frame named out.
name: sentiment-r
version: v1
runtime: R
src: ./sentiment.R
entrypoint: handler
replicas: 1
poolsize: 1
inputs:
reviewText: character
output:
out: data.frame
Related Links
For more information, see these articles: