π Text Analysis Using Python π§
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 expressionscollections.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.
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:
π 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.










