CS303E Homework 11

Instructor: Dr. Bill Young
Due Date: Friday, November 4, 2022 at 11:59pm

Overview

Text abbreviations are shortened forms of words or phrases that save time and characters. They're often used for social media posts and text messages to limit the number of characters used. Many text abbreviations are also used as text slang, especially by younger generations, but they can be mysterious to older folks. Perhaps a useful utility would be a translator. That's what you'll be building in this homework.

In this file Texting Terms is a listing of some common texting terms with their definitions. I found this online (so please don't bug me if it's inaccurate, incomplete, or a bit vulgar). You should copy this file into your own directory/folder. Each line in the file has the form term:definition. You'll use the information contained to create a translator from texting slang to more standard English.

Your task is to read lines from the file to create a Python dictionary where each term is associated with its definition. (Note that each of the terms in this list is a single word; each definition is a word or phrase.) Then you'll build a menu-driven application that will allow the user to perform the following actions:

  1. display the meaning of a term;
  2. provide the translation of a message (replacing terms in the dictionary with their definitions);
  3. extend or change the dictionary by adding a new term/definition pair or replacing the definition of an existing term;
  4. print a menu of options;
  5. exit the application.
To complete this homework, you'll perform several tasks:

Task 1: Build the dictionary: Ask the user for a filename and verify that the file exists. If not, print an error message and exit. If so, open the file for reading. Read lines from the file, parse each line, and insert the term/definition pair into the dictionary. Lowercase each term before using it as a key into the dictionary; leave the definitions as supplied. Return the resulting dictionary.

Task 2: Isolate punctuation: Write a function that crawls through a string and inserts a space before some puctuation marks. Return the new string. Thus,

Hey Grannie, PLMK your POV on the QTPI I'm seeing. ILU!
becomes
Hey Grannie , PLMK your POV on the QTPI I'm seeing . ILU !
This step just makes the later transformations simpler. (Notice that I didn't change the single quote since it often falls in the middle of words.)

Task 3: Translate a string: Write a function that takes a string and dictionary and replaces words in the string that are defined in the dictionary with their definitions. For example,

Hey Grannie , PLMK your POV on the QTPI I'm seeing . ILU !
becomes
Hey Grannie , Please let me know your point of view on the Cutie pie I'm seeing . I love you !
Don't worry about the odd punctuation or the fact that some words may be capitalized (or not). Fixing those would be more complicated that what's needed for this assignment..

Task 4: Write the main program: Write a main program that does the following steps:

  1. Print a welcome message.
  2. Accept from the user a filename for the file containing the directory information. Check that it exists; print an error message and return, if not.
  3. Create the dictionary.
  4. Write a loop that accepts commands from the user and executes the commands. Commands are specified by numbers 1 to 5.
  5. Upon exit, print a Goodbye message.
See the sample output below for how all of this should work together.

Sample Output

> python TextTranslate.py

Welcome to the Text Translator application.

Specify file containing terms and definitions: noSuchFile
File does not exist: noSuchFile

> python TextTranslate.py

Welcome to the Text Translator application.

Specify file containing terms and definitions: textingTerms
Building dictionary from: textingTerms

The following actions are available:
  1 - Explain a term.
  2 - Translate a message.
  3 - Extend/Change the dictionary.
  4 - Show this menu.
  5 - Exit the application

Choose an action (1-5): 17
Action not recognized; please enter 1-5.

Choose an action (1-5): abcd
Action not recognized; please enter 1-5.

Choose an action (1-5): 1
  Please enter a term to explain: Grannie

Term not defined: Grannie

Choose an action (1-5): 1
  Please enter a term to explain: IMho

IMho: In my humble opinion

Choose an action (1-5): 2
  Please enter a message to translate: Grannie, WTF. Is your HUB? PLZ TBL to explain. I'm LMAO here.

Message translation: Grannie , What the f*** . Is your Head up your butt ? Please Text back later to explain . I'm Laughing my a** off here . 

Choose an action (1-5): 4

The following actions are available:
  1 - Explain a term.
  2 - Translate a message.
  3 - Extend/Change the dictionary.
  4 - Show this menu.
  5 - Exit the application

Choose an action (1-5): 3
  Add the following term: Grannie
  With definition: Dearest Grandmother

Choose an action (1-5): 1
  Please enter a term to explain: grannie

grannie: Dearest Grandmother

Choose an action (1-5): 3
  Add the following term: wtf
  With definition: Could you please explain yourself

Choose an action (1-5): 3
  Add the following term: hub
  With definition: head in the right place

Choose an action (1-5): 3
  Add the following term: lmao
  With definition: sincerely amused

Choose an action (1-5): 2
  Please enter a message to translate: Grannie, WTF. Is your HUB? PLZ TBL to explain. I'm LMAO here.

Message translation: Dearest Grandmother , Could you please explain yourself . Is your head in the right place ? Please Text back later to explain . I'm sincerely amused here . 

Choose an action (1-5): 4

The following actions are available:
  1 - Explain a term.
  2 - Translate a message.
  3 - Extend/Change the dictionary.
  4 - Show this menu.
  5 - Exit the application

Choose an action (1-5): help                             # Not in the official
                                                         # interface, but see the
The following actions are available:                     # programming tips below.
  1 - Explain a term.
  2 - Translate a message.
  3 - Extend/Change the dictionary.
  4 - Show this menu.
  5 - Exit the application

Choose an action (1-5): 5

Thanks for using this app. Goodbye!

> 

Turning in the Assignment:

The program should be in a file named TextTranslator.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to hw11 in the assignments sections by uploading your Python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: TextTranslator.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

Programming Tips:

Add robustness: I always find it a good idea to make any system I code as robust and user-friendly as possible. For example, I find it annoying when using a system to have to worry about the case of input commands. Why not make commands case insensitive so that "EnCRypt" works as well as "encrypt"? It's easy to do that; just lowercase the command entered by the user (using comm.lower()) and then compare that to the lowercase version. If they match, accept the command; otherwise, reject it.

Remember: if the command is stored in variable comm, comm.lower() doesn't change comm. You'd want to do something like:

   commLower = comm.lower()
Also, when using a menu driven system like the one you're coding here, I find it annoying when I can't remember either the command to exit, or the command to get the help menu. In my own implementation I allowed 'exit', 'quit', 'leave' as alternatives to exit the program, and allowed 'help' and 'info' as commands that would print the help menu. I typically don't publicize such commands because they're outside the "official" interface. But they make things more user-friendly. You don't have to do this in your program, but you're welcome to if you like. The TAs will only test the official interface.

Choose the correct type: When interacting with the application menu in this homework, the user inputs a number 1, ..., 5 but it's read as a string. You could convert it to a int, but there's no reason to do that because you never do any arithmetic using these inputs. Why not just leave them as strings.

There's another reason not to convert for this assignment: If you want to add alternative commands, like "exit", "info", etc. then you don't need to hassle dealing with commands that can be either strings or integers. I defined the following global constant:

EXPLAIN_TERM = "1"
...
EXIT_APP     = "5"

LEGAL_COMMANDS = [EXPLAIN_TERM, TRANSLATE_MSG, EXTEND_DICT, SHOW_MENU, EXIT_APP,
                  'exit', 'quit', 'leave', 'help', 'info']
Having this global constant makes it easy to add command variants. A command is syntactically legal if and only if it's in the list.