Reference no: EM132356960
The Deslangifier
In this assignment, you are writing a program that converts common texting abbreviations to English words to allow people like yours truly can understand. For the assignment, you are provided with a .txt file, called TextToEnglish.txt, that provides the abbreviations and their English translation. The file contains GRated translations only. The txt file has comma separated values that list the key as the first item, a comma to separate the key from the value, and then the value.
For this assignment, you need to submit one file, where LastName is your last name. Please include comments in your code that explain what your code is doing. The comments should also include your name, recitation TA, and the assignment number. Each function should be commented with the functions purpose and description of the parameters.
Steps:
1. Write a function, called CreateDictionary that is given the name of the text file to be read. The function opens the given file, reads in data, and generates a dictionary. The dictionary key is the text abbreviation and the value is the English translation.
For example, one entry in your dictionary will be, ‘l8': ‘late' because one of the rows in the txt file contains ‘l8' and ‘late'. ?Your function should have one input: the name of the file to open, and one return value: the dictionary. Test your function before moving on to the next step. You can do this by defining the function and then calling it in your .py file: ?wordDictionary = CreateDictionary(filename)
2. Write a function Deslang(...) that takes two parameters, a string and a dictionary, and returns the deslanged string. Each word in the string will be replaced if it has an entry in the dictionary. Any word not in the dictionary should be copied to the results. ?deslanged = Deslang(slang, wordDictionary) ?For example, if the slang string is: "David, y r u l8"?your function should return: "David, why are you late"
3. Your main function in the .py file containing your functions, CreateDictionary, and Deslang, should do the following:
a. Call the CreateDictionary function.
b. Prompt the user for a text abbreviation and check if it is in the dictionary. If it is, print the English words associated with the abbreviation, otherwise print "Not Found". ?Your main must also allow the user to continue inputting abbreviations and printing the English meaning of those abbreviations until the user enters "quit".
c. Prompt the user for an arbitrary number of text abbreviations, separated by a space, and print a string that displays the meaning of all abbreviations (by calling the Deslang function). ?Your main must also allow the user to continue inputting sentences with abbreviations and printing the English meaning of those sentences without abbreviations until the user enters "quit".
For this assignment, will be testing your code by importing your code into our own .py files and calling your functions. We will not use your main function. Therefore, your functions for CreateDictionary and Deslang should not produce any output or require any input from user.
You can test this by creating another .py file that imports your assignment into it and has its own main function that calls your required functions.
Lists:
A list in python is an important data type. To store elements as a list, place all the elements enclosed within square brackets and separated by comma. Unlike in c++ we can have different types of elements in the same python list. Below are some of the examples of lists
myList = [] # empty list?myList = [1, 2, 3]# list of integers?myList = [1, "Hello", 3.4]# list with mixed datatypesmyList = ["csci1300", [2, 4, 6]] # nested list
Access elements in a list
To access the elements in a list we need to use the index operator [ ]. Similar to arrays in c++, python lists index starts from 0. For example, a list having 10 elements will have index from 0 to 9.
Dictionaries in Python:
A dictionary is an associative array. It is also known as hashes. Dictionaries in python have a key and value pairs; where each key is associated to a value. Keys in dictionary should be unique and the values can be of any Python data type. Due to this reason dictionaries in python are unordered key-value pairs.
In a dictionary each and every key is separated from its value by a colon (:), the elements are separated by commas, and the whole thing is enclosed in curly braces.
Deleting a Dictionary and specific elements:
To delete a element in a dictionary with a specific key we use the below command
del mydict['Name'] # remove the element with key 'Name'
To delete all the elements in a dictionary we use the below command
mydict.clear()# delete all the entries in dictTo delete the entire dictionary we use the below command
delmydict# delete the entire dictionary File I/O in Python:
The file I/O operations are similar to C++. The steps followed to open and close a file are as shown below
1) Open a file in the correct mode and store it in a file object.?2) Use this file object to modify or read from the file as required.?3) Once done with all the operations in a file , please make sure to close the file.
Recitation Activity
Write a function called countElements that counts the number of times two elements appear in a list. Your task is to ask the user to enter a list and two elements which they need to count in the list. The list and each of the elements should be inputs to a function called countElements. The function should print the count of the number of times the elements appear in the list.