4

Im using difflib and tried to compare the two sentence and get the difference.

Somewhat like this.

https://text-compare.com/

i have this code but instead of word by word it analyzed letter by letter.

import difflib

# define original text
# taken from: https://en.wikipedia.org/wiki/Internet_Information_Services
original = ["IIS 8.5 has several improvements related"]

# define modified text
edited = ["It has several improvements related"]

# initiate the Differ object
d = difflib.Differ()

# calculate the difference between the two texts
diff = d.compare(original, edited)

# output the result
print ('\n'.join(diff))
Mark Anthony Libres
  • 906
  • 1
  • 7
  • 14
  • 1
    Does this help? https://stackoverflow.com/questions/39001097/match-changes-by-words-not-by-characters – m4n0 Jul 29 '20 at 14:56

1 Answers1

11

If you remove the []'s from your strings, and call .split() on them in the .compare() perhaps you'll get what you want.

import difflib

# define original text
# taken from: https://en.wikipedia.org/wiki/Internet_Information_Services
original = "IIS 8.5 has several improvements related"

# define modified text
edited = "It has several improvements related"

# initiate the Differ object
d = difflib.Differ()

# calculate the difference between the two texts
diff = d.compare(original.split(), edited.split())

# output the result
print ('\n'.join(diff))

Output

+ It
- IIS
- 8.5
  has
  several
  improvements
  related
Chris
  • 15,819
  • 3
  • 24
  • 37