Natural Language Processing using TextBlob
On this Page
Problem Scenario
Natural Language Processing (NLP) is one of the hottest topics in Machine Learning. For decades, humans have been communicating with machines by writing code with specific rules so that machines can respond to commands and perform tasks. After decades of keyboard, mouse and years of the touchscreen, we have made a big leap in NLP in recent years which enables us to communicate with machines using natural languages.
NLP is very useful and is now available in our daily life. Chatbots accurately provide solutions to people 24/7. Virtual Assistants (Alexa, Google Assistant, Siri, Cortana, etc.) understand our voices and help us in a lot of things. Machine translator can translate most of the major languages on the fly. Text summarization, content categorization, sentiment analysis, text-to-speech conversion, speech-to-text conversion, and others are bringing communication between humans and machines to the next level.
The live demo is available at our Machine Learning Showcase.
Description
There are a lot of open source projects for NLP such as Stanford's Core NLP Suite, Natural Language Toolkit, Apache OpenNLP, etc. Each of them has strengths and weaknesses. In this use case, we will use a python library called TextBlob which is very simple, easy to use and yet powerful. We will use TextBlob and build NLP API with 4 operations using Remote Python Script Snap and SnapLogic Ultra Task. Those 4 operations include noun phrase extraction, part-of-speech tagging, sentiment analysis, and tokenization.
Objectives
- Model Testing: Use Remote Python Script Snap from ML Core Snap Pack to deploy python script to use pre-built NLP model using TextBlob library. Then, test the operations using samples.
- Model Hosting: Use Remote Python Script Snap from ML Core Snap Pack to deploy python script to host the model and schedule an Ultra Task to provide API.
- API Testing: Use REST Post Snap to send sample requests to the Ultra Task to make sure the API is working as expected.
We are going to build 3 pipelines: Model Testing, Model Hosting, and API Testing; and an Ultra Task to accomplish the above objectives. Each of these pipelines is described in the Pipelines section below.
Pipelines
Model Testing
The JSON Generator is used to generate a text sample for each of the NLP operations: noun phrase extraction, part-of-speech tagging, sentiment analysis, and tokenization. Those samples are then fed into the Remote Python Script Snap which outputs the results.
The picture below shows the content of the JSON Generator Snap. Each document contains $operation and $text.
The output of Remote Python Script Snap is displayed below.
Python Script
Below is the script from the Remote Python Script Snap used in this pipeline. The script has the following 3 main functions:
- snaplogic_init
- snaplogic_process
- snaplogic_final
The first function (snaplogic_init) is executed before consuming input data. The second function (snaplogic_process) is called on each of the incoming documents. The last function (snaplogic_final) is processed after all incoming documents are consumed by snaplogic_process.
We use SLTool.ensure to automatically install required libraries. SLTool class contains useful methods: ensure, execute, encode, decode, etc. In this case, we need nltk and textblob.
In snaplogic_process, the operation and text are extracted from the incoming document. Based on the operation, the corresponding NLP operation is performed.
Model Hosting
This pipeline is scheduled as an Ultra Task to provide a REST API that is accessible by external applications. The core component of this pipeline is the Remote Python Script Snap that is the same as in the Model Testing pipeline. Instead of taking the data from the JSON Generator, the Remote Python Script Snap takes the data from API request. The Filter Snap is used to authenticate the request by checking the token that can be changed in pipeline parameters. The Extract Params Snap (Mapper) extracts the required fields from the request. The Prepare Response Snap (Mapper) maps from result to $content which will be the response body. This Snap also adds headers to allow Cross-Origin Resource Sharing (CORS).
Building API
To deploy this pipeline as a REST API, click the calendar icon in the toolbar. Either Triggered Task or Ultra Task can be used.
Triggered Task is good for batch processing since it starts a new pipeline instance for each request. Ultra Task is good to provide REST API to external applications that require low latency. In this case, the Ultra Task is preferable. Bearer token is not needed here since the Filter Snap will perform authentication inside the pipeline.
In order to get the URL, click Show tasks in this project in Manager in the Create Task window. Click the small triangle next to the task then Details. The task detail will show up with the URL.
API Testing
In this pipeline, sample requests are generated by JSON Generator. The requests are sent to the Ultra Task by REST Post Snap. The Mapper Snap is used to extract responses which are in $response.entity.
Below is the content of the JSON Generator Snap. It contains $token and $params which will be included in the request body sent by REST Post Snap.
The REST Post Snap gets the URL from the pipeline parameters. Your URL can be found in the Manager page. In some cases, it is required to check Trust all certificates in the REST Post Snap.
The output of the REST Post Snap is shown below. The last Mapper Snap is used to extract $response.entity from the request.
Downloads
Important steps to successfully reuse Pipelines
- Download and import the pipeline into the SnapLogic application.
- Configure Snap accounts as applicable.
- Provide pipeline parameters as applicable.
Have feedback? Email documentation@snaplogic.com | Ask a question in the SnapLogic Community
© 2017-2024 SnapLogic, Inc.