Advertisement
Python253

generate_acronyms

Jun 3rd, 2024 (edited)
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: generate_acronyms.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script generates acronyms from text input and saves them to files.
  10.    - It provides options to run a demo with sample text or generate acronyms from user-selected text files.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.    - The following modules:
  15.        - tkinter
  16.        - string
  17.  
  18. Functions:
  19.    - create_acronym_from_text(text):
  20.        Generates an acronym from the given text.
  21.    - run_demo():
  22.        Runs a demo to generate an acronym from sample text and save it to a file.
  23.    - generate_acronym_from_file():
  24.        Generates an acronym from a text file selected by the user and saves it to a file.
  25.    - main():
  26.        Displays menu options and executes corresponding actions.
  27.  
  28. Usage:
  29.    - Run the script and follow the prompts to choose between running a demo or generating acronyms from a file.
  30.  
  31. Additional Notes:
  32.    - A larger sample story with hidden acronym messages can be found here: https://pastebin.com/d9fWM75H
  33.    - Save the file as: 'hidden_lore_exploration_log.txt' in the current working directory & run this script.
  34. """
  35.  
  36. import sys
  37. import tkinter as tk
  38. from tkinter import filedialog
  39. import string
  40.  
  41. def create_acronym_from_text(text):
  42.     """
  43.    Generate an acronym from the given text.
  44.  
  45.    Args:
  46.        text (str): The input text from which the acronym will be generated.
  47.  
  48.    Returns:
  49.        str: The acronym generated from the input text.
  50.    """
  51.     # Define punctuation characters
  52.     punctuations = string.punctuation
  53.  
  54.     # Remove punctuations from the text
  55.     text_no_punctuations = ''.join(char for char in text if char not in punctuations)
  56.  
  57.     # Create acronym from text without punctuations
  58.     return ''.join(word[0] for word in text_no_punctuations.split() if word)
  59.  
  60. def run_demo():
  61.     """
  62.    Run a demo to generate an acronym from sample text and save it to a file.
  63.    Demo Result: "HELLO ACRONYM HAHA!" (HELLOACRONYMHAHA)
  64.    """
  65.     sample_text = """
  66.    HELLO, EVERYONE! LISTEN, LET'S OFFER A CHEERFUL RECEPTION. OH, NEVERMIND, YOU MEANT HELLO ACRONYM. HELLO ACRONYM!
  67.     """
  68.     acronym = create_acronym_from_text(sample_text)
  69.     output_filename = 'acronym_string.txt'
  70.     with open(output_filename, 'w', encoding='utf-8') as outfile:
  71.         outfile.write(acronym)
  72.     print(f"\nSample Text:\n'{sample_text}'")
  73.     print(f"\nAcronym string:\n'{acronym}'")
  74.     print(f"\nProcesses are complete and the file '{output_filename}' was saved in the CWD.")
  75.     sys.exit(0)
  76.  
  77. def generate_acronym_from_file():
  78.     """
  79.    Generate an acronym from a text file selected by the user and save it to a file.
  80.    """
  81.     try:
  82.         root = tk.Tk()
  83.         root.withdraw()  # Hide the root window
  84.         file_path = filedialog.askopenfilename(title="Select the text file", filetypes=(("Text files", "*.txt"), ("All files", "*.*")))
  85.        
  86.         if not file_path:
  87.             print("\nNo file selected!   Exiting Program...   GoodBye!\n")
  88.             sys.exit(1)
  89.        
  90.         with open(file_path, 'r', encoding='utf-8') as infile:
  91.             content = infile.read()
  92.        
  93.         acronym = create_acronym_from_text(content)
  94.         output_filename = 'acronym_string.txt'
  95.         with open(output_filename, 'w', encoding='utf-8') as outfile:
  96.             outfile.write(acronym)
  97.        
  98.         print(f"Acronym string: {acronym}")
  99.         print(f"\nProcesses are complete and the file '{output_filename}' was saved in the CWD.")
  100.         sys.exit(0)
  101.    
  102.     except Exception as e:
  103.         print(f"\nAn error occurred: {e}\n")
  104.         sys.exit(1)
  105.  
  106. def main():
  107.     """
  108.    Main function to display menu options and execute corresponding actions.
  109.    """
  110.     while True:
  111.         print("Menu:\n")
  112.         print("1: Run Demo")
  113.         print("2: Generate Acronym")
  114.         print("0: Exit")
  115.         choice = input("\nEnter your choice: ")
  116.        
  117.         if choice == '1':
  118.             run_demo()
  119.         elif choice == '2':
  120.             generate_acronym_from_file()
  121.         elif choice == '0':
  122.             print("\nExiting Program...   GoodBye!\n")
  123.             sys.exit(0)
  124.         else:
  125.             print("\nInvalid choice! Please try again.\n")
  126.  
  127. if __name__ == "__main__":
  128.     main()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement