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.
Prerequisites.
- 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.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
TextBlob Python Library sample script details:
- Extract different noun phrases in TextBlob using a simple noun_phrase attribute.
- Perform Sentiment Analysis The
sentiment
property returns a named tuple of the formSentiment(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.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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.") print(wiki.tags) #NounPhrase extraction print(wiki.noun_phrases) #Sentiment analysis testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!") print(testimonial.sentiment) #Tokenization zen = TextBlob("Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex.") print(zen.words) print(zen.sentences) # sentence objects have the same properties and methods as Textblobs for sentence in zen.sentences: print(sentence.sentiment) #Words Inflection and Lemmatization sentence = TextBlob('Use 4 spaces per indentation level.') #sentence.words is a word object. print(sentence.words) print(sentence.words[2].singularize()) print(sentence.words[-1].pluralize()) #lemmatized by calling lemmatize method w = Word("octopi") print(w.lemmatize()) w = Word("went") print(w.lemmatize("v")) #Wordnet integration word = Word("peace") print(word.synsets) print(Word("hack").get_synsets(pos=VERB)) octopus = Synset('octopus.n.02') shrimp = Synset('shrimp.n.03') print(octopus.path_similarity(shrimp)) # WordList is a python list with additional methods. animals = TextBlob("cat dog octopus") print(animals.words) print(animals.words.pluralize()) #spelling corrections use correct b = TextBlob("I havv goood speling!") print(b.correct()) #parsing print(b.parse()) |
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.