a phython problem There are a dictionary called textToEnglush.txt and looks like
ID: 3774973 • Letter: A
Question
a phython problem
There are a dictionary called textToEnglush.txt
and looks like this
$$,money
/.,Slashdot
*4u,Kiss for you
*67,unknown
*eg*,evil grin
0day,software illegally obtained before it was released
0noe,Oh No
0vr,over
10q,thank you
10tacle,Tentacle
10x,thanks
12b,wannabe
1337,elite
133t,elite
143,I love you
187,murder
1ab,wannabe
1daful,wonderful
1dering,wondering
1nce,once
1sec,one second
2,too
2b,to be
2b4u,too bad for you
2bad4u2,too bad for you too
2bh,to be honest
2da,to the
2dae,today
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
Code:
def createDictionary(filename):
a = open(filename,"r")
b = a.readlines()
d = {}
for i in b:
c = i.split(",")
d[c[0]] = c[1][:-1]
return d
wordDictionary = createDictionary("input.txt")
def Deslang(slang,wordDictionary):
slangd = slang.split(" ")
for i in range(len(slangd)):
if slangd[i] in wordDictionary.keys():
slangd[i] = wordDictionary[slangd[i]]
else:
slangd[i] = slangd[i]
return " ".join(slangd)
while True:
slang = input("Please enter a text abbreviation:")
if(input == "quit"):
break
else:
result = Deslang(slang,wordDictionary)
print(result)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.