9.
Text Analysis Tool: Build a tool that analyses a paragraph: frequency of each word, longest
word, number of sentences, etc
def analyze_text(text):
# Convert text to lowercase
text = [Link]()
# Sentence count
sentence_count = 0
for ch in text:
if ch == '.' or ch == '!' or ch == '?':
sentence_count += 1
# Remove punctuation manually
for ch in ".,!?":
text = [Link](ch, "")
# Split into words
words = [Link]()
# Word count
word_count = len(words)
# Character count (excluding spaces)
char_count = 0
for ch in text:
if ch != " ":
char_count += 1
# Find longest word
longest_word = words[0]
for word in words:
if len(word) > len(longest_word):
longest_word = word
# Word frequency using dictionary
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
# Find most frequent word
most_frequent_word = ""
max_count = 0
for word in word_freq:
if word_freq[word] > max_count:
max_count = word_freq[word]
most_frequent_word = word
# Display Results
print("\n------ TEXT ANALYSIS REPORT ------")
print("Total Words:", word_count)
print("Total Characters (without spaces):", char_count)
print("Number of Sentences:", sentence_count)
print("Longest Word:", longest_word)
print("Most Frequent Word:", most_frequent_word, "->", max_count, "times")
print("\nWord Frequency:")
for word in word_freq:
print(word, ":", word_freq[word])
# -------- MAIN PROGRAM --------
text = input("Enter a paragraph:\n")
analyze_text(text)