Python Program for -Word Analysis
import string
from collections import Counter
def word_analysis(text):
# Convert to lowercase to treat "Word" and "word" as the same
text = [Link]()
# Remove punctuation
text = [Link]([Link]('', '', [Link]))
# Split the text into words (tokenization)
words = [Link]()
# Count word frequencies
word_counts = Counter(words)
# Calculate unique words
unique_word_count = len(word_counts)
# Calculate total words
total_word_count = sum(word_counts.values())
return total_word_count, unique_word_count, word_counts
# Example Usage:
sample_text = "This is a sample text. This text is a sample for word
analysis."
total, unique, counts = word_analysis(sample_text)
print(f"Total words: {total}")
print(f"Unique words: {unique}")
print("Word frequencies:", counts.most_common())
Explanation
📦 Import Section
import string
👉 This imports Python’s built-in string module.
We use it here to access [Link], which contains
all common punctuation marks like:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
from collections import Counter
👉 This imports Counter, a super useful tool for counting things
easily.
Instead of manually counting words with loops, Counter does it
for us automatically.
🧠 Function Definition
def word_analysis(text):
👉 This defines a function named word_analysis.
It takes one input: text (a string you want to analyze).
🔡 Step 1: Convert to Lowercase
text = [Link]()
👉 This converts everything to lowercase.
Why?
Because "Word" and "word" should be treated as the same word.
Without this, they would be counted separately.
✂️Step 2: Remove Punctuation
text = [Link]([Link]('', '',
[Link]))
This line looks complicated, but here’s what it does:
[Link] → gives all punctuation symbols.
[Link]('', '', [Link]) → creates
a rule saying:
“Remove all these punctuation characters.”
[Link](...) → applies that rule to the text.
So:
"Hello, world!"
becomes:
"Hello world"
🔎 Step 3: Split into Words
words = [Link]()
👉 .split() breaks the text into a list of words based on spaces.
Example:
"this is fun"
becomes:
['this', 'is', 'fun']
Now we have a list of individual words 🎉
🔢 Step 4: Count Word Frequencies
word_counts = Counter(words)
👉 Counter(words) counts how many times each word appears.
If:
words = ['this', 'is', 'this']
Then:
Counter(words)
becomes:
{'this': 2, 'is': 1}
Very powerful and very simple 😎
📊 Step 5: Count Unique Words
unique_word_count = len(word_counts)
👉 word_counts is like a dictionary:
{'this': 2, 'is': 1}
The number of keys = number of unique words.
So we use len() to count them.
🔢 Step 6: Count Total Words
total_word_count = sum(word_counts.values())
👉 word_counts.values() gives:
[2, 1]
👉 sum() adds them together:
2 + 1 = 3
So this gives total word count.
🔙 Step 7: Return Results
return total_word_count, unique_word_count, word_counts
👉 The function sends back three things:
1. Total number of words
2. Number of unique words
3. The word frequency dictionary
🧪 Example Usage
sample_text = "This is a sample text. This text is a
sample for word analysis."
👉 This stores some text in a variable.
total, unique, counts = word_analysis(sample_text)
👉 We call the function and unpack the results into:
total
unique
counts
print(f"Total words: {total}")
👉 Prints total word count.
print(f"Unique words: {unique}")
👉 Prints number of unique words.
print("Word frequencies:", counts.most_common())
👉 most_common() sorts words from most frequent to least
frequent.
Example output:
[('this', 2), ('is', 2), ('a', 2), ...]
🎉 In Simple Terms
Your program:
1. Cleans the text
2. Breaks it into words
3. Counts how many times each word appears
4. Tells you:
o Total words
o Unique words
o Frequency of each word
If you'd like, I can also:
Draw a visual diagram of how data changes step by step
Rewrite it in an even simpler beginner version
Or explain how translate() works in more depth 😊
Create your first image
Got an idea? Try one of our new curated styles and filters or
imagine something from scratch.
Try now
Caricature Trend
Camcorder
Neon fantasy
Norman Rockwell
Iconic
Post-rain sunset
Flower petals
Gold
Crayon
Paparazzi
Clouds
Department photoshoot
Minimalist
Kalighat
Chikankari
Rajasthani textile
Iridescent metal portrait
Bollywood poster
Festival
Mithila
Jaipur textile
Sari landscape
Desi outfit
Sketch
Dramatic
Plushie
Retro anime
Baseball bobblehead
Doodle
3D glam doll
Sugar cookie
Fisheye
Inkwork
Pop art
Ornament
Art school