Do you want to build a GUI App with Textual Data Processing capabilities? In this post will get to understand how to use TextBlob Python Library using Python4Delphi in Delphi/C++ application. TextBlob provides access to common text-processing operations through a familiar interface. You can treat
TextBlob objects as if they were Python strings that learned how to do Natural Language Processing.
It provides a consistent API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, and more.
Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and Lazarus (FPC). They let you easily execute Python scripts, create new Python modules and new Python types. You can use Python4Delphi a number of different ways such as:
- Create a Windows GUI around your existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed-sensitive Python apps with functions from Delphi for more speed.
- If not python and Python4Delphi is not installed on your machine, Check this how to run a simple python script in Delphi application using Python4Delphi sample app
- Open windows open command prompt, and type pip install -U TextBlob to install TextBlob. For more info for Installing Python Modules check here
- Followed by python -m textblob.download_corpora to download packages such as wordnet to nltk_data.
- First, run the Demo1 project for executing Python script in Python for Delphi. Then load the Texblob sample script in the Memo1 field and press the Execute Script button to see the result. On Clicking Execute Button the script strings are executed using the below code. Go to GitHub to download the Demo1 source.
procedure TForm1.Button1Click(Sender: TObject);
PythonEngine1.ExecStrings( Memo1.Lines );
TextBlob Python Library sample script details:
- Extract different noun phrases in TextBlob using a simple noun_phrase attribute.
- Perform Sentiment Analysis The
sentimentproperty returns a named tuple of the form
Sentiment(polarity, subjectivity). The polarity score is a float value within the range [-1.0, 1.0]. 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.
- Straight-forward tokenization. Tokenization is the process of dividing a large paragraph into many words or sentences.
- Lemmatization a process to convert the words to their original form as they were in the dictionary.
- Transform your text into single or plural.
- Wordnet integration You can access the synsets for a Word via the synsets property or the get_synsets method, optionally passing in a part of speech.
- TextBlob also offers you word/phrase counts, uppercase and lowercase conversion, spelling correction, translation, N-grams detection, and many more.
from textblob import TextBlob
from textblob import Word
from textblob.wordnet import VERB
from textblob.wordnet import Synset
wiki = TextBlob("Python is a high-level, general-purpose programming language.")
testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
zen = TextBlob("Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex.")
# sentence objects have the same properties and methods as Textblobs
for sentence in zen.sentences:
#Words Inflection and Lemmatization
sentence = TextBlob('Use 4 spaces per indentation level.')
#sentence.words is a word object.
#lemmatized by calling lemmatize method
w = Word("octopi")
w = Word("went")
word = Word("peace")
octopus = Synset('octopus.n.02')
shrimp = Synset('shrimp.n.03')
# WordList is a python list with additional methods.
animals = TextBlob("cat dog octopus")
#spelling corrections use correct
b = TextBlob("I havv goood speling!")
Note: Samples used for demonstration were picked from here with only the difference of printing the outputs. You can check the APIs and some more samples from the same place.
You have read the quick overview of TextBlob library, download this library from here and use user-friendly Date Time manipulations in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.