# This program translates a single line of text from text messaging # abbreviations to English # by Prof. Dipak Ghosal def buildmapping(filename): # create the dictionary of im abbreviations to english # the mappings are in a file dictionary = {} infile = open(filename, "r") for line in infile: temp = line.split(":") dictionary[temp[0]]=temp[1].rstrip() infile.close() return dictionary def translateabbrev(imword, dictionary): # takes a word in the immesage (imword) and translates it # usign the dictionary # first, determine if the last character is a puntuation lastchar = imword[len(imword) - 1] if lastchar in ",.?!;:": imword = imword.rstrip(lastchar) else: lastchar = "" # now translate if imword in dictionary: word = dictionary[imword] else: word = imword return word+lastchar def main(): dictionary = buildmapping("abbreviations.txt") im = input("Please input the IM: ") parts = im.split() translatedmessage = "" for i in parts: translatedmessage = translatedmessage + translateabbrev(i, dictionary) + " " print(translatedmessage) main()