import string
def analyze_text(paragraph):
Convert to lowercase
text = paragraph.lower()
Count sentences
sentence_count = paragraph.count('.') + paragraph.count('!') + paragraph.count('?')
Remove punctuation for word analysis
for ch in string.punctuation:
text = text.replace(ch, "")
words = text.split()
Word frequency
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
Longest word
longest_word = max(words, key=len) if words else ""
Total words
total_words = len(words)
Display results
print("\n--- Text Analysis ---")
print("Total Words:", total_words)
print("Number of Sentences:", sentence_count)
print("Longest Word:", longest_word)
print("\nWord Frequency:")
for word in freq:
print(word, ":", freq[word])
paragraph = input("Enter a paragraph:\n")
analyze_text(paragraph) Page | 18
Pyt
import string
def analyze_text(paragraph):
Convert to lowercase
text = paragraph.lower()
Count sentences
sentence_count = paragraph.count('.') + paragraph.count('!') + paragraph.count('?')
Remove punctuation for word analysis
for ch in string.punctuation:
text = text.replace(ch, "")
words = text.split()
Word frequency
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
Longest word
longest_word = max(words, key=len) if words else ""
Total words
total_words = len(words)
Display results
print("\n--- Text Analysis ---")
print("Total Words:", total_words)
print("Number of Sentences:", sentence_count)
print("Longest Word:", longest_word)
print("\nWord Frequency:")
for word in freq:
print(word, ":", freq[word])
paragraph = input("Enter a paragraph:\n")
analyze_text(paragraph) Page | 18
Pyt