0

There are a lot of threads on how to check the difference in characters between two strings using difflib, but I specifically want to know if there is a way or a module that can tell me the words deleted and added between two strings.

For example, if we have

foo = 'This is a sentence'
bar = 'I am a sentence'

I would like to know if there's a function along the lines of

deleted_words(foo, bar)

that returns a list containing "This" and "is".

Is there a module which has this functionality, or do I need to make an algorithm to do this myself?

1
  • you have not done a great job of explaining the requirements ... however it sounds like you will probably have to develope your own solution Commented Nov 28, 2017 at 1:03

1 Answer 1

1

If you require an API like the one you describe, that returns exactly what you describe, then you may need to write something yourself.

There are, however, modules that are designed to diff two text bodies, for example: diff-match-patch.

One simple example is:

diff_main("Good dog", "Bad dog") => [(-1, "Goo"), (1, "Ba"), (0, "d dog")]

And a direct link to Google Code's wiki on the topic: https://code.google.com/archive/p/google-diff-match-patch/wikis/API.wiki

There a more extensive explanation of this library on this SO itself: https://stackoverflow.com/a/16086633/3084820

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.