{ $values { "seq1" sequence } { "seq2" sequence } { "lcs" "a longest common subsequence" } }\r
{ $description "Given two sequences, calculates a longest common subsequence between them. Note two things: this is only one of the many possible LCSs, and the LCS may not be contiguous." } ;\r
\r
-HELP: diff\r
+HELP: lcs-diff\r
{ $values { "old" sequence } { "new" sequence } { "diff" "an edit script" } }\r
{ $description "Given two sequences, find a minimal edit script from the old to the new. There may be more than one minimal edit script, and this chooses one arbitrarily. This script is in the form of an array of the tuples of the classes " { $link retain } ", " { $link delete } " and " { $link insert } " which have their information stored in the 'item' slot." } ;\r
\r
"This vocabulary provides words for three apparently unrelated but in fact very similar problems: finding a longest common subsequence between two sequences, getting a minimal edit script (diff) between two sequences, and calculating the Levenshtein distance between two sequences. The implementations of these algorithms are very closely related, and all running times are O(nm), where n and m are the lengths of the input sequences."\r
{ $subsections\r
lcs\r
- diff\r
+ lcs-diff\r
levenshtein\r
}\r
-"The " { $link diff } " word returns a sequence of tuples of the following classes. They all hold their contents in the 'item' slot."\r
+"The " { $link lcs-diff } " word returns a sequence of tuples of the following classes. They all hold their contents in the 'item' slot."\r
{ $subsections\r
insert\r
delete\r
\r
PRIVATE>\r
\r
-: diff ( old new -- diff )\r
+: lcs-diff ( old new -- diff )\r
2dup [ lcs-initialize ] [ lcs-step ] run-lcs trace-diff ;\r
\r
: lcs ( seq1 seq2 -- lcs )\r
- [ diff [ retain? ] filter ] keep [ item>> ] swap map-as ;\r
+ [ lcs-diff [ retain? ] filter ] keep [ item>> ] swap map-as ;\r