Search code examples
pythonregexnlpnltkpython-re

Re:sub expected string or byte-like object error


class REReplacer(object):
   def __init__(self, pattern = R_patterns):
      self.pattern = [(re.compile(regex), repl) for (regex, repl) in patterns]

   def replace(self, text):
      s = text
      for (pattern, repl) in self.pattern:
         s = re.sub(pattern, repl, s)
      return s

I have got this code which replaces certain words with their replacements. When I call the method replace of class REplacer,

rep=REplacer()
rep.replace("I like oranges") 

it works perfectly fine with strings but gives an error with list or nested lists.

Error- (re.sub) expected string or bytes-like object.

Is there a way (except for converting the list to string) in order to make the function work of list of sentences? Thanks in advance. Seems like predefined re.sub takes string as argument. Should I split the words of the list?


Solution

  • If text is a list (or some other iterable type), loop over it and perform the replacements, and return a list of results.

    from collections.abc import Iterable
    
    def replace(self, text):
        if isinstance(text, string):
            for (pattern, repl) in self.pattern:
                text = re.sub(pattern, repl, text)
            return text
        elif isinstance(text, Iterable):
            return [self.replace(i) for i in text]
        else:
            raise ValueError('text must be a string or iterable collection')