How Can We Integrate an NLP Model Into the Project?

We can find how to train models from many sources. But there are not many resources on how to use these models. At least I couldn’t find it when I needed it. Therefore, in this article, I will describe how I incorporated a model I trained for sentiment analysis into my project.

I used sentiment analysis to evaluate the comments received on an e-commerce site. So I’m going to go through this example.

I will not describe model training. You can find how I trained the model I mentioned in this article and the codes of the training from the link below.

Github:Sentiment-Analysis

In order to use the model after training, it is necessary to save it first.

Path determination and save the model.

Connecting The Model With The Database

After we have our model ready, we need to connect it with our database. Thus, we can extract the comments from our site from the database and determine whether they are positive or negative. Then we need to save these values back to the database.

I first created a python file to connect our model and database. Next, I imported the libraries I would use.

Import libraries

I used the ‘pypyodbc’ library to connect with the database. We use SQL Server as database. I made the link with the code snippet below.

Database connection

In the “Server=” section, we write the name that is written in our “Server name” section.

SQL Server login screen

In the “Database=” part, we enter the name of the database that we will use. Here I wrote “Cars” because I will use the “Cars” database.

Databases

Our database connection is ready. I have defined a cursor to use while operating in the database.

Define cursor

In this file, I need to establish a connection with the model. Otherwise, I cannot use the model here.

Model connection

Before I wrote the code to pull the comments later, I had to write a method in which the incoming comments were evaluated by our model.

Define prediction method

All that’s left is to pull the comments from the database and send them for evaluation. Here I had to use some SQL query commands. With our first SQL query ‘ SELECT CommentContent FROM Comments ‘ we moved our cursor to the CommentContent part of our Comments table (where our comments are). Next, our action finds the comment’s id and sends that comment to our “prediction” method.

If we get a value greater than 0 from the analysis of the comment, we know that the comment is positive. It is found in the database through that comment id and “Positive” is written in the Sentiment part.

If the value is less than 0, the comment is a negative comment. It is found in the database through that comment id and “Negative” is written in the Sentiment part.

Evaluation part

Conclusion

Thus, we have included our model in our project. We analyze the comments received on our site and record whether that comment is positive or negative in the database.

After defining our model, we can use it in different ways for other projects with the same logic.

--

--

--

A CE student.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Linear Regression and Gradient Descent from scratch in PyTorch

Unsupervised Learning: Clustering Algorithms

Denoising MNIST images using an Autoencoder and Tensorflow in python

FAST AI JOURNEY: COURSE V3. PART 2. LESSON 9.

Heart Disease Prediction Using Machine Learning (With GUI)

ML.NET Spam Message Detector on Sentiment Analysis

XLM-RoBERTa: The multilingual alternative for non-english NLP

Sentiment Analysis using NLP libraries.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Merve Çaylı

Merve Çaylı

A CE student.

More from Medium

NLP Part II- TFIDF

Semantic Role Labeler Argument Categories

Understanding greedy search and beam search

NLP Challenges with Tisane: Code Words & Secret Language