Do you want to perform Natural Language Processing tasks like part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, etc. in your GUI app? This post will get you to understand how to use TextBlob Python Library using Python4Delphi (P4D) in the Delphi/C++ Builder application and perform some interesting NLP tasks.
TextBlob is a Python library for processing textual data. It provides a simple and consistent API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.
TextBlob stands on the shoulders of Giants, like NLTK and pattern, and plays nicely with both.
TextBlob powerful features at a glance:
- Noun phrase extraction
- Part-of-speech tagging (POS tagging)
- Sentiment analysis
- Classification (Naive Bayes, Decision Tree)
- Tokenization (splitting text into words and sentences)
- Word and phrase frequencies
- Parsing
- n-grams
- Word inflection (pluralization and singularization) and lemmatization
- Spelling correction
- Add new models or languages through extensions
- WordNet integration
Table of Contents
Hands-On
This post will guide you on how to perform Natural Language Processing tasks via Python’s TextBlob and then display it in the Delphi Windows GUI app.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
The following code shows a demo for Part-of-Speech Tagging, Noun Phrase Extraction, and Sentiment Analysis capabilities using TextBlob (visit source here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from textblob import TextBlob text = ''' The titular threat of The Blob has always struck me as the ultimate movie monster: an insatiably hungry, amoeba-like mass able to penetrate virtually any safeguard, capable of--as a doomed doctor chillingly describes it--"assimilating flesh on contact. Snide comparisons to gelatin be damned, it's a concept with the most devastating of potential consequences, not unlike the grey goo scenario proposed by technological theorists fearful of artificial intelligence run rampant. ''' blob = TextBlob(text) print(blob.tags) print(blob.noun_phrases) for sentence in blob.sentences: print(sentence.sentiment.polarity) |
Here is the results in Python GUI:
The sentiment property returns a named tuple of the form Sentiment(polarity, subjectivity). The polarity score is a float value within the range [-1.0, 1.0] where -1.0 is very negative and 1.0 is very positive sentiment. The subjectivity is a float value t within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.
Let’s try TextBlob to perform the same operations as above to the excerpt of our articles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from textblob import TextBlob text = ''' Learn To Build A Flexible Hello World Python GUI In A Delphi Windows App How about combining the strength of Delphi and Python for your applications to provide first-class solutions for your customer needs? Or you are an expert in Delphi who want to also work with Python, as it is a high and growing demand in the market out there? Thinking about how to do it? Don’t worry! Python4Delphi does for us. Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and C++Builder. They let you easily execute Python scripts, create new Python modules and new Python types. This post will guide you on how to use these components, create a VCL application, run a simple Python Hello World! script in it, and gets the output. You can easily build Python GUI apps using your favorite Python language features and libraries for Windows using Delphi and C++Builder and Python4Delphi. In order to run the Python script in Python for Delphi, open and run project Demo1. Then insert the script into lower Memo, click Execute button, and get the result in upper Memo. You can find the Demo1 source on GitHub. Python4Delphi Demo1 Sample App shows how to run a Python Script by typing the python code in a Memo, execute and populate the result in another Memo. You can find the Demo1 source on GitHub. Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi. First, open the Demo01.dproj with RAD Studio: ''' blob = TextBlob(text) print(blob.tags) print(blob.noun_phrases) for sentence in blob.sentences: print(sentence.sentiment.polarity |
It runs blazingly-fast, doing all the operations smoothly for even longer text. It also works perfectly even for neutral sentences.
Congratulations, now you have learned how to perform Natural Language Processing tasks via Python’s TextBlob and then display it in the Delphi Windows GUI app
Check out the TextBlob library for Python and use it in your projects: https://pypi.org/project/textblob/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi