
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:
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:
> 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!
>
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:
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.