Skip to content

Commit 059bbee

Browse files
Added sentiment analysis script - ML services
1 parent 21a5601 commit 059bbee

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
To install the pretrained model in SQL Server, open an elevated CMD promtp:
3+
1. Navigate to the SQL Server installation path:
4+
C:\<SQL SERVER Installation path>\Microsoft SQL Server\140\Setup Bootstrap\SQL2017\x64
5+
2. Run the following command:
6+
RSetup.exe /install /component MLM /<version>/language 1033 /destdir <SQL_DB_instance_folder>\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs
7+
Example:
8+
RSetup.exe /install /component MLM /version 9.2.0.24 /language 1033 /destdir "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs"
9+
The models will be downloaded and extracted.
10+
*/
11+
12+
13+
USE [tpcxbb_1gb]
14+
GO
15+
16+
--******************************************************************************************************************
17+
-- STEP 1 Stored procedure that uses a pretrained model to determine sentiment of a text, such as a product review
18+
--******************************************************************************************************************
19+
CREATE OR ALTER PROCEDURE [dbo].[get_sentiment]
20+
(@text NVARCHAR(MAX))
21+
AS
22+
BEGIN
23+
DECLARE @script nvarchar(max);
24+
25+
--The Python script we want to execute
26+
SET @script = N'
27+
import pandas as p
28+
from microsoftml import rx_featurize, get_sentiment
29+
30+
analyze_this = text
31+
32+
# Create the data
33+
text_to_analyze = p.DataFrame(data=dict(Text=[analyze_this]))
34+
35+
# Get the sentiment scores
36+
sentiment_scores = rx_featurize(data=text_to_analyze,ml_transforms=[get_sentiment(cols=dict(scores="Text"))])
37+
38+
# Lets translate the score to something more meaningful
39+
sentiment_scores["Sentiment"] = sentiment_scores.scores.apply(lambda score: "Positive" if score > 0.6 else "Negative")
40+
';
41+
42+
EXECUTE sp_execute_external_script
43+
@language = N'Python'
44+
, @script = @script
45+
, @output_data_1_name = N'sentiment_scores'
46+
, @params = N'@text nvarchar(max)'
47+
, @text = @text
48+
WITH RESULT SETS (("Text" NVARCHAR(MAX),"Score" FLOAT, "Sentiment" NVARCHAR(30)));
49+
50+
END
51+
52+
GO
53+
54+
--******************************************************************************************************************
55+
-- STEP 2 Execute the stored procedure to get sentiment of your own text
56+
--******************************************************************************************************************
57+
EXECUTE [dbo].[get_sentiment] N'ENTER YOUR OWN TEXT HERE';
58+
GO
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
To install the pretrained model in SQL Server, open an elevated CMD promtp:
3+
1. Navigate to the SQL Server installation path:
4+
C:\<SQL SERVER Installation path>\Microsoft SQL Server\140\Setup Bootstrap\SQL2017\x64
5+
2. Run the following command:
6+
RSetup.exe /install /component MLM /<version>/language 1033 /destdir <SQL_DB_instance_folder>\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs
7+
Example:
8+
RSetup.exe /install /component MLM /version 9.2.0.24 /language 1033 /destdir "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs"
9+
The models will be downloaded and extracted.
10+
*/
11+
12+
13+
USE [tpcxbb_1gb]
14+
GO
15+
16+
--******************************************************************************************************************
17+
-- STEP 1 Stored procedure that uses a pretrained model to determine sentiment of a text, such as a product review
18+
--******************************************************************************************************************
19+
CREATE OR ALTER PROCEDURE [dbo].[get_review_sentiment]
20+
AS
21+
BEGIN
22+
DECLARE @script nvarchar(max);
23+
24+
--The Python script we want to execute
25+
SET @script = N'
26+
from microsoftml import rx_featurize, get_sentiment
27+
28+
# Get the sentiment scores
29+
sentiment_scores = rx_featurize(data=reviews, ml_transforms=[get_sentiment(cols=dict(scores="review"))])
30+
31+
# Lets translate the score to something more meaningful
32+
sentiment_scores["Sentiment"] = sentiment_scores.scores.apply(lambda score: "Positive" if score > 0.6 else "Negative")
33+
';
34+
35+
EXECUTE sp_execute_external_script
36+
@language = N'Python'
37+
, @script = @script
38+
, @input_data_1 = N'SELECT CAST(pr_review_content AS NVARCHAR(4000)) AS review FROM product_reviews'
39+
, @input_data_1_name = N'reviews'
40+
, @output_data_1_name = N'sentiment_scores'
41+
WITH RESULT SETS (("Review" NVARCHAR(MAX),"Score" FLOAT, "Sentiment" NVARCHAR(30)));
42+
43+
END
44+
45+
GO
46+
47+
--******************************************************************************************************************
48+
-- STEP 2 Execute the stored procedure
49+
--******************************************************************************************************************
50+
EXECUTE [dbo].[get_review_sentiment];
51+
GO
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
USE [tpcxbb_1gb]
2+
GO
3+
4+
--**************************************************************
5+
-- STEP 1 Create a table for storing the machine learning model
6+
--**************************************************************
7+
DROP TABLE IF EXISTS [dbo].[models]
8+
GO
9+
CREATE TABLE [dbo].[models](
10+
[language] [varchar](30) NOT NULL,
11+
[model_name] [varchar](30) NOT NULL,
12+
[model] [varbinary](max) NOT NULL,
13+
[create_time] [datetime2](7) NULL DEFAULT (sysdatetime()),
14+
[created_by] [nvarchar](500) NULL DEFAULT (suser_sname()),
15+
PRIMARY KEY CLUSTERED
16+
(
17+
[language],
18+
[model_name]
19+
)
20+
)
21+
GO
22+
23+
--*************************************************************************************************************
24+
-- STEP 2 Look at the dataset we will use in this sample
25+
-- Tag is a label indicating the sentiment of a review. These are actual values we will use to train the model
26+
-- For training purposes, we will use 90% percent of the data.
27+
-- For testing / scoring purposes, we will use 10% percent of the data.
28+
--*************************************************************************************************************
29+
CREATE OR ALTER VIEW product_reviews_training_data
30+
AS
31+
SELECT TOP(CAST( ( SELECT COUNT(*) FROM product_reviews)*.9 AS INT))
32+
CAST(pr_review_content AS NVARCHAR(4000)) AS pr_review_content,
33+
CASE
34+
WHEN pr_review_rating <3 THEN 1
35+
WHEN pr_review_rating =3 THEN 2
36+
ELSE 3
37+
END AS tag
38+
FROM product_reviews;
39+
GO
40+
41+
CREATE OR ALTER VIEW product_reviews_test_data
42+
AS
43+
SELECT TOP(CAST( ( SELECT COUNT(*) FROM product_reviews)*.1 AS INT))
44+
CAST(pr_review_content AS NVARCHAR(4000)) AS pr_review_content,
45+
CASE
46+
WHEN pr_review_rating <3 THEN 1
47+
WHEN pr_review_rating =3 THEN 2
48+
ELSE 3
49+
END AS tag
50+
FROM product_reviews;
51+
GO
52+
53+
-- Look at the dataset we will use in this sample
54+
SELECT TOP(100) * FROM product_reviews_training_data;
55+
GO
56+
57+
--***************************************************************************************************
58+
-- STEP 3 Create a stored procedure for training a
59+
-- text classifier model for product review sentiment classification (Positive, Negative, Neutral)
60+
-- 1 = Negative, 2 = Neutral, 3 = Positive
61+
--***************************************************************************************************
62+
CREATE OR ALTER PROCEDURE [dbo].[create_text_classification_model]
63+
AS
64+
BEGIN
65+
DECLARE @model varbinary(max)
66+
, @train_script nvarchar(max);
67+
68+
--The Python script we want to execute
69+
SET @train_script = N'
70+
##Import necessary packages
71+
from microsoftml import rx_logistic_regression,featurize_text, n_gram
72+
import pickle
73+
74+
## Defining the tag column as a categorical type
75+
training_data["tag"] = training_data["tag"].astype("category")
76+
77+
## Create a machine learning model for multiclass text classification.
78+
## We are using a text featurizer function to split the text in features of 2-word chunks
79+
model = rx_logistic_regression(formula = "tag ~ features", data = training_data, method = "multiClass", ml_transforms=[
80+
featurize_text(language="English",
81+
cols=dict(features="pr_review_content"),
82+
word_feature_extractor=n_gram(2, weighting="TfIdf"))])
83+
84+
## Serialize the model so that we can store it in a table
85+
modelbin = pickle.dumps(model)
86+
';
87+
88+
EXECUTE sp_execute_external_script
89+
@language = N'Python'
90+
, @script = @train_script
91+
, @input_data_1 = N'SELECT * FROM product_reviews_training_data'
92+
, @input_data_1_name = N'training_data'
93+
, @params = N'@modelbin varbinary(max) OUTPUT'
94+
, @modelbin = @model OUTPUT;
95+
96+
--Save model to DB Table
97+
DELETE FROM dbo.models WHERE model_name = 'rx_logistic_regression' and language = 'Python';
98+
INSERT INTO dbo.models (language, model_name, model) VALUES('Python', 'rx_logistic_regression', @model);
99+
END;
100+
GO
101+
102+
--***************************************************************************************************
103+
-- STEP 4 Execute the stored procedure that creates and saves the machine learning model in a table
104+
--***************************************************************************************************
105+
106+
EXECUTE [dbo].[create_text_classification_model];
107+
--Take a look at the model object saved in the model table
108+
SELECT * FROM dbo.models;
109+
GO
110+
111+
--******************************************************************************************************************
112+
-- STEP 5 --Stored procedure that uses the model we just created to predict/classify the sentiment of product reviews
113+
--******************************************************************************************************************
114+
CREATE OR ALTER PROCEDURE [dbo].[predict_review_sentiment]
115+
AS
116+
BEGIN
117+
-- text classifier for online review sentiment classification (Positive, Negative, Neutral)
118+
DECLARE
119+
@model_bin varbinary(max)
120+
, @prediction_script nvarchar(max);
121+
122+
-- Select the model binary object from the model table
123+
SET @model_bin = (select model from dbo.models WHERE model_name = 'rx_logistic_regression' and language = 'Python');
124+
125+
126+
--The Python script we want to execute
127+
SET @prediction_script = N'
128+
from microsoftml import rx_predict
129+
from revoscalepy import rx_data_step
130+
import pickle
131+
132+
## The input data from the query in @input_data_1 is populated in test_data
133+
## We are selecting 10% of the entire dataset for testing the model
134+
135+
## Unserialize the model
136+
model = pickle.loads(model_bin)
137+
138+
## Use the rx_logistic_regression model
139+
predictions = rx_predict(model = model, data = test_data, extra_vars_to_write = ["tag", "pr_review_content"], overwrite = True)
140+
141+
## Converting to output data set
142+
result = rx_data_step(predictions)
143+
';
144+
145+
EXECUTE sp_execute_external_script
146+
@language = N'Python'
147+
, @script = @prediction_script
148+
, @input_data_1 = N'SELECT * FROM product_reviews_test_data'
149+
, @input_data_1_name = N'test_data'
150+
, @output_data_1_name = N'result'
151+
, @params = N'@model_bin varbinary(max)'
152+
, @model_bin = @model_bin
153+
WITH RESULT SETS (("Review" NVARCHAR(MAX),"Tag" FLOAT, "Predicted_Score_Negative" FLOAT, "Predicted_Score_Neutral" FLOAT, "Predicted_Score_Positive" FLOAT));
154+
END
155+
GO
156+
157+
158+
--***************************************************************************************************
159+
-- STEP 6 Execute the multi class prediction using the model we trained earlier
160+
--***************************************************************************************************
161+
EXECUTE [dbo].[predict_review_sentiment]
162+
GO
163+
164+
165+
166+
167+

0 commit comments

Comments
 (0)