Home > Net >  Create a function in python that replaces "to be honest" in a sentence with "TBH"
Create a function in python that replaces "to be honest" in a sentence with "TBH"

Time:11-14

Create a function in python that replaces at least four different words or phrases with internet slang acronyms such as LOL, OMG, TBH. For example, if the user enters a sentence "Oh my god, I am scared to be honest." The output should be "OMG I am scared TBH". The program must not use any built-in find, replace, encode, index, or translate functions. The program can use indexing (i.e., [ ] ), slicing (i.e., :), the in operator, and the len() function.

This is what I have so far:

user_string = (input("Please enter a string: ")).lower()

punctuations = '''.,!@#$%^&*()[]{};:-'"\|<>/?_~'''

new_string = ""

list = []

for i in range(0, len(user_string)):
    if (user_string[i] not in punctuations):
        new_string = new_string   user_string[i]

print(new_string)

slang = "to be honest"

for i in range(0, len(slang)):
    for j in range(0, len(new_string)):
        if (new_string[j] == slang[i]):
            list.append(j)
            if (i < len(slang)):
                i = i   1
        
        elif (new_string[j] != slang[i]):
            if (len(list) > 0):
                list.pop()

print(list)

First I am getting the sentence from the user and removing all the punctuations from the sentence. Then I have created a variable called slang which holds the slang that I want to replace in the sentence with the acronym "TBH".

I have nested for loops which compare the string that the user has entered to the first letter of the slang variable. If the letters are the same, it compares the next letter of the string with the next letter of the slang.

I'm getting an error from the last part. How do I check if "to be honest" is in the string that the user has entered? And if it is in the string, how do I replace it with "TBH"?

CodePudding user response:

I cannot see any python errors that your code will actually produce, given the number of guard clauses, so I will assume what you mean by error is actually the program not working as you intended.

With that in mind, the main problem with your code is that you have nested for loops. This means that for any one character in slang, you check it against every character in new_string.

If you run through your code with this in mind, you will see that for every character in slang, you are attempting to add one value to the list and remove len(slang) - 1 values from it. Your clause, however, prevents this from causing an python error.

I would also like to mention that the statement

if (i < Len(slang)):
    i = i   1

is completely unnecessary because i is already automatically incremented by the for loop, which could cause issues later. It is guarded by a clause though, which is why it isn't a problem yet.

CodePudding user response:

If you're still stuck on this problem, here's my version on how to solve this exercise:

# This is a dictionary so we can automate the replacement on the `__main__` scope
targets = {'to be honest': 'TBH', 'oh my god': 'OMG'}

# Returns a list of intervals that tells where all occurences of the 
# `sequence` passed as parameter resides inside `source`.
#
# If `sequence` is not present, the list will be empty.
def findSequences(source, sequence):
    # This is our return value.
    intervals = []

    # len is O(1). But if you need to implement your own len function,
    # this might be handy to save for the linear complexity.
    srcLength = len(source)
    seqLength = len(sequence)

    # If the sequence is larger than source, it's not inside
    if (seqLength > srcLength):
        return intervals

    # If it's smaller or equal than source, it might be
    else:
        buffer = ''
        for i in range(srcLength):
            buffer = ''

            # From a starting character on index `i`, we will create
            # a temporary buffer with the length of sequence.
            for j in range(seqLength):
                # We must take care to not go out of the source string
                # otherwise, there's no point in continuing on building
                # buffer.
                if (i j >= srcLength):
                    break
                else:
                    buffer  = source[i j]

            # If this temporary buffer equals sequence, we found the
            # substring!
            if (buffer == sequence):

                # Return the interval of the substring
                intervals.append((i, i j))

        # Out of the for-loop.
        return intervals

# Takes out any characters inside `punctuation` from source.
#
# Uses the `in` keyword on the if-statement. But as the post says,
# it's allowed.
def takeOutPunctuation(source, punctuation='.,!@#$%^&*()[]{};:-\'"\\|<>/?_~'):
    buffer = ''
    for char in source:
        if (char not in punctuation):
            buffer  = char

    return buffer

# A naive approach would not to find all intervals, but to find the first 
# `phrase` occurence inside the `source` string, and replace it. If you do 
# that, it will get replacing "TBH" to "TBH2" infinitelly, always append "2" 
# to the string.
#
# This function is smart enough to avoid that.
#
# It replaces all occurences of the `phrase` string into a `target` string.
#
# As `findSequences` returns a list of all capture's intervals, the
# replacement will not get stuck in an infinite loop if we use
# parameters such as: myReplace(..., "TBH", "TBH2")
def myReplace(source, phrase, target):
    intervals = findSequences(source, phrase)

    if (len(intervals) == 0):
        return source
    else:
        # Append everything until the first capture
        buffer = source[:intervals[0][0]]

        # We insert this first interval just for writting less code inside the for-loop.
        #
        # This is not a capture, it's just so we can access (i-1) when the iteration
        # starts.
        intervals.insert(0, (0, intervals[0][0]))

        # Start a the second position of the `intervals` array so we can access (i-1)
        # at the start of the iteration.
        for i in range(1, len(intervals)):
            # For every `phrase` capture, we append:
            # - everything that comes before the capture
            # - the `target` string
            buffer  = source[intervals[i-1][1] 1:intervals[i][0]]   target

        # Once the iteration ends, we must append everything that comes later
        # after the last capture.
        buffer  = source[intervals[-1][1] 1:]

        # Return the modified string
        return buffer

if __name__ == '__main__':
    # Note: I didn't wrote input() here so we can see what the actual input is.
    user_string = 'Oh my god, I am scared to be honest and to be honest and to be honest!'.lower()
    user_string = takeOutPunctuation(user_string)

    # Automated Replacement
    for key in targets:
        user_string = myReplace(user_string, key, targets[key])

    # Print the output:
    print(user_string)
    # -> OMG i am scared TBH and TBH and TBH

Note: I used Python 3.10.2 to run this script.

  • Related