Home > Net >  How do I use text replace with plural/singular strings
How do I use text replace with plural/singular strings

Time:11-02

I'm trying to replace words from strings using text.replace(). It works well till the replace words with plurals are used as follows:

def replacing():
    texter = []
    del texter[:]
    repl = ['diabetes', 'mellitus', 'dm', ]
    it = ''
    try:
        it = iter(np.array(repl))
    except:
        pass
    txt = "tell me if its can also cause coronavirus"

    for i in range(len(np.array(repl1))):
        try:
            p = it.__next__()
            x = txt.replace("its", p)
            texter.append(x)
            x = txt.replace("it", p)
            texter.append(x)
            xxx = txt.replace("them", p)
            texter.append(xxx)
            xxxx = txt.replace("the same", p)
            texter.append(xxx)
            xxxxx = txt.replace("this", p)
            texter.append(xxx)
        except StopIteration:
            break
    mm = list(OrderedDict.fromkeys(texter))
    print (mm)

replacing()

This is the result of this code:

['tell me if diabetes can also cause coronavirus', 'tell me if diabetess can also cause coronavirus', 'tell me if mellitus can also cause coronavirus', 'tell me if mellituss can also cause coronavirus', 'tell me if dm can also cause coronavirus', 'tell me if dms can also cause coronavirus']

Notice the misspell replaced words as 'diabetess' instead of 'diabetes', 'mellituss' instead of mellitus and 'dms' instead of 'dm'.

I noted the keywords 'it and its', since are similar end up bringing the errors.

How can I avoid this

CodePudding user response:

The issue is that you are replacing "it" and "its" separately. txt.replace("it", p) creates a copy of txt with "it" replaced by p, so "its" becomes "diabetess". Use the re module to specify that you want to replace "it" or "its". Your for loop would look like this:

for i in range(len(np.array(repl))):
        try:
            p = it.__next__()
            x = re.sub("its|it", p, txt)
            texter.append(x)
            xxx = txt.replace("them", p)
            texter.append(xxx)
            xxxx = txt.replace("the same", p)
            texter.append(xxx)
            xxxxx = txt.replace("this", p)
            texter.append(xxx)
        except StopIteration:
            break
  • Related