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:

  • .

  • !

  • ?


👉 Key Idea:
Break problem into small independent tasks.

Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • Start

  • Read paragraph

  • Convert to lowercase

  • Remove punctuation

  • Split into words

  • Count word frequency

  • Find longest word

  • Count sentences

  • Display results

  • Stop


Algorithm

  1. Start

  2. Read paragraph

  3. Convert to lowercase

  4. Count sentences

  5. Remove punctuation

  6. Split paragraph into words

  7. Count frequency using dictionary

  8. Find longest word

  9. Display:

    • Total words

    • Total sentences

    • Word frequencies

    • Longest word

  10. 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!


Output:
Total Words: 7
Total Sentences: 2
Longest Word: powerful
Word Frequency:
python : 2
is : 2
easy : 1
powerful : 1
and : 1
popular : 1

Important Concepts Used

  • String methods (lower()replace()split()count())

  • Dictionary

  • Looping

  • Conditional comparison

  • Text processing logic


Viva Voce Questions

  1. Why do we convert text to lowercase?

  2. What does split() do?

  3. How is sentence count calculated?

  4. What is dictionary used for?

  5. How do we find longest word?

  6. Can this handle paragraphs from file?

  7. What is time complexity?


Procedure to Execute the Program

  1. Open Python IDE

  2. Create new file

  3. Type program

  4. Save as .py

  5. Run program

  6. 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:

Left Side of the Record
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.
Write the following neatly on the left-hand side page:

-:END:-

Comments

Popular posts from this blog