Advertisement
Python253

consonants_count

Mar 3rd, 2024 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: consonants_count.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Consonants Count Script
  8.  
  9. This script reads text from a specified file in the same directory and counts
  10. the number of consonants in the text. It utilizes a function, count_consonants,
  11. to perform the consonant counting operation.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Save the text file in the same directory as the script.
  18. 2. Replace 'texty.txt' with the actual file name in the script.
  19. 3. Run the script.
  20. 4. The script will display the number of consonants in the text.
  21. """
  22.  
  23. def count_consonants(text):
  24.     """Count the number of consonants in a text.
  25.  
  26.    Args:
  27.        text (str): Text.
  28.  
  29.    Returns:
  30.        int: Number of consonants in the text.
  31.  
  32.    """
  33.     consonants = set("bcdfghjklmnpqrstvwxyz")
  34.     return sum(char.lower() in consonants for char in text)
  35.  
  36. def read_text_from_file(file_path):
  37.     """Read text from a file.
  38.  
  39.    Args:
  40.        file_path (str): Path to the file.
  41.  
  42.    Returns:
  43.        str: Text read from the file.
  44.  
  45.    """
  46.     with open(file_path, 'r', encoding='utf-8') as file:
  47.         return file.read()
  48.  
  49. def main():
  50.     # Specify the file path
  51.     file_path = 'texty.txt'  # Replace 'texty.txt' with the actual file name
  52.  
  53.     # Read text from the file
  54.     text = read_text_from_file(file_path)
  55.  
  56.     # Count consonants
  57.     consonant_count = count_consonants(text)
  58.  
  59.     # Display the result
  60.     print(f"Number of consonants in the text: {consonant_count}")
  61.  
  62. if __name__ == "__main__":
  63.     main()
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement