1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
Name
Text::WordDiff - Track changes between documents
Synopsis
use Text::WordDiff;
my $diff = word_diff 'file1.txt', 'file2.txt', { STYLE => 'HTML' };
my $diff = word_diff \$string1, \$string2, { STYLE => 'ANSIColor' };
my $diff = word_diff \*FH1, \*FH2; \%options;
my $diff = word_diff \&reader1, \&reader2;
my $diff = word_diff \@records1, \@records2;
# May also mix input types:
my $diff = word_diff \@records1, 'file_B.txt';
Description
This module is a variation on the lovely Text::Diff module. Rather than
generating traditional line-oriented diffs, however, it generates
word-oriented diffs. This can be useful for tracking changes in
narrative documents or documents with very long lines. To diff source
code, one is still best off using Text::Diff. But if you want to see how
a short story changed from one version to the next, this module will do
the job very nicely.
What is a Word?
I'm glad you asked! Well, sort of. It's a really hard question to
answer. I consulted a number of sources, but really just did my best to
punt on the question by reformulating it as, "How do I split text up
into individual words?" The short answer is to split on word boundaries.
However, every word has two boundaries, one at the beginning and one at
the end. So splitting on "/\b/" didn't work so well. What I really
wanted to do was to split on the *beginning* of every word. Fortunately,
_Mastering Regular Expressions_ has a recipe for that:
"/(?<!\w)(?=\w)/". I've borrowed this regular expression for use in
Perls before 5.6.x, but go for the Unicode variant in 5.6.0 and newer:
"/(?<!\p{IsWord})(?=\p{IsWord})/". With either of these regular
expressions, this sentence, for example, would be split up into the
following tokens:
my @words = (
'With ',
'either ',
'of ',
'these ',
'regular ',
"expressions,\n",
'this ',
'sentence, ',
'for ',
'example, ',
'would ',
'be ',
'split ',
'up ',
'into ',
'the ',
'following ',
'tokens:'
);
Note that this allows the tokens to include any spacing or punctuation
after each word. So it's not just comparing words, but word-like tokens.
This makes sense to me, at least, as the diff is between these tokens,
and thus leads to a nice word-and-space-and-punctation type diff. It's
not unlike what a word processor might do (although a lot of them are
character-based, but that seemed a bit extreme--feel free to dupe this
module into Text::CharDiff!).
Now, I acknowledge that there are localization issues with this
approach. In particular, it will fail with Chinese, Japanese, and Korean
text, as these languages don't put non-word characters between words.
Ideally, Test::WordDiff would then split on every charaters (since a
single character often equals a word), but such is not the case when the
"utf8" flag is set on a string. For example, This simple script:
use strict;
use utf8;
use Data::Dumper;
my $string = '뼈뼉뼘뼙뼛뼜뼝뽀뽁뽄뽈뽐뽑뽕뾔뾰뿅뿌뿍뿐뿔뿜뿟뿡쀼쁑쁘쁜쁠쁨쁩삐';
my @tokens = split /(?<!\p{IsWord})(?=\p{IsWord})/msx, $string;
print Dumper \@tokens;
Outputs:
$VAR1 = [
"\x{bf08}\x{bf09}\x{bf18}\x{bf19}\x{bf1b}\x{bf1c}\x{bf1d}\x{bf40}\x{bf41}\x{bf44}\x{bf48}\x{bf50}\x{bf51}\x{bf55}\x{bf94}\x{bfb0}\x{bfc5}\x{bfcc}\x{bfcd}\x{bfd0}\x{bfd4}\x{bfdc}\x{bfdf}\x{bfe1}\x{c03c}\x{c051}\x{c058}\x{c05c}\x{c060}\x{c068}\x{c069}\x{c090}"
];
Not so useful. It seems to be less of a problem if the "use utf8;" line
is commented out, in which case we get:
$VAR1 = [
'뼈',
'뼉',
'뼘',
'뼙',
'뼛',
'뼜',
'뼝',
'뽀',
'뽁',
'뽄',
'뽈',
'뽐',
'뽑',
'뽕',
'뾔',
'뾰',
'뿅',
'뿌',
'뿍',
'뿐',
'뿔',
'뿜',
'뿟',
'뿡',
'?',
'?쁑',
'쁘',
'쁜',
'쁠',
'쁨',
'쁩',
'삐'
];
Someone whose more familiar with non-space-using languages will have to
explain to me how I might be able to duplicate this pattern within the
scope of "use utf8;", seing as it may very well be important to have it
on in order to ensure proper character semantics.
However, if my word tokenization approach is just too naive, and you
decide that you need to take a different approach (maybe use
Lingua::ZH::Toke or similar module), you can still use this module;
you'll just have to tokenize your strings into words yourself, and pass
them to word_diff() as array references:
word_diff \@my_words1, \@my_words2;
Options
word_diff() takes two arguments from which to draw input and an optional
hash reference of options to control its output. The first two arguments
contain the data to be diffed, and each may be in the form of any of the
following (that is, they can be in two different formats):
* String
A bare scalar will be assumed to be a file name. The file will be
opened and split up into words. word_diff() will also "stat" the
file to get the last modified time for use in the header, unless the
relevant option ("MTIME_A" or "MTIME_B") has been specified
explicitly.
* Scalar Reference
A scalar reference will be assumed to refer to a string. That string
will be split up into words.
* Array Reference
An array reference will be assumed to be a list of words.
* File Handle
A glob or IO::Handle-derived object will be read from and split up
into its constituent words.
The optional hash reference may contain the following options.
Additional options may be specified by the formattting class; see the
specific class for details.
* STYLE
"ANSIColor", "HTML" or an object or class name for a class providing
"file_header()", "hunk_header()", "same_items()", "delete_items()",
"insert_items()", "hunk_footer()" and "file_footer()" methods.
Defaults to "ANSIColor" for nice display of diffs in an ANSI
Color-supporting terminal.
If the package indicated by the "STYLE" has no "new()" method,
"word_diff()" will load it automatically (lazy loading). It will
then instantiate an object of that class, passing in the options
hash reference with which the formatting class can initialize the
object.
Styles may be specified as class names ("STYLE => "My::Foo""), in
which case they will be instantiated by calling the "new()"
construcctor and passing in the options hash reference, or as
objects ("STYLE => My::Foo->new").
The simplest way to implement your own formatting style is to create
a new class that inherits from Text::WordDiff::Base, wherein the
"new()" method is already provided, and the "file_header()" returns
a Unified diff-style header. All of the other formatting methods
simply return empty strings, and are therefore ripe for overriding.
* FILENAME_A, MTIME_A, FILENAME_B, MTIME_B
The name of the file and the modification time "files" in epoch
seconds. Unless a defined value is specified for these options, they
will be filled in for each file when word_diff() is passed a
filename. If a filename is not passed in and "FILENAME_A" and
"FILENAME_B" are not defined, the header will not be printed by the
base formatting base class.
* OUTPUT
The method by which diff output should be, well, *output*. Examples
and their equivalent subroutines:
OUTPUT => \*FOOHANDLE, # like: sub { print FOOHANDLE shift() }
OUTPUT => \$output, # like: sub { $output .= shift }
OUTPUT => \@output, # like: sub { push @output, shift }
OUTPUT => sub { $output .= shift },
If "OUTPUT" is not defined, word_diff() will simply return the diff
as a string. If "OUTPUT" is a code reference, it will be called once
with the file header, once for each hunk body, and once for each
piece of content. If "OUTPUT" is an IO::Handle-derived object,
output will be sent to that handle.
* FILENAME_PREFIX_A, FILENAME_PREFIX_B
The string to print before the filename in the header. Defaults are
"---", "+++".
* DIFF_OPTS
A hash reference to be passed as the options to
"Algorithm::Diff->new". See Algorithm::Diff for details on available
options.
Formatting Classes
Text::WordDiff comes with two formatting classes:
Text::WordDiff::ANSIColor
This is the default formatting class. It emits a header and then the
diff content, with deleted text in bodfaced red and inserted text in
boldfaced green.
Text::WordDiff::HTML
Specify "STYLE => 'HTML'" to take advantage of this formatting
class. It outputs the diff content as XHTML, with deleted text in
"<del>" elements and inserted text in "<ins>" elements.
To implement your own formatting class, simply inherit from
Text::WordDiff::Base and override its methods as necssary. By default,
only the "file_header()" formatting method returns a value. All others
simply return empty strings, and are therefore ripe for overriding:
package My::WordDiff::Format;
use base 'Text::WordDiff::Base';
sub file_footer { return "End of diff\n"; }
The methods supplied by the base class are:
"new()"
Constructs and returns a new formatting object. It takes a single
hash reference as its argument, and uses it to construct the object.
The nice thing about this is that if you want to support other
options in your formatting class, you can just use them in the
formatting object constructed by the Text::WordDiff::Base class and
document that they can be passed as part of the options hash
refernce to word_diff().
"file_header()"
Called once for a single call to "word_diff()", this method outputs
the header for the whole diff. This is the only formatting method in
the base class that returns anything other than an empty string. It
collects the filenames from "filname_a()" and "filename_b()" and, if
they're defined, uses the relevant prefixes and modification times
to return a unified diff-style header.
"hunk_header()"
This method is called for each diff hunk. It should output any
necessary header for the hunk.
"same_items()"
This method is called for items that have not changed between the
two sequnces being compared. The unchanged items will be passed as a
list to the method.
"delete_items"
This method is called for items in the first sequence that are not
present in the second sequcne. The deleted items will be passed as a
list to the method.
"insert_items"
This method is called for items in the second sequence that are not
present in the first sequcne. The inserted items will be passed as a
list to the method.
"hunk_footer"
This method is called at the end of a hunk. It should output any
necessary content to close out the hunk.
"file_footer()"
This method is called once when the whole diff has been procssed. It
should output any necessary content to close out the diff file.
"filename_a"
This accessor returns the value specified for the "FILENAME_A"
option to word_diff().
"filename_b"
This accessor returns the value specified for the "FILENAME_B"
option to word_diff().
"mtime_a"
This accessor returns the value specified for the "MTIME_A" option
to word_diff().
"mtime_b"
This accessor returns the value specified for the "MTIME_B" option
to word_diff().
"filename_prefix_a"
This accessor returns the value specified for the
"FILENAME_PREFIX_A" option to word_diff().
"filename_prefix_b"
This accessor returns the value specified for the
"FILENAME_PREFIX_B" option to word_diff().
See Also
Text::Diff
Inspired the interface and implementation of this module. Thanks
Barry!
Text::ParagraphDiff
A module that attempts to diff paragraphs and the words in them.
Algorithm::Diff
The module that makes this all possible.
Support
This module is stored in an open repository at the following address:
<https://svn.kineticode.com/Text-WordDiff/trunk/>
Patches against Text::WordDiff are welcome. Please send bug reports to
<bug-text-worddiff@rt.cpan.org>.
Author
David Wheeler <david@kineticode.com>
Copyright and License
Copyright (c) 2005-2008 David Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
|