Sitemap
Nazish Javeed

Nazish Javeed

Software Engineer|Artificial Intelligence Engineer|Let connect with me

πŸ“Š Text Analysis Using Python 🧠

4 min readMar 16, 2026

--

Text Analysis

Text analysis is one of the most important tasks in Natural Language Processing (NLP). It helps us understand, process, and extract useful insights from textual data such as articles, reviews, or social media posts.

In this tutorial, we will create a simple text analysis project using Python. This project will analyze a text and provide insights like:

  • πŸ“Œ Total number of words
  • πŸ“Œ Number of unique words
  • πŸ“Œ Most frequently used words

Even beginners can easily follow along. Let’s get started! πŸš€

🧰 Requirements

To run this project, you only need Python installed on your system. We will use two built-in Python libraries:

  • re – for text cleaning using regular expressions
  • collections.Counter – for counting word frequency

No external libraries are required. πŸ‘

πŸ“ Step 1: Import Required Libraries

First, we import the necessary modules.

  • Counter helps count occurrences of words.
  • re is used to clean the text by removing punctuation and symbols.

πŸ“„ Step 2: Create Sample Text

Next, we define a small paragraph that we want to analyze.

Press enter or click to view image in full size
sample text

This text will be used for our analysis.

🧹 Step 3: Clean the Text

Before analyzing text, we should remove punctuation and convert everything to lowercase.

This step ensures that words like AI, ai, and Ai are treated the same.

πŸ”€ Step 4: Convert Text into Words

Now we split the cleaned text into individual words.

This creates a list of all words in the text.

πŸ“Š Step 5: Perform Text Analysis

Now we calculate different statistics from the text.

Here’s what each part does:

  • word_count β†’ Total number of words
  • unique_words β†’ Number of distinct words
  • most_common(5) β†’ Top 5 frequently used words

πŸ’» Complete Python Code

Below is the complete working code for the project.

Get Nazish Javeed’s stories inΒ yourΒ inbox

Join Medium for free to get updates fromΒ thisΒ writer.

# πŸ“Š Text Analysis Using Python 🧠

from collections import Counter
import re

text = β€œβ€β€
Artificial Intelligence is transforming the world.
AI makes machines smart and helps humans solve complex problems.
β€œβ€β€

# πŸ”Ή Clean text
clean_text = re.sub(r’[^a-zA-Z\s]’, β€˜β€™, text.lower())

# πŸ”Ή Word list
words = clean_text.split()

# πŸ”Ή Word count
word_count = len(words)

# πŸ”Ή Unique words
unique_words = len(set(words))

# πŸ”Ή Most common words
common_words = Counter(words).most_common(5)

print(β€œπŸ“Œ Total Words:”, word_count)
print(β€œπŸ“Œ Unique Words:”, unique_words)
print(β€œπŸ“Œ Most Common Words:”, common_words)

πŸ“ˆ Example Output

When you run the code, you may see an output like this:

Press enter or click to view image in full size

🌟 Why Text Analysis is Important

Text analysis is widely used in many applications:

  • πŸ“Š Sentiment Analysis (analyzing reviews or opinions)
  • πŸ“° News Classification
  • πŸ’¬ Chatbots and Virtual Assistants
  • πŸ“§ Spam Email Detection
  • πŸ“š Document Summarization

It is a fundamental step in Natural Language Processing (NLP).

πŸš€ Conclusion

In this tutorial, we built a simple text analysis tool using Python. We learned how to:

βœ” Clean text using regular expressions
βœ” Split text into words
βœ” Count total and unique words
βœ” Find the most common words

This is just the beginning! You can extend this project by adding:

  • πŸ“Š Word frequency visualization
  • 🧠 Sentiment analysis
  • ☁ Processing large text datasets

Python makes text analysis simple, powerful, and fun.

πŸš€ Conclusion

In this tutorial, we built a simple text analysis tool using Python. We learned how to:

βœ” Clean text using regular expressions
βœ” Split text into words
βœ” Count total and unique words
βœ” Find the most common words

This is just the beginning! You can extend this project by adding:

  • πŸ“Š Word frequency visualization
  • 🧠 Sentiment analysis
  • ☁ Processing large text datasets

Python makes text analysis simple, powerful, and fun.

--

--

Nazish Javeed
Nazish Javeed

Software Engineer|Artificial Intelligence Engineer|Let connect with me