I'm trying to develop a way of comparing two sequences (probably originally text, such as text writing or source code, possibly converted to lists). As a familiar example, consider the revisions display for any typical SE question or the diff output from a command-line diff
.
I found a cool Mathematica function called SequenceAlignment
which looks promising:
text1 = ExampleData[{"Text", "ToBeOrNotToBe"}];
text2 = StringReplace[text1, {"s" -> "th"}]
(*
To be, or not to be,--that ith the quethtion:-- Whether 'tith nobler
in the mind to thuffer The thlingth and arrowth of outrageouth
fortune Or to take armth againtht a thea of troubleth, And by
oppothing end them? ...
*)
(lisp programming... :)
Now:
sa = SequenceAlignment[text1, text2]
gives:
{"To be, or not to be,--that i", {"s", "th"}, " the que", {"s", "th"},
"tion:-- Whether 'ti", {"s", "th"}, " nobler in the mind to ", {"s", "th"},
"uffer " ...
which I want to convert to some kind of colored display. The best I've managed so far is this:
Reap[
If[Length[#] == 2,
Sow[Column[{Style[#[[1]], Red], Style[#[[2]], Green]}]],
Sow[Style[#, Black]]]
& /@
sa]
but it's not a pretty display:
How can I make this display look like a single piece of text with colored markup, like the SE revisions display? And would it be possible to process Mathematica code as well - without evaluating said code first, obviously?
sa
to a text display. And I don't know whetherSequenceAlignment
is the right tool for the job... $\endgroup$text2 = StringReplace[text1, {"s" -> "ss"}]
and see how your output looks like. $\endgroup$