Replacing string with words from a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Qbert16
    New Member
    • Mar 2008
    • 5

    Replacing string with words from a list

    I'm having a lot of trouble with the following task for one of my assignments in python. Basically I need to take the user input and replace any words in the input and replace them with words from a list if they are in it. Here's an example

    list_of_words = \
    [
    [['Hi', "G'day", 'Hey'], 'Hello'],
    [['Bye', 'Farewell', 'Later', 'Ciao', 'Quit'], 'Goodbye'],
    [['Nope', 'Nah'], 'No']
    ]

    So if the user types 'Hi, how are you', they should get in return 'Hello how are you'

    The example list that I need to work with is the same layout, but much larger, so I didn't think it would be necessary to post it all here.

    I think I know how to approach this, but keep messing up. I tried to use a for loop to say for words that the user types, if they are in the list_of_words, replace them with the line they are on and the second part of the list. I have split up the users input into a list, so the loop should just iterate through each item in that list I'd imagine. We've been told to not use .replace() by the way.

    I've tried to use a counter to make the loop go to a different line in the list each time, but don't think that's the way to go. I think I need to do a loop inside a loop, but I get confused. Any pointers to show me in the right direction would be appreciated.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Qbert16
    I'm having a lot of trouble with the following task for one of my assignments in python. Basically I need to take the user input and replace any words in the input and replace them with words from a list if they are in it. Here's an example

    list_of_words = \
    [
    [['Hi', "G'day", 'Hey'], 'Hello'],
    [['Bye', 'Farewell', 'Later', 'Ciao', 'Quit'], 'Goodbye'],
    [['Nope', 'Nah'], 'No']
    ]

    So if the user types 'Hi, how are you', they should get in return 'Hello how are you'

    The example list that I need to work with is the same layout, but much larger, so I didn't think it would be necessary to post it all here.

    I think I know how to approach this, but keep messing up. I tried to use a for loop to say for words that the user types, if they are in the list_of_words, replace them with the line they are on and the second part of the list. I have split up the users input into a list, so the loop should just iterate through each item in that list I'd imagine. We've been told to not use .replace() by the way.

    I've tried to use a counter to make the loop go to a different line in the list each time, but don't think that's the way to go. I think I need to do a loop inside a loop, but I get confused. Any pointers to show me in the right direction would be appreciated.
    You will need two loops. One to check each word in the sentence entered, and another to look at each element in list_of_words. You will also need to strip punctuation from each word, and capitalize the entered words.

    Split the entered phrase with string method split().
    Use built-in function enumerate() to iterate on each word in the split phrase.
    Iterate on each element in list_of_words.[code=Python]
    if word.strip(stri ng.punctuation) .capitalize() in elem[0]:[/code]If the word is found, assign elem[1] to the corresponding position in the split phrase.
    Use string method join() to rejoin the modified split phrase.
    HTH
    -BV

    Comment

    • micmast
      New Member
      • Mar 2008
      • 144

      #3
      If you cannot use replace I would indeed split the string the user inputs, and then loop it
      [CODE=python]
      sentence = input.split(' ')
      output = ''
      for word in sentence:
      for entry in list_of_words:
      # the first line with possible words
      words_to_replac e = entry[0]
      word_to_change_ to = entry[1]
      for word_to_replace in words_to_replac e:
      # Every word seperately
      if word == word_to_replace :
      # so the word is the same, so we change it
      output = output + ' ' + word_to_change_ to
      else:
      # we don't need to change this word.
      output = output + ' ' + word

      print output
      # the sentence should have changed :)

      [/CODE]

      the code is probably not 100% correct but it will give you an idea

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        It would really help us here if you posted the code that you tried along with the errors that you have recieved. Then we will be able to help you find and fix your errors.

        Comment

        • Qbert16
          New Member
          • Mar 2008
          • 5

          #5
          Thanks a lot for the replys, they really helped. Here's my updated code now..

          Code:
           
          list_of_words = \
          [
          [['Hi', "G'day", 'Hey'], 'Hello'],
          [['Bye', 'Farewell', 'Later', 'Ciao', 'Quit'], 'Goodbye'],
          [['Nope', 'Nah'], 'No']
          ]
          punctuation_marks = ['?', ',', '!', '.']
          
          def standardise_phrase(enter_phrase):
          
              
              for punctuation in punctuation_marks:
                  if punctuation in enter_phrase:
                      enter_phrase = enter_phrase.replace(punctuation, '')
              split_user_input = enter_phrase.split()
          
          
              
              output = ''
              for word in split_user_input:
          
                  for entry in list_of_words:
                      
                      words_to_replace = entry[0]
                      word_to_change_to = entry[1]
                      
                      
                      for word_to_replace in words_to_replace:
                          if word == word_to_replace:  
                              output = output + word_to_change_to + ' '
          
                  if word not in list_of_words:
                      output = output + word + ' '
          
                  return output
          The code works, however it either gives not enough or too many words back depending on weather I have the last 2 lines of code in.

          Eg print standardise_phr ase('Hi said Jane. Bye said Tom')
          will come out as either;
          Hello Hi said Jane Goodbye Bye said Tom
          or
          Hello Goodbye

          I've tried some ways of taking out the unwanted words (Hi and Bye in the example), but as you can see in my code, the 3rd last line " if word not in list_of_words:" doesn't work because it can't check the whole list in 1 go I think.

          I don't know if it helps, but other code I've tried instead of the 3rd and 2nd last lines that has not worked is..

          Code:
                  for excess_word in split_user_input:
                      if excess_word not in output:
                          if excess_word not in words_to_replace:
                             output = output + excess_word + ' '
          Thanks again.

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            Ok you're problem (as you suspected) lies in the nested for loop where you say "if word not in list_of_words". One thing you could do is place a boolean variable to say whether there was a hit inside the loop checking for replaceable words. This would be initialized to 0 (or False) right after the "for word in user_input". After the word replacing loop, change your "not in" if check to simply check for that variable to be false, in which case you would keep the same thing that's there already...

            Oh, also move your return output statement back one indentation (it's returning inside the for loop!)

            [code=python]
            for word in split_user_inpu t:
            rplcd = 0
            for entry in list_of_words:

            words_to_replac e = entry[0]
            word_to_change_ to = entry[1]


            for word_to_replace in words_to_replac e:
            if word == word_to_replace :
            rplcd = 1
            output = output + word_to_change_ to + ' '

            if not rplcd:
            output = output + word + ' '

            return output
            [/code]

            HTH!

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Following is an example solution that I worked up.[code=Python]import string

              list_of_words = \
              [
              [['Hi', "G'day", 'Hey'], 'Hello'],
              [['Bye', 'Farewell', 'Later', 'Ciao', 'Quit'], 'Goodbye'],
              [['Nope', 'Nah'], 'No']
              ]

              sentence = raw_input("Ente r sentence")
              sentenceList = sentence.split( )

              for i, word in enumerate(sente nceList):
              for elem in list_of_words:
              if word.strip(stri ng.punctuation) .capitalize() in elem[0]:
              sentenceList[i] = elem[1]

              print ' '.join(sentence List)[/code]HTH
              -BV

              Comment

              • Qbert16
                New Member
                • Mar 2008
                • 5

                #8
                Thanks a bunch jlm699 and bvdet, that got the duplicates not entered into the list.

                Thanks!

                Comment

                Working...