How to Use Texthero to Prep a Text-based Dataset for Your NLP Project

Natural Language Processing (NLP) is one of the most important fields of study and research in today’s world. It has many applications in the business sector such as chatbots, sentiment analysis, and document classification.

Preprocessing and representing text is one of the trickiest and most annoying parts of working on an NLP project. Text-based datasets can be incredibly thorny and difficult to preprocess. But fortunately, the latest Python package called Texthero can help you solve these challenges.

What is Texthero?

Texthero is a simple Python toolkit that helps you work with a text-based dataset. It provides quick and easy functionalities that let you preprocess, represent, map into vectors and visualize text data in just a couple of lines of code.

Texthero is designed to be used on top of pandas, so it makes it easier to preprocess and analyze text-based Pandas Series or Dataframes.

If you are working on an NLP project, Texthero can help you get things done faster than before and gives you more time to focus on important tasks.

NOTE: The Texthero library is still in the beta version. You might face some bugs and pipelines might change. A faster and better version will be released and it will bring some major changes.

Texthero Overview

Texthero has four useful modules that handle different functionalities that you can apply in your text-based dataset.

  1. Preprocessing
    This module allows for the efficient pre-processing of text-based Pandas Series or DataFrames. It has different methods to clean your text dataset such as lowercase(), remove_html_tags() and remove_urls().
  2. NLP
    This module has a few NLP tasks such as named_entities, noun_chunks, and so on.
  3. Representation
    This module has different algorithms to map words into vectors such as TF-IDF, GloVe, Principal Component Analysis(PCA), and term_frequency.
  4. Visualization
    The last module has three different methods to visualize the insights and statistics of a text-based Pandas DataFrame. It can plot a scatter plot and word cloud.

Install Texthero

Texthero is free, open-source, and well documented. To install it open a terminal and execute the following command:

The package uses a lot of other libraries on the back-end such as Gensim, SpaCy, scikit-learn, and NLTK. You don’t need to install them all separately, pip will take care of that.

How to use Texthero

In this article I will use a news dataset to show you how you can use different methods provided by texthero’s modules in your own NLP project.

We will start by importing important Python packages that we are going to use.

Then we’ll load a dataset from the twitter .

Let’s look at the top 5 rows of the dataset:

As you can see, in our dataset we have three columns (id, content, and category). For this article we will focus on the content feature.

We have created a new dataframe focused on content only, and then we’ll show the top 5 rows.

Preprocessing with Texthero.

We can use the clean(). method to pre-process a text-based Pandas Series.

The clean() method runs seven functions when you pass a pandas series. These seven functions are:

  • lowercase(s): Lowercases all text.
  • remove_diacritics(): Removes all accents from strings.
  • remove_stopwords(): Removes all stop words.
  • remove_digits(): Removes all blocks of digits.
  • remove_punctuation(): Removes all string.punctuation (!”#$%&’()*+,-./:;<=>?@[]^_`{|}~).
  • fillna(s): Replaces unassigned values with empty spaces.
  • remove_whitespace(): Removes all white space between words

Now we can see the cleaned news content.

Custom Cleaning

If the default pipeline from the clean() method does not fit your needs, you can create a custom pipeline with the list of functions that you want to apply in your dataset.

As an example, I created a custom pipeline with only 5 functions to clean my dataset.

Now I can use the custome_pipeline to clean my dataset.

You can see the clean dataset we have created by using custom pipeline .

Useful preprocessing methods

Here are some other useful functions from preprocessing modules that you can try to clean you text-based dataset.

Remove digits

You can use the remove_digits() function to remove digits in your text-based datasets.

output: Hi call me my mobile number is + call me at : am
dtype: object

Remove stopwords

You can use the remove_stopwords() function to remove stopwords in your text-based datasets.

output: need know NLP develop chatbot desire
dtype: object

Remove URLs

You can use the remove_urls() function to remove links in your text-based datasets.

output: Go to to read more articles you like
dtype: object

Tokenize

Tokenize each row of the given Pandas Series by using the tokenize() method and return a Pandas Series where each row contains a list of tokens.

output: [You, can, think, of, Texthero, as, a, tool, to, help, you, understand, and, work, with, text, based, dataset]
dtype: object

You can remove html tags from the given Pandas Series by using the remove_html_tags() method.

output: hello world
dtype: object

Useful visualization methods

Texthero contains different method to visualize insights and statistics of a text-based Pandas DataFrame.

Top words

If you want to know the top words in your text-based dataset, you can use the top_words() method from the visualization module. This method is useful if you want see additional words that you can add to the stop words lists.

This method does not return a bar graph, so I will use matplotlib to visualize the top words in a bar graph.

In the graph above we can visualize the top 20 words from our news dataset.

Wordclouds

The wordcloud() method from the visualization module plots an image using WordCloud from the word_cloud package.

We passed the dataframe series and number of maximum words (for this example, it is 100 words) in the wordcloud() method.

Useful representation methods

Texthero contains different methods from the representation module that help you map words into vectors using different algorithms such as TF-IDF, word2vec or GloVe. In this section I will show you how you can use these methods.

TF-IDF

You can represent a text-based Pandas Series using TF-IDF. I created a new pandas series with two pieces of news content and represented them in TF_IDF features by using the tfidf() method.

output: 0 [0.13736056394868904, 0.13736056394868904, 0.1…
dtype: object

NOTE: TF-IDF stands for term frequency-inverse document frequency.

Term Frequency

You can represent a text-based Pandas Series using the term_frequency() method. Term frequency (TF) is used to show how frequently an expression (term or word) occurs in a document or text content.

output: 0 [1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, …
dtype: object

K-means

Texthero can perform K-means clustering algorithm by using the kmeans() method. If you have an unlabeled text-based dataset, you can use this method to group content according to their similarities.

In this example, I will create a new pandas dataframe called news with the following columns content,tfidf and kmeans_labels.

We will use only the first 30 pieces of cleaned content from our news_content dataframe and cluster them into groups by using the kmeans() method.

In the above source code, in the pipeline of the k-means method we passed the number of clusters which is 5. This means we will group these contents into 5 groups.

Now the selected news content has been labeled into five groups.

PCA

You can also use the pca() method to perform principal component analysis on the given Pandas Series. Principal component analysis ( PCA) is a technique for reducing the dimensionality of your datasets. This increases interpretability but at the same time minimizes information loss.

In this example we use the tfidf features from the news dataframe and represent them into two components by using the pca() method. Finally we will show a scatterplot by using the scatterplot() method.

Wrap up

In this article, you’ve learned the basics of how to use the Texthero toolkit Python package in your NLP project. You can learn more about the methods available in the documentation.

Refrences :

https://www.freecodecamp.org

https://www.analyticsvidhya.com/blog/2020/08/how-to-use-texthero-to-prepare-a-text-based-dataset-for-your-nlp-project/

Lead Data Scientist