Python Text Statistics Calculator (Words, Vowels, Case Count)

Aim: Write a function that reads a file file1 and displays the number of words, number of vowels, blank spaces, lower case letters and uppercase letters

Program:

def count_of_char():
 print("Enter the Name of File:")
 fileName = str(input())
 fileHandle = open("fileName","r")
 tot = 0
 number_of_words=0
 l_c_l=0
 u_c_l=0
 ch=0
# to dispaly file
 data=fileHandle.read()
 print(data)

#to get count of lower and upper case
 for ch in data:
  if (ch.islower()):
   l_c_l+=1
  elif(ch.isupper()):
   u_c_l+=1
  ch=ch.lower()
 print("Lower case letters are :",l_c_l)
 print("Upper case letters are :",u_c_l)

#to get count of blank spaces
 for char in data:
  if char==' ':
     tot = tot+1
  if tot>1:
     print("\nThere are "+ str(tot) + "Blank spaces available in the File")

#to get count of vowels
 vowels = ['a','e','i','o','u','A','E','I','O','U']

 for char in data:
   if char in vowels:
     tot = tot+1

 print("\nTotal Vowels are:")
 print(tot)
#to get count of no of words
 for line in data:
   number _of _words+= len(line. split())
 print("number_ of_ words:")
 print(number_ of_ words)

count_ of_ char()


Output:

Python Text Statistics Calculator (Words, Vowels, Case Count)

Sample Viva Questions and Answers

  1. What is the purpose of the text statistics calculator?
    The program calculates various statistics of a given text, including the number of words, vowels, and the count of uppercase and lowercase letters.

  2. What inputs does the text statistics calculator require?
    The program requires a string input, which can be provided directly or read from a text file.

  3. How does the program count the number of words in the text?
    The program typically splits the text into words using spaces or punctuation as delimiters.

  4. What method is used to count vowels in the text?
    The program iterates through each character in the text and checks if it is a vowel (a, e, i, o, u) in both uppercase and lowercase.

  5. How does the program differentiate between uppercase and lowercase letters?
    The program checks the ASCII values or uses string methods to determine if a character is uppercase or lowercase.

  6. What will the output of the program include?
    The output will display the total number of words, the count of vowels, and the counts of uppercase and lowercase letters.

  7. What is the time complexity of counting words and vowels in the text?
    The time complexity is generally O(n), where n is the number of characters in the text, as each character needs to be processed.

  8. How can the program be modified to ignore punctuation when counting words?
    By removing punctuation from the text before splitting it into words.

  9. What improvements could be made to the program?
    Adding functionality to handle different languages or character sets and providing a user-friendly interface for input.

  10. Can the program handle large texts efficiently?
    Yes, with proper implementation, the program can handle large texts, but memory management and optimization techniques may be necessary.

๐Ÿ“Œ Python Programming Lab – Complete Guide

Welcome to the Python Programming Lab! This comprehensive lab series is designed to provide a solid foundation in Python programming through practical exercises and hands-on learning. From basic calculations to advanced concepts like file handling, modules, and GUI programming, this lab covers it all. Letโ€™s dive into what youโ€™ll be learning throughout this lab course.


๐Ÿ” Week 1: Introduction to Python Basics

In the first week, you will explore fundamental Python concepts aimed at building a strong foundation for your programming journey.

  • Getting Started with Python: Visit the Python Official Website to explore documentation and use the help() function in the interpreter.
  • Python as a Calculator: Learn how to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
  • Calculating Compound Interest: Write a program to calculate compound interest using the formula with given principal, rate, and time.
  • Distance Calculation: Compute the distance between two points using the distance formula.
  • Reading User Details: Write a program to collect and print user details like name, address, email, and phone number.

๐Ÿ” Week 2: Python Loops and Conditional Statements

This week focuses on using loops and conditional statements effectively.

  • Pattern Generation: Print a triangle pattern using nested loops.
  • Character Identification: Write a program to identify whether the input is a digit, lowercase, uppercase, or special character.
  • Fibonacci Sequence Generation: Use a while loop to generate the Fibonacci series.
  • Prime Numbers Identification: Find all prime numbers within a specified range using the break statement.

๐Ÿ” Week 3: Data Structures and Functions

Dive into data structures and creating functions to enhance your programming skills.

  • Converting Lists and Tuples to Arrays: Understand how to convert different data structures.
  • Finding Common Values: Compare two arrays to find common values.
  • Calculating GCD: Create a function to calculate the greatest common divisor (GCD) of two numbers.
  • Palindrome Checker: Write a function that checks if a given string is a palindrome.

๐Ÿ” Week 4: Advanced Functions and String Manipulation

This week emphasizes sorting, handling duplicate elements, and manipulating strings effectively.

  • Checking Sorted Lists: Write a function to check if a list is sorted.
  • Handling Duplicates: Create functions to detect and remove duplicates from lists.
  • Dictionary Inversion: Write a program to swap keys and values in a dictionary.
  • String Manipulation: Add commas between characters, remove words from strings, and convert sentences to title case without using built-in functions.
  • Binary String Generation: Use recursion to generate all binary strings of a specified length.

๐Ÿ” Week 5: Working with Matrices and Modules

Learn to work with matrices and create custom modules for various applications.

  • Matrix Operations: Define, print, add, and multiply square matrices using Python.
  • Creating Modules: Build modules using geometrical shapes and their operations.
  • Exception Handling: Implement exception handling for robust error management.

๐Ÿ” Week 6: Object-Oriented Programming and Validation

This week focuses on using classes, inheritance, and validation techniques.

  • Drawing Shapes with Classes:
    • Create classes for rectangles, points, and circles, and draw them on a canvas.
    • Add colors and attributes to these shapes to enhance visualization.
  • Method Resolution Order (MRO): Demonstrate MRO in multiple inheritance scenarios.
  • Data Validation: Write programs to validate phone numbers and email addresses.

๐Ÿ” Week 7: File Handling and Text Processing

Master file handling techniques and analyzing text data.

  • File Merging: Combine the contents of two files into a new file.
  • Word Search: Create a function to find specific words in a file.
  • Word Frequency Analysis: Identify the most frequent words in a text file.
  • Text Statistics: Count words, vowels, blank spaces, lowercase, and uppercase letters in a file.

๐Ÿ” Week 8: Python Libraries and GUI Programming

Explore advanced Python libraries and build simple graphical user interfaces.

  • NumPy, Plotly, and Scipy: Learn how to install and use these powerful libraries for data analysis and visualization.
  • Digital Logic Gates: Implement logic gate operations such as AND, OR, NOT, and EX-OR.
  • Adder Circuits: Create programs for Half Adders, Full Adders, and Parallel Adders.
  • GUI Programming: Build a simple window wizard with text labels, input fields, and buttons using Python’s GUI tools.

๐ŸŒŸ Conclusion:

The Python Programming Lab R23 provides you with a strong foundation in Python programming through a wide range of exercises, from basic concepts to advanced topics. By the end of this lab, you will have gained valuable skills in data processing, file handling, OOP, modules, and GUI development. Start coding and enhance your Python skills! ๐Ÿš€


;