Program9
9. Text Analysis Tool: Build a tool that analyses a paragraph: frequency of each word, longest word, number of sentences, etc.
Objective of the Program
To analyze textual data
To process strings effectively
To use dictionary for frequency counting
To apply logical conditions
To build a mini text analytics tool
Logical Construction (Think Before You Code)
Suppose the input paragraph is:
Python is easy. Python is powerful and popular.
We must compute:
1. Word frequency
2. Longest word
3. Number of words
4. Number of sentences
Step 1: Preprocessing
Convert text to lowercase (avoid duplication)
Remove punctuation
Split paragraph into words
Step 2: Word Frequency
Use dictionary:
key → word
value → count
Step 3: Longest Word
Traverse word list
Compare lengths
Store longest word
Step 4: Sentence Count
Count occurrences of:
.!?
Break problem into small independent tasks.
Flowchart (Block Diagram of Logic)
Start
Read paragraph
Convert to lowercase
Remove punctuation
Split into words
Count word frequency
Find longest word
Count sentences
Display results
Stop
Algorithm
Start
Read paragraph
Convert to lowercase
Count sentences
Remove punctuation
Split paragraph into words
Count frequency using dictionary
Find longest word
Display:
Total words
Total sentences
Word frequencies
Longest word
Stop
Python Program
⚠ Students must complete the missing parts.
# Text Analysis Tool
paragraph = input("Enter a paragraph: ")
# Convert to lowercase
text = paragraph.____________()
# Count sentences
sentence_count = text.count('.') + text.count('!') + text.count('__________')
# Remove punctuation
for ch in ".,!?;:":
text = text.replace(ch, "")
words = text.split()
# Total words
total_words = len(____________)
# Word frequency
freq = {}
for word in words:
if word in freq:
freq[word] += ____________
else:
freq[word] = ____________
# Longest word
longest = ""
for word in words:
if len(word) > len(____________):
longest = word
print("Total Words:", total_words)
print("Total Sentences:", sentence_count)
print("Longest Word:", longest)
print("Word Frequency:")
for key in freq:
print(key, ":", freq[key])
Probable Output
Input: Python is easy. Python is powerful and popular!
Important Concepts Used
String methods (
lower(),replace(),split(),count())Dictionary
Looping
Conditional comparison
Text processing logic
Viva Voce Questions
Why do we convert text to lowercase?
What does
split()do?How is sentence count calculated?
What is dictionary used for?
How do we find longest word?
Can this handle paragraphs from file?
What is time complexity?
Procedure to Execute the Program
Open Python IDE
Create new file
Type program
Save as
.pyRun program
Enter paragraph
Google Colab – Online Execution
👉 Paste Colab link here.
[https://colab.research.google.com/drive/1WwTrYN500kYiGmODdSUNoNWxZFjHNGZR?usp=sharing]
Program Explanation
Assignment for Students
Sort frequency in descending order
Display most frequent word
Ignore common stop words
Read paragraph from file instead of input
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.
Write the following neatly on the left-hand side page:

Comments
Post a Comment