Python,使用 difflib 逐字比较两个句子

Python, compare two sentence by words using difflib

我使用 difflib 并尝试比较两个句子并找出差异。

有点像这样。

我有这段代码,但它不是逐字逐句地逐字分析。

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))

如果您从字符串中删除 []'s,并在 .compare() 中对它们调用 .split(),也许您会得到想要的结果。

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))

输出

+ It
- IIS
- 8.5
  has
  several
  improvements
  related