PYTHON: Translation - Instant message abbreviation --> text?

In class we developed the IM translation program which takes an IM message consisting of acronyms and converts them full text. We used a dictionary which was created from a file called abbreviations.txt. You are required to extend the code to account for the following two additional requirements 1) take care punctuations and 2) if acronym is not in the dictionary, then keep it as it is in the translated message. - Use "abbreviations.txt" as the dictionary file name. Don't ask user the file name. - Punctuation mark only appears at the end of a word. There must be a blank between a punctuation mark and the next word. The punctuation marks you need to handle are: ",", ".", "?", and "!".

abbreviations.txt =

y:why

r:are

u:you

l8:late

lol:laught out loud

BRB:Be right back

Here is my code so far :

# diimenhanced.py
#

def main():
    infile = open("abbreviations.txt", "r")
    d = {}
    w = []
    for line in infile:
        temp = line.split(":")
        d[temp[0]]=temp[1].rstrip()
        w.append(temp[0])
    infile.close()
    im = input("Please input the instant message:   ")
    tim = ""
    templist = im.split()

    for i in templist:
        if i[-1] in w:
            tim = tim + ' ' + d[i]
        else:
            word = i[0:-1]
            if word in w:
                tim = tim + ' ' + d[word] + i[-1]
            else:
                tim = tim + i

    print(tim)

main()

I've been trying to get it to work but it keeps on spitting out the same IM unless i put a period at the end. ex: BRB. ---> Be right back. and ex: BRB ---> BRB :/ due Friday tomorrow, really need help! THANK YOU! and please im only in introductory python so if you could refrain from using BUILT-IN functions or complicated methods.

Answers


you can try to split it into a list and make substitution there

def main():
    infile = open("abbreviations.txt", "r")
    d = {}
    w = []
    for line in infile:
        temp = line.split(":")
        d[temp[0]]=temp[1].rstrip()
        # For easy punctuation marks manage add these rules
        d[temp[0]+"?"] = temp[1].rstrip()+"?"
        d[temp[0]+"!"] = temp[1].rstrip()+"!"
        d[temp[0]+","] = temp[1].rstrip()+","
        d[temp[0]+"."] = temp[1].rstrip()+"."
        w.append(temp[0])
    infile.close()
    im = input("Please input the instant message:   ")
    im = im.split() # Split with space as separator
    res = "" # String to store the result
    for i in im: #Iterate over words
        if i in d: # If the word is in our dic
            res += d[i] # Add the substitution to result
        else: # If no substitution needed
            res += i  # Add original word
        res += " " # Add a space after each word

    res = res.strip() # Delete extra spaces at the end
    print(im)
    print(res)


main()

Need Your Help

Does WCF really replace .NET Remoting?

c# .net wcf .net-remoting

I can understand that WCF is in general better than Remoting, but the two seem quite different to me. MS make this pretty picture to show how great WCF is (or perhaps how poor the other techs are t...