Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: generate_acronyms.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script generates acronyms from text input and saves them to files.
- - It provides options to run a demo with sample text or generate acronyms from user-selected text files.
- Requirements:
- - Python 3.x
- - The following modules:
- - tkinter
- - string
- Functions:
- - create_acronym_from_text(text):
- Generates an acronym from the given text.
- - run_demo():
- Runs a demo to generate an acronym from sample text and save it to a file.
- - generate_acronym_from_file():
- Generates an acronym from a text file selected by the user and saves it to a file.
- - main():
- Displays menu options and executes corresponding actions.
- Usage:
- - Run the script and follow the prompts to choose between running a demo or generating acronyms from a file.
- Additional Notes:
- - A larger sample story with hidden acronym messages can be found here: https://pastebin.com/d9fWM75H
- - Save the file as: 'hidden_lore_exploration_log.txt' in the current working directory & run this script.
- """
- import sys
- import tkinter as tk
- from tkinter import filedialog
- import string
- def create_acronym_from_text(text):
- """
- Generate an acronym from the given text.
- Args:
- text (str): The input text from which the acronym will be generated.
- Returns:
- str: The acronym generated from the input text.
- """
- # Define punctuation characters
- punctuations = string.punctuation
- # Remove punctuations from the text
- text_no_punctuations = ''.join(char for char in text if char not in punctuations)
- # Create acronym from text without punctuations
- return ''.join(word[0] for word in text_no_punctuations.split() if word)
- def run_demo():
- """
- Run a demo to generate an acronym from sample text and save it to a file.
- Demo Result: "HELLO ACRONYM HAHA!" (HELLOACRONYMHAHA)
- """
- sample_text = """
- HELLO, EVERYONE! LISTEN, LET'S OFFER A CHEERFUL RECEPTION. OH, NEVERMIND, YOU MEANT HELLO ACRONYM. HELLO ACRONYM!
- """
- acronym = create_acronym_from_text(sample_text)
- output_filename = 'acronym_string.txt'
- with open(output_filename, 'w', encoding='utf-8') as outfile:
- outfile.write(acronym)
- print(f"\nSample Text:\n'{sample_text}'")
- print(f"\nAcronym string:\n'{acronym}'")
- print(f"\nProcesses are complete and the file '{output_filename}' was saved in the CWD.")
- sys.exit(0)
- def generate_acronym_from_file():
- """
- Generate an acronym from a text file selected by the user and save it to a file.
- """
- try:
- root = tk.Tk()
- root.withdraw() # Hide the root window
- file_path = filedialog.askopenfilename(title="Select the text file", filetypes=(("Text files", "*.txt"), ("All files", "*.*")))
- if not file_path:
- print("\nNo file selected! Exiting Program... GoodBye!\n")
- sys.exit(1)
- with open(file_path, 'r', encoding='utf-8') as infile:
- content = infile.read()
- acronym = create_acronym_from_text(content)
- output_filename = 'acronym_string.txt'
- with open(output_filename, 'w', encoding='utf-8') as outfile:
- outfile.write(acronym)
- print(f"Acronym string: {acronym}")
- print(f"\nProcesses are complete and the file '{output_filename}' was saved in the CWD.")
- sys.exit(0)
- except Exception as e:
- print(f"\nAn error occurred: {e}\n")
- sys.exit(1)
- def main():
- """
- Main function to display menu options and execute corresponding actions.
- """
- while True:
- print("Menu:\n")
- print("1: Run Demo")
- print("2: Generate Acronym")
- print("0: Exit")
- choice = input("\nEnter your choice: ")
- if choice == '1':
- run_demo()
- elif choice == '2':
- generate_acronym_from_file()
- elif choice == '0':
- print("\nExiting Program... GoodBye!\n")
- sys.exit(0)
- else:
- print("\nInvalid choice! Please try again.\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement