Skip to content

Examples

lare edited this page Jan 23, 2025 · 16 revisions

Examples

1. Cleaning a body of text mined from an API

In this short example, we will be looking at cleaning a body of text mined from an API, to prepare your text for further processing and feature engineering like Sentiment Analysis etc. With cleantweet, it is easy to clean a body of text with just a few lines of code:

Install the cleantweet library

!pip install cleantweet

Import the cleantweet library

import cleantweet as clt

Create an instance of the CleanTweet class

data = clt.CleanTweet('mini corpus for testing')

The text document before using cleantweet

mini corpus for testing

Call the clean() method on the object

data = data.clean()

View the cleaned body of text

print(data)

The text document after using cleantweet

mini corpus for testing

2. Creating a Wordcloud

You can use the DiagramTweet class to create different diagrams for NLP tasks. To create a Wordcloud using cleantweet, takes two lines of code:

figures = clt.DiagramTweet('mini corpus for testing.txt') figures.word_cloud()

The first line creates an instance of the DiagramTweet class and the second line calls the word_cloud() method. To know more about the different [parameters of the word_cloud() method, you can see it's full definition in the API docs

Wordcloud Example

3. Creating a Frequency Distribution of the Most Frequent Words

The DiagramTweet class has several methods for creating diagrams that can be used to visualize the textual statistics in your corpus. One of them is the Frequency Distribution chart; this chart shows you how frequent each word appears in your text. Creating a frequency distribution with cleantweet is just two lines of code:

figures = clt.DiagramTweet('mini corpus for testing.txt') figures.frequency_distribution(20)

The first line creates an instance of the DiagramTweet class and the second line calls the frequency_distribution() method. To know more about the different [parameters of the frequency_distribution() method, you can see it's full definition in the API docs

Frequency Distribution Example