// Jason Gross Commack HS SR Div

 

import re

class TranslatorString(object):

    def __init__(self, sentence):

        self._origSent = sentence

        self._words = sentence.split(' ')

        self._locked = [[False for letter in word] for word in self._words]

        self._sentence = [[letter for letter in word] for word in self._words]

    def applyReplace(self, find, replace, oncePerWord=False):

        for word in range(len(self._words)):

            if not oncePerWord:

                cur = find.search(self._words[word])

                last = (-1, -1)

                while cur != None and cur.start() > last[0] and cur.end() > last[1]:

                    last = (cur.start(), cur.end())

##                    print (self._sentence[word][cur.start():cur.end()], self._words[word][cur.start():cur.end()], replace, ''.join([''.join(j) for j in self._sentence]))

                    if not any([i for i in self._locked[word][cur.start():cur.end()]]):

                        self._locked[word][cur.start():cur.end()] = [True for i in range(cur.start(), cur.end())]

                        self._sentence[word][cur.start()] = replace

                        self._sentence[word][cur.start() + 1:cur.end()] = ['' for i in range(cur.start() + 1, cur.end())]

##                        print (self._sentence[word][cur.start():cur.end()], self._words[word][cur.start():cur.end()], replace, ''.join([''.join(j) for j in self._sentence]))

                        cur = find.search(self._words[word], cur.end())

                    else:

                        cur = find.search(self._words[word], cur.start() + 1)

##                        if cur != None: print (self._sentence[cur.start():cur.end()],  self._origSent[cur.start():cur.end()])

##                        else: print last

            else:

                cur = find.search(self._words[word])

                if cur != None:

##                    print (self._sentence[word][cur.start():cur.end()], self._words[word][cur.start():cur.end()], replace, ''.join([''.join(j) for j in self._sentence]))

                    if not any([i for i in self._locked[word][cur.start():cur.end()]]):

##                        print True

                        self._locked[word][cur.start():cur.end()] = [True for i in range(cur.start(), cur.end())]

                        self._sentence[word][cur.start()] = replace

                        self._sentence[word][cur.start() + 1:cur.end()] = ['' for i in range(cur.start() + 1, cur.end())]

##                        print (self._sentence[word][cur.start():cur.end()], self._words[word][cur.start():cur.end()], replace, ''.join([''.join(j) for j in self._sentence]))

    def _getSentence(self):

        return ' '.join([''.join(word) for word in self._sentence])

    Sentence = property(_getSentence)

class Translator(object):

    def __init__(self, priorities):

        self._priorities = [(re.compile(i[0]), i[1], i[2]) for i in priorities]

    def parse(self, sentence):

        rtn = TranslatorString(sentence)

        for i in self._priorities:

            rtn.applyReplace(i[0], i[1], i[2])

        return rtn.Sentence

def initProg(string):

    endSentence = "(?<=[.!\\?])"

    pri1 = [(i[0], i[1].upper(), (False if len(i) == 2 else i[2])) for i in

            [#("\\Z"     , ' Bork Bork Bork!'),

             ("THE"     , 'ZEE'),

             ("AN"      , 'UN'),

             ("AU"      , 'OO'),

             ("A\\B"    , 'E'),

             ("OW"      , 'OO'),

             ("O"       , 'U'),

             ("IR"      , 'UR'),

             ("\\BTION" , 'SHUN'),

             ("\\BI"    , 'EE', True),

             ("EN\\b"   , 'EE'),

             ("F"       , 'FF'),

             ("E\\b"    , 'E-A'),

             ("\\BU"    , 'OO'),

             ("V"       , 'F'),

             ("W"       , 'V')]]

    pri2 = [(i[0], i[1].upper(), (False if len(i) == 2 else i[2])) for i in

            [#("\\Z"     , ' Bork Bork Bork!'),

             ("THE"     , 'ZEE'),

             ("AN"      , 'UN'),

             ("AU"      , 'OO'),

             ("A\\B"    , 'E'),

             ("OW"      , 'OO'),

             ("\\BTION" , 'SHUN'),

             ("O"       , 'U'),

             ("IR"      , 'UR'),

             ("\\BI"    , 'EE', True),

             ("EN\\b"   , 'EE'),

             ("F"       , 'FF'),

             ("E\\b"    , 'E-A'),

             ("\\BU"    , 'OO'),

             ("V"       , 'F'),

             ("W"       , 'V')]]

    pri12 = [(i[0], i[1].upper(), (False if len(i) == 2 else i[2])) for i in

            [(endSentence, ' Bork Bork Bork!'),

             ("THE"     , 'ZEE'),

             ("AN"      , 'UN'),

             ("AU"      , 'OO'),

             ("A\\B"    , 'E'),

             ("OW"      , 'OO'),

             ("O"       , 'U'),

             ("IR"      , 'UR'),

             ("\\BTION" , 'SHUN'),

             ("\\BI"    , 'EE', True),

             ("EN\\b"   , 'EE'),

             ("F"       , 'FF'),

             ("E\\b"    , 'E-A'),

             ("\\BU"    , 'OO'),

             ("V"       , 'F'),

             ("W"       , 'V')]]

    pri22 = [(i[0], i[1].upper(), (False if len(i) == 2 else i[2])) for i in

            [(endSentence, ' Bork Bork Bork!'),

             ("THE"     , 'ZEE'),

             ("AN"      , 'UN'),

             ("AU"      , 'OO'),

             ("A\\B"    , 'E'),

             ("OW"      , 'OO'),

             ("\\BTION" , 'SHUN'),

             ("O"       , 'U'),

             ("IR"      , 'UR'),

             ("\\BI"    , 'EE', True),

             ("EN\\b"   , 'EE'),

             ("F"       , 'FF'),

             ("E\\b"    , 'E-A'),

             ("\\BU"    , 'OO'),

             ("V"       , 'F'),

             ("W"       , 'V')]]

    theType = 1

    if theType == 1: return (string.upper().strip(), pri1, theType)

    elif theType == 2: return (string.upper().strip(), pri2, theType)

    if theType == 12: return (string.upper().strip(), pri12, theType)

    elif theType == 22: return (string.upper().strip(), pri22, theType)

def runProg(sentence, priorities, theType):

    rtn = Translator(priorities)

    if theType in (1, 2): return rtn.parse(sentence) + " Bork Bork Bork!".upper()

    else: return rtn.parse(sentence)

def runACSL3():

    print 'ACSL Senior Program 3: Swedish Chef'

    print 'By Jason Gross'

    print ''

    print 'Type in the input when prompted.'

    print 'Press enter to signify that a line of input has been entered.'

    print '(Hit enter after each line)'

   # print "Type 'QUIT' and press enter to signify that all lines of input"

   # print 'have been entered (this exits the program).'

    print ''

    print "To exit, hold down 'Ctrl' and press the 'C' key."

    print ''

    var = raw_input('Enter input: ')

    while True:

        try:

            prog = initProg(var)

            output = runProg(*prog)

            print 'The output is:'

            print output

        except BaseException, e:

            print 'There has been an error: %s' % str(e)

            print 'This program cannot solve this input'

            print 'Continue with the other inputs'

        print ''

        var = raw_input('Enter input: ')

runACSL3()