pyton: In this assignment, you are writing a program that converts common textin
ID: 3572885 • Letter: P
Question
pyton: In this assignment, you are writing a program that converts common texting abbreviations to English words to allow people like yours truly can understand. OMG! 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 called Assignment10_LastName.py, 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”.
Explanation / Answer
# your code goes here#!/usr/bin/python
def CreateDictionary (fileName):
dict_file= open(fileName)
abbreviation_dictionary = {}
"""
Populated Abbreviations - Meanings
"""
for abbr in dict_file :
"""
removing new line if present
"""
abbr = abbr.replace(" ","")
array = abbr.split(":")
abbreviation_dictionary[array[0]] = array[1]
return abbreviation_dictionary
def Deslang(stri,abbr_dictionary):
for word in stri.split():
if word in abbr_dictionary.keys():
stri.replace(word,abbr_dictionary[word])
return stri
def main() :
#fileName to go in CreateDictionary function
wordDictionary = CreateDictionary (fileName)
print "Enter 1 to check abbreviations. Enter 2 to deslang sentences. Enter quit anytime to quit"
user_number =input("Enter your choice: ")
if (user_number == 1):
while True:
user_input = raw_input("Enter word: ")
if (user_input == "quit"):
break
if user_input in wordDictionary.keys():
print wordDictionary[user_input]
else :
print "Not Found"
elif (user_number == 2) :
while True:
user_input = raw_input("Enter sentence: ")
if (user_input == "quit"):
break
print Deslang(user_input,wordDictionary)
if __name__ == "__main__":main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.