Text::WordDiff(3pm) User Contributed Perl Documentation Text::WordDiff(3pm)

Text::WordDiff - Track changes between documents

    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';

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.

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})/". Adding some additional controls for punctuation and control characters, this sentence, for example, would be split up into the following tokens:

  my @words = (
      "Adding ",
      "some ",
      "additional ",
      "controls",
      "\n",
      "for ",
      "punctuation ",
      "and ",
      "control ",
      "characters",
      ", ",
      "this ",
      "sentence",
      ", ",
      "for ",
      "example",
      ", ",
      "would ",
      "be",
      "\n",
      "split ",
      "up ",
      "into ",
      "the ",
      "following ",
      "tokens",
      ":",
  );

So it's not just comparing words, but word-like tokens and control/punctuation 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-punctuation 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 character (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 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  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 = [
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X',
            '?',
            '?X',
            'X',
            'X',
            'X',
            'X',
            'X',
            'X'
          ];

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;

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

The optional hash reference may contain the following options. Additional options may be specified by the formattting class; see the specific class for details.

Text::WordDiff comes with two formatting classes:

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

Inspired the interface and implementation of this module. Thanks Barry!
A module that attempts to diff paragraphs and the words in them.
The module that makes this all possible.

This module is stored in an open GitHub repository <http://github.com/theory/text-worddiff/>. Feel free to fork and contribute!

Please file bug reports via GitHub Issues <http://github.com/theory/text-worddiff/issues/> or by sending mail to bug-Text-WordDiff@rt.cpan.org <mailto:bug-Text-WordDiff@rt.cpan.org>.

David E. Wheeler <david@justatheory.com>

Currently maintained by the developers of The Perl Shop <tps@cpan.org>.

Copyright (c) 2005-2011 David E. Wheeler. Some Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2022-12-12 perl v5.36.0