0% found this document useful (0 votes)
4 views3 pages

Program 9

The document describes a Python function that analyzes a given paragraph for various metrics such as word frequency, longest word, number of sentences, and character count. It processes the text by converting it to lowercase, removing punctuation, and counting words and sentences. The results are displayed in a structured report format, including the most frequent word and its occurrence count.

Uploaded by

sravanisathya.p
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Program 9

The document describes a Python function that analyzes a given paragraph for various metrics such as word frequency, longest word, number of sentences, and character count. It processes the text by converting it to lowercase, removing punctuation, and counting words and sentences. The results are displayed in a structured report format, including the most frequent word and its occurrence count.

Uploaded by

sravanisathya.p
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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)

You might also like