Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding differences between strings

Tags:

python

string

I have the following function that gets a source and a modified strings, and bolds the changed words in it.

def appendBoldChanges(s1, s2):
    "Adds <b></b> tags to words that are changed"
    l1 = s1.split(' ')
    l2 = s2.split(' ')
    for i, val in enumerate(l1):
        if l1[i].lower() != l2[i].lower():
            s2 = s2.replace(l2[i], "<b>%s</b>" % l2[i])

    return s2
print appendBoldChanges("britney spirs", "britney spears") # returns britney <b>spears</b>

It works fine on strings with the same word count, but fails with different word counts, as sora iro days and sorairo days.

How can I take the spaced into consideration?

like image 259
iTayb Avatar asked May 27 '12 15:05

iTayb


1 Answers

You could use difflib, and do it like this:

from difflib import Differ

def appendBoldChanges(s1, s2):
    "Adds <b></b> tags to words that are changed"
    l1 = s1.split(' ')
    l2 = s2.split(' ')
    dif = list(Differ().compare(l1, l2))
    return " ".join(['<b>'+i[2:]+'</b>' if i[:1] == '+' else i[2:] for i in dif 
                                                           if not i[:1] in '-?'])

print appendBoldChanges("britney spirs", "britney sprears")
print appendBoldChanges("sora iro days", "sorairo days")
#Output:
britney <b>sprears</b>
<b>sorairo</b> days
like image 75
fraxel Avatar answered Sep 20 '22 13:09

fraxel