| 1 | #!/usr/bin/perl -I/home/phil/r/salesForce/perl/ -I/home/phil/perl/cpan/DataEditXml/lib -I/home/phil/perl/cpan/DataTableText/lib -I/home/phil/perl/cpan/DitaGBStandard/lib -I/home/phil/perl/cpan/DataEditXmlToDita/lib -I/home/phil/perl/cpan/DataEditXmlXref/lib -I/home/phil/perl/cpan/DataEditXmlLint/lib/ -I/home/phil/perl/cpan/GitHubCrud/lib/ -I/home/phil/perl/cpan/DataEditXml/lib/ -I/home/phil/perl/cpan/DitaPCD/lib/ -I/home/phil/perl/cpan/FlipFlop/lib/ -I/home/phil/perl/cpan/DataEditXmlReuse/lib/ | ||
| 2 | #------------------------------------------------------------------------------- | ||
| 3 | # Reuse Xml via Dita conrefs. | ||
| 4 | # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc, 2019 | ||
| 5 | #------------------------------------------------------------------------------- | ||
| 6 | # podDocumentation | ||
| 7 | |||
| 8 | package Data::Edit::Xml::Reuse; | ||
| 9 | our $VERSION = 20200503; | ||
| 10 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use warnings FATAL => qw(all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Carp qw(confess cluck); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Data::Dump qw(dump); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use Data::Edit::Xml; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use Data::Edit::Xml::Xref; | ||
| 17 | use Data::Table::Text qw(:all); | ||
| 18 | use Dita::GB::Standard; | ||
| 19 | use Storable qw(store retrieve); | ||
| 20 | use Time::HiRes qw(time); | ||
| 21 | use utf8; | ||
| 22 | |||
| 23 | #D1 Reuse Xml # Reuse Xml via Dita conrefs. | ||
| 24 | |||
| 25 | sub newReuse(%) #P Create a new cross reuser. | ||
| 26 | {my (%attributes) = @_; # Attributes | ||
| 27 | |||
| 28 | my $reuse = genHash(__PACKAGE__, # Attributes used by the reuser. | ||
| 29 | dictionary => undef, #I The dictionary file into which to store the duplicate L<Xml>. | ||
| 30 | fileExtensions => [qw(.dita .ditamap .xml .fodt)], #I The extensions of the L<Xml> files to examine in the L<inputFolder>. | ||
| 31 | getFileUrl => qq(/cgi-bin/uiSelfServiceXref/client.pl?getFile=), #I An optional url to retrieve a specified file from the server running xref used in generating html reports. The complete url is obtained by appending the fully qualified file name to this value. | ||
| 32 | htmlFolder => undef, #I Folder into which to write reports as html. | ||
| 33 | inputFiles => [], # The files selected from L<inputFolder> for analysis because their extensions matched L<fileExtensions>. | ||
| 34 | inputFolder => undef, #I A folder containing the L<xml> files with extensions named in L<fileExtensions> to be analyzed for reuse. | ||
| 35 | matchBlocks => [], # [[md5, content]*] blocks of content that match with the confidence level expressed by L<matchSimilarContent> | ||
| 36 | matchInBlock => {}, # {md5 => matchBlocks} : index into L<matchBlocks> by md5 sum. | ||
| 37 | matchSimilarTagContent => 0.5, #I Confidence level between 0 and 1: match content under L<tags> with this level of confidence. | ||
| 38 | maximumColumnWidth => 180, #I Truncate columns in text reports to this length or allow any width if B<undef>. | ||
| 39 | minimumLength => 16, #I The minimum length content must have to be considered for matching. | ||
| 40 | minimumReferences => 2, #I The minimum number of references content must have before it can be reused. | ||
| 41 | outputFolder => undef, #I A folder into which to write the deduplicated L<Xml>. | ||
| 42 | reportsFolder => undef, #I A folder into which reports will be written. | ||
| 43 | reusableContent => {}, # {tag}{md5sum}{content}++ potentially reusable content. | ||
| 44 | tags => {p=>1}, #I {tag=>1} only consider tags that appear as keys in this hash with truthful values. | ||
| 45 | timeEnded => undef, # Time the run ended. | ||
| 46 | timeStart => undef, # Time the run started. | ||
| 47 | ); | ||
| 48 | |||
| 49 | loadHash($reuse, @_); # Load attributes complaining about any invalid ones | ||
| 50 | } # newReuse | ||
| 51 | |||
| 52 | sub reuse(%) # Check Xml for reuse opportunities. | ||
| 53 | {my (%attributes) = @_; # Reuse attributes | ||
| 54 | my $reuse = newReuse(%attributes); # Create the reuser | ||
| 55 | |||
| 56 | $reuse->timeStart = time; # Start time | ||
| 57 | |||
| 58 | for my $attr(qw(inputFolder outputFolder reportsFolder)) | ||
| 59 | {$reuse->{$attr} or confess "Please supply a value for: $attr"; | ||
| 60 | } | ||
| 61 | |||
| 62 | $reuse->dictionary //= fpe($reuse->outputFolder, qw(dictionary xml)); # Default dictionary | ||
| 63 | |||
| 64 | lll "Reuse started on input folder:", $reuse->inputFolder; # Title | ||
| 65 | |||
| 66 | my @series = # Must be done in series at the start | ||
| 67 | (q(loadInputFiles), | ||
| 68 | q(analyzeInputFiles), | ||
| 69 | q(conRef), | ||
| 70 | ); | ||
| 71 | |||
| 72 | if (1) # Perform phases in series that must be run in series | ||
| 73 | {my @times; | ||
| 74 | |||
| 75 | for my $phase(@series) # Each phase in series | ||
| 76 | {my $startTime = time; | ||
| 77 | lll "Reuse phase $phase"; | ||
| 78 | |||
| 79 | $reuse->$phase; # Execute phase | ||
| 80 | |||
| 81 | push @times, [$phase, sprintf("%12.4f", time - $startTime)]; # Phase time | ||
| 82 | } | ||
| 83 | |||
| 84 | my $delta = sprintf("%.3f seconds", time - $reuse->timeStart); # Time so far | ||
| 85 | |||
| 86 | formatTables($reuse, [sort {$$b[1] <=> $$a[1]} @times], # Update after each phase so we can see progress on long running jobs | ||
| 87 | columns => <<END, | ||
| 88 | Phase Reuse processing phase | ||
| 89 | Time_Seconds Time in seconds taken by this processing phase | ||
| 90 | END | ||
| 91 | title => qq(Processing phases elapsed times in descending order), | ||
| 92 | head => <<END, | ||
| 93 | Reuse phases took the following times on DDDD | ||
| 94 | |||
| 95 | Total run time: $delta | ||
| 96 | END | ||
| 97 | file => fpe(q(timing), qw(reuse_phases txt))); # Write phase times | ||
| 98 | } | ||
| 99 | |||
| 100 | $reuse->timeEnded = time; # Run ended time | ||
| 101 | |||
| 102 | formatTables($reuse, [[$reuse->timeStart, $reuse->timeEnded, # Write run times | ||
| 103 | $reuse->timeEnded - $reuse->timeStart]], | ||
| 104 | columns => <<END, | ||
| 105 | Start_Time Start time of the run | ||
| 106 | End_Time End time of the run | ||
| 107 | Elapsed_Time Reuse took this many seconds to run | ||
| 108 | END | ||
| 109 | title => qq(Run times in seconds), | ||
| 110 | head => qq(Reuse took the following time to run on DDDD), | ||
| 111 | file => fpe(q(timing), qw(run txt))); | ||
| 112 | |||
| 113 | lll "Reuse finished on folder:", $reuse->inputFolder unless # Show that we have finished unless in development | ||
| 114 | $reuse->inputFolder =~ m(cpan/DataEditXmlReuse); | ||
| 115 | |||
| 116 | $reuse # Return Reuse results | ||
| 117 | } | ||
| 118 | |||
| 119 | sub formatTables($$%) #P Format reports. | ||
| 120 | {my ($reuse, $data, %options) = @_; # Reuser, table to be formatted, options | ||
| 121 | |||
| 122 | $reuse && ref($reuse) =~ m(reuse)i or cluck "No reuser"; # Check parameters | ||
| 123 | $data && ref($data) =~ m(array)i or cluck "No data for table"; | ||
| 124 | |||
| 125 | cluck "No file for table" unless $options{file}; # Check for required options | ||
| 126 | cluck "No columns for table" unless $options{columns}; | ||
| 127 | cluck "No title for table" unless $options{title}; | ||
| 128 | |||
| 129 | formatHtmlAndTextTables | ||
| 130 | ($reuse->reportsFolder, $reuse->htmlFolder, $reuse->getFileUrl, | ||
| 131 | $reuse->inputFolder, $data, %options, | ||
| 132 | maximumColumnWidth => $reuse->maximumColumnWidth, | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub loadInputFiles($) #P Load the names of the files to be processed. | ||
| 137 | {my ($reuse) = @_; # Cross referencer | ||
| 138 | my @in = searchDirectoryTreesForMatchingFiles # Input files | ||
| 139 | $reuse->inputFolder, $reuse->fileExtensions->@*; | ||
| 140 | |||
| 141 | $reuse->inputFiles = [@in]; | ||
| 142 | |||
| 143 | if (@in == 0) # Complain if there are no input files to analyze | ||
| 144 | {my $i = $reuse->inputFolder; | ||
| 145 | my $e = join " ", @{$reuse->fileExtensions}; | ||
| 146 | my $x = -d $i ? "The input folder does exist." : | ||
| 147 | "The input folder does NOT exist!"; | ||
| 148 | confess join '', | ||
| 149 | "No files with the specified file extensions ", | ||
| 150 | "in the specified input folder:\n", | ||
| 151 | "$e\n$i\n$x\n"; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | sub ffc($$) #P First few characters of a string with white space normalized. | ||
| 156 | {my ($reuse, $string) = @_; # Reuser, String | ||
| 157 | firstNChars(nws($string), $reuse->maximumColumnWidth) | ||
| 158 | } | ||
| 159 | |||
| 160 | sub reuseParams($) #P Tabulate reuse parameters. | ||
| 161 | {my ($reuse) = @_; # Reuser | ||
| 162 | |||
| 163 | push my @t, | ||
| 164 | [q(MinimumLength:), $reuse->minimumLength, q(The minimum size for content to be reused)], | ||
| 165 | [q(MinimumReferences:), $reuse->minimumReferences, q(The minimum number of references for content to be reused)]; | ||
| 166 | |||
| 167 | formatTableBasic(\@t); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub analyzeOneFile($$) #P Analyze one input file. | ||
| 171 | {my ($reuse, $file) = @_; # Reuser, File to analyze | ||
| 172 | |||
| 173 | my $x = Data::Edit::Xml::new($file); # Parse xml | ||
| 174 | |||
| 175 | $x->subMd5($reuse->tags->%*); # Md5 sum for each sub tree in parse tree | ||
| 176 | } # analyzeOneFile | ||
| 177 | |||
| 178 | sub analyzeInputFiles($) #P Analyze the input files. | ||
| 179 | {my ($reuse) = @_; # Reuser | ||
| 180 | lll "Reuse: analyze input files"; | ||
| 181 | my $ml = $reuse->minimumLength; | ||
| 182 | my $mr = $reuse->minimumReferences; | ||
| 183 | |||
| 184 | processFilesInParallel # Md5 sums for each parse tree | ||
| 185 | sub | ||
| 186 | {analyzeOneFile $reuse, $_[0]; | ||
| 187 | }, | ||
| 188 | sub # Merge results for all parse trees | ||
| 189 | {my @results = @_; | ||
| 190 | mmm "Reuse: merge results"; | ||
| 191 | |||
| 192 | my %r; # Reuse | ||
| 193 | |||
| 194 | for my $h(@results) # Merge | ||
| 195 | {for my $t(sort keys $h->%*) # Tag | ||
| 196 | {for my $m(sort keys $h->{$t}->%*) # Md5 | ||
| 197 | {for my $s(sort keys $h->{$t}->{$m}->%*) # String | ||
| 198 | {if (length($s) >= $ml) | ||
| 199 | {$r{$t}{$m}{$s} += $$h{$t}{$m}{$s}; # Count of reuses | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | $reuse->reusableContent = \%r; | ||
| 207 | |||
| 208 | mmm "Reuse: create dictionary"; | ||
| 209 | my @t; my @report; # Create a generic concept to conref from | ||
| 210 | for my $t(sort keys %r) | ||
| 211 | {my @r; | ||
| 212 | for my $m(sort keys $r{$t}->%*) | ||
| 213 | {for my $s(sort keys $r{$t}{$m}->%*) | ||
| 214 | {if ((my $c = $r{$t}{$m}{$s}) >= $mr) # Reuse if there are enough references | ||
| 215 | {my $g = guidFromMd5($m); | ||
| 216 | push @r, [$s, qq(<$t id="$g">$s</$t>)]; | ||
| 217 | push @report, [$c, $t, length($s), ffc($reuse, $s), guidFromMd5 $m]; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | push @t, map {$$_[1]} sort {$$a[0] cmp $$b[0]} @r; | ||
| 222 | } | ||
| 223 | |||
| 224 | my $t = join "\n", <<END, @t, <<END2; | ||
| 225 | <concept id="dictionary"> | ||
| 226 | <title>Dictionary</title> | ||
| 227 | <conbody> | ||
| 228 | END | ||
| 229 | </conbody> | ||
| 230 | </concept> | ||
| 231 | END2 | ||
| 232 | my %seen; # List of elements already seen | ||
| 233 | my $x = Data::Edit::Xml::new($t); | ||
| 234 | |||
| 235 | $x->by(sub # Reuse text within the dictionary | ||
| 236 | {my ($o) = @_; | ||
| 237 | if (my ($m, $s) = $o->subMd5Tree) | ||
| 238 | {if (length($s) >= $ml) | ||
| 239 | {my $t = -t $o; | ||
| 240 | if ($r{$t}{$m}) | ||
| 241 | {if ($seen{$m}++) | ||
| 242 | {$o->set(id=>undef, conref=>q(#dictionary/).guidFromMd5($m)); | ||
| 243 | $o->deleteContent; | ||
| 244 | $o->putNextAsComment($s); | ||
| 245 | } | ||
| 246 | else | ||
| 247 | {$o->id = guidFromMd5($m); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | }); | ||
| 253 | |||
| 254 | overWriteFile($reuse->dictionary, -p $x); # Create dictionary | ||
| 255 | my $p = reuseParams($reuse); | ||
| 256 | |||
| 257 | formatTable([sort {$$b[0] <=> $$a[0]} @report], <<END, # Report reuse count | ||
| 258 | Count Number of times the following content is reused | ||
| 259 | Tag The tag the following content occurred under | ||
| 260 | Length Length of the content being reused | ||
| 261 | Content Content being reused - first few characters there-of. | ||
| 262 | Md5 Md5 sum of content being reused | ||
| 263 | END | ||
| 264 | title => q(Reuse content by tag), | ||
| 265 | head => <<END, | ||
| 266 | NNNN blocks of content have been reused on DDDD | ||
| 267 | |||
| 268 | The content field below shows the first few characters of each block after | ||
| 269 | white space normalization. | ||
| 270 | |||
| 271 | $p | ||
| 272 | END | ||
| 273 | summarize => 0, | ||
| 274 | file => fpe($reuse->reportsFolder, qw(lists reused_content_by_tag txt))); | ||
| 275 | }, $reuse->inputFiles->@*; | ||
| 276 | |||
| 277 | reportSimilarContent($reuse) if $reuse->matchSimilarTagContent; # Look for similar content - done here so we can mark up files for similar content search | ||
| 278 | } # analyzeInputFiles | ||
| 279 | |||
| 280 | sub conRefOneFile($$) #P Conref one file. | ||
| 281 | {my ($reuse, $file) = @_; # Reuser, File to analyze | ||
| 282 | my $of = swapFilePrefix $file, $reuse->inputFolder, $reuse->outputFolder; # Output file | ||
| 283 | my %t = $reuse->tags->%*; | ||
| 284 | my $l = $reuse->minimumLength; | ||
| 285 | my $n = $reuse->minimumReferences; | ||
| 286 | my $x = Data::Edit::Xml::new($file); # Parse xml | ||
| 287 | my $r = $reuse->reusableContent; | ||
| 288 | |||
| 289 | $x->downToDie(sub # Replace largest blocks possible | ||
| 290 | {my ($o) = @_; | ||
| 291 | my $t = -t $o; | ||
| 292 | if ($t{$t}) # Move duplicate content to dictionary | ||
| 293 | {if (my ($m, $s) = $o->subMd5Tree) | ||
| 294 | {if (length($s) >= $l) | ||
| 295 | {if ($$r{$t}{$m}{$s} >= $n) | ||
| 296 | {$o->deleteContent; | ||
| 297 | my $f = relFromAbsAgainstAbs($reuse->dictionary, $of); | ||
| 298 | $o->set(conref=>qq($f#dictionary/).guidFromMd5($m)); | ||
| 299 | $o->putNextAsComment($s); | ||
| 300 | die; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | }); | ||
| 306 | |||
| 307 | $x->by(sub # Mark similar content with guids of similar items | ||
| 308 | {my ($o) = @_; | ||
| 309 | my $t = -t $o; | ||
| 310 | if ($t{$t}) | ||
| 311 | {if (my ($m, $s) = $o->subMd5Tree) | ||
| 312 | {if (length($s) >= $l) | ||
| 313 | {if (my $b = $reuse->matchInBlock->{$m}) # Mark similar content with guids of similar items | ||
| 314 | {my $r = join ' ', map{guidFromMd5 $_} grep{$_ ne $m} @$b; # Related guids | ||
| 315 | $o->set(id=>guidFromMd5($m), xtrf=>$r); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | }); | ||
| 321 | |||
| 322 | Data::Edit::Xml::Xref::editXml $file, $of, -p $x; | ||
| 323 | } # conRefOneFile | ||
| 324 | |||
| 325 | sub conRef($) #P Replace common text with conrefs. | ||
| 326 | {my ($reuse) = @_; # Cross referencer | ||
| 327 | lll "Create conrefs"; | ||
| 328 | processFilesInParallel # Md5 sums for each parse tree | ||
| 329 | sub | ||
| 330 | {conRefOneFile($reuse, $_[0]); | ||
| 331 | }, | ||
| 332 | undef, $reuse->inputFiles->@*; | ||
| 333 | } # conRef | ||
| 334 | |||
| 335 | sub reportSimilarContent($) #P Report content likely to be similar on the basis of their vocabulary. | ||
| 336 | {my ($reuse) = @_; # Reuser | ||
| 337 | lll "Reuse: find similar content"; | ||
| 338 | |||
| 339 | my $l = $reuse->matchSimilarTagContent; # Match level | ||
| 340 | my $p = int($l * 100); # Match level as a percentage | ||
| 341 | |||
| 342 | my %content; | ||
| 343 | if (my $r = $reuse->reusableContent) # Content to be matched identified by md5 sum | ||
| 344 | {for my $t(sort keys %$r) | ||
| 345 | {for my $m(sort keys $$r{$t}->%*) | ||
| 346 | {for my $s(sort keys $$r{$t}{$m}->%*) | ||
| 347 | {my $t = detagString $s; | ||
| 348 | $content{$m} = $t if length($t) >= $l; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | mmm "Reuse: partition similar content"; | ||
| 355 | my @m = grep {scalar(@$_) > 1} # Partition into like content based on vocabulary | ||
| 356 | setPartitionOnIntersectionOverUnionOfHashStringSetsInParallel | ||
| 357 | ($l, \%content); | ||
| 358 | |||
| 359 | $reuse->matchBlocks = [@m]; # Blocks of tags whose content is similar | ||
| 360 | |||
| 361 | for my $b(@m) | ||
| 362 | {for my $m(@$b) | ||
| 363 | {$reuse->matchInBlock->{$m} = $b; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | mmm "Reuse: report similar content"; | ||
| 368 | my @t; | ||
| 369 | for my $a(@m) # Each block of matching topics | ||
| 370 | {my ($first, @rest) = @$a; | ||
| 371 | push @t, [scalar(@$a), ffc($reuse, $content{$first}), guidFromMd5 $first], | ||
| 372 | map {[q(), ffc($reuse, $content{$_}), guidFromMd5 $_ ]} @rest; | ||
| 373 | push @t, [q(), q(), q()]; | ||
| 374 | } | ||
| 375 | |||
| 376 | my $m = @m; | ||
| 377 | my $q = reuseParams($reuse); | ||
| 378 | formatTables($reuse, \@t, | ||
| 379 | columns => <<END, | ||
| 380 | Similar The number of similar tags in this block | ||
| 381 | Tag_Content First few characters of similar content with white space normalized. | ||
| 382 | Md5Sum Md5 sum of originating tag content | ||
| 383 | END | ||
| 384 | title=>qq(Tags with similar vocabulary with $p % confidence), | ||
| 385 | head=><<END, | ||
| 386 | Reuse found $m groups of tags which have similar vocabulary in their content with $p % confidence on DDDD | ||
| 387 | |||
| 388 | $q | ||
| 389 | |||
| 390 | The GUIDS on the right an be used to L<grep> the output L<corpus> for the | ||
| 391 | corresponding text. | ||
| 392 | END | ||
| 393 | clearUpLeft => -1, summarize=>0, zero=>1, | ||
| 394 | file=>(my $f = fpe(qw(similar tag_blocks_by_vocabulary txt)))); | ||
| 395 | } # reportSimilarTopicsByVocabulary | ||
| 396 | |||
| 397 | #D0 | ||
| 398 | # podDocumentation | ||
| 399 | =pod | ||
| 400 | |||
| 401 | =encoding utf-8 | ||
| 402 | |||
| 403 | =head1 Name | ||
| 404 | |||
| 405 | Data::Edit::Xml::Reuse - Reuse L<Xml> via the L<Dita> L<conref> facility. | ||
| 406 | |||
| 407 | =head1 Synopsis | ||
| 408 | |||
| 409 | =head2 Reusing Identical Content | ||
| 410 | |||
| 411 | L<Data::Edit::Xml::Reuse> scans an entire document L<corpus> looking for | ||
| 412 | opportunities to reuse identical L<Xml> via the L<Dita> L<conref> facility. | ||
| 413 | Duplicated identical content is moved to a separate L<Xml> file called the | ||
| 414 | B<dictionary>. Duplicated content in the L<corpus> is replaced with references to | ||
| 415 | the singular content in the dictionary. Larger blocks of identical content are | ||
| 416 | favored over smaller blocks of content where possible. | ||
| 417 | |||
| 418 | L<Data::Edit::Xml::Reuse> provides parameters that qualify the minimum size of | ||
| 419 | a block of content and the minimum number of references to a block of content | ||
| 420 | to be moved to the dictionary. | ||
| 421 | |||
| 422 | The following example checks the a L<corpus> of Dita L<Xml> documents held in | ||
| 423 | folder L<inputFolder|/inputFolder>. A copy of the L<corpus> with a L<conref> | ||
| 424 | replacing each block of identical content under the B<table> and B<p> tags is | ||
| 425 | placed in the L<outputFolder|/outputFolder> as long as such content is at least | ||
| 426 | 32 characters long and has a minimum of 4 references to it: | ||
| 427 | |||
| 428 | use Data::Edit::Xml::Reuse; | ||
| 429 | |||
| 430 | my $x = Data::Edit::Xml::Reuse::reuse | ||
| 431 | (inputFolder => q(in), | ||
| 432 | outputFolder => q(out), | ||
| 433 | reportsFolder => q(reports), | ||
| 434 | minimumLength => 32, | ||
| 435 | minimumReferences => 4, | ||
| 436 | tags => {map {$_=>1} qw(table p)}, | ||
| 437 | ); | ||
| 438 | |||
| 439 | The actual number of times each block of content was reused can be found in | ||
| 440 | report: | ||
| 441 | |||
| 442 | lists/reused_content_by_tag.txt | ||
| 443 | |||
| 444 | in the L<reportsFolder|/reportsFolder>. | ||
| 445 | |||
| 446 | =head2 Matching Similar Content | ||
| 447 | |||
| 448 | Optionally, L<Data::Edit::Xml::Reuse> will also report similar content using | ||
| 449 | the: | ||
| 450 | |||
| 451 | matchSimilarTagContent => 0.9, | ||
| 452 | |||
| 453 | keyword. Content under the specified L<tags> that matches to the specified | ||
| 454 | level of confidence between 0 and 1 is assigned a L<guid> B<id> attribute and | ||
| 455 | written to report: | ||
| 456 | |||
| 457 | similar/tag_blocks_by_vocabulary.txt | ||
| 458 | |||
| 459 | in the L<reportsFolder|/reportsFolder>. | ||
| 460 | |||
| 461 | The L<tags> containing similar content will have this L<guid> listed on their | ||
| 462 | B<xtrf> attribute making it easy to locate related content using L<grep>. | ||
| 463 | |||
| 464 | The report, combined with the id and xtrf attributes, helps identify similar | ||
| 465 | text, in situ, perhaps to be standardized further and eventually reused. | ||
| 466 | |||
| 467 | =head1 Description | ||
| 468 | |||
| 469 | Reuse L<Xml|https://en.wikipedia.org/wiki/XML> via the L<Dita|http://docs.oasis-open.org/dita/dita/v1.3/os/part2-tech-content/dita-v1.3-os-part2-tech-content.html> L<conref|http://docs.oasis-open.org/dita/dita/v1.3/errata02/os/complete/part3-all-inclusive/archSpec/base/conref.html#conref> facility. | ||
| 470 | |||
| 471 | |||
| 472 | Version 20191221. | ||
| 473 | |||
| 474 | |||
| 475 | The following sections describe the methods in each functional area of this | ||
| 476 | module. For an alphabetic listing of all methods by name see L<Index|/Index>. | ||
| 477 | |||
| 478 | |||
| 479 | |||
| 480 | =head1 Reuse Xml | ||
| 481 | |||
| 482 | Reuse Xml via Dita conrefs. | ||
| 483 | |||
| 484 | =head2 reuse(%) | ||
| 485 | |||
| 486 | Check Xml for reuse opportunities. | ||
| 487 | |||
| 488 | Parameter Description | ||
| 489 | 1 %attributes Reuse attributes | ||
| 490 | |||
| 491 | B<Example:> | ||
| 492 | |||
| 493 | |||
| 494 | if (1) { | ||
| 495 | owf($in[0], <<END); # Base file | ||
| 496 | <concept id="c"> | ||
| 497 | <title>Ordering information</title> | ||
| 498 | <conbody> | ||
| 499 | <p>For further information, please visit our web site or contact your local sales company.</p> | ||
| 500 | <p>Made in Sweden</p> | ||
| 501 | <table> | ||
| 502 | <tbody> | ||
| 503 | <row> | ||
| 504 | <entry><p>Ice Associates</p></entry> | ||
| 505 | <entry><p>North Pole 1</p></entry> | ||
| 506 | </row> | ||
| 507 | </tbody> | ||
| 508 | </table> | ||
| 509 | <p>aaa bbb ccc ddd eee</p> | ||
| 510 | </conbody> | ||
| 511 | </concept> | ||
| 512 | END | ||
| 513 | |||
| 514 | owf($in[1], <<END); # Similar file | ||
| 515 | <concept id="c"> | ||
| 516 | <title>Ordering information</title> | ||
| 517 | <conbody> | ||
| 518 | <p>For further information, please visit our web site or contact your local sales company.</p> | ||
| 519 | <p>Copyright © 2018 - 2019. All rights reserved.</p> | ||
| 520 | <p>Made in Norway</p> | ||
| 521 | <table> | ||
| 522 | <tbody> | ||
| 523 | <row> | ||
| 524 | <entry><p>Ice Associates</p></entry> | ||
| 525 | <entry><p>North Pole 1</p></entry> | ||
| 526 | </row> | ||
| 527 | </tbody> | ||
| 528 | </table> | ||
| 529 | <p>aaa bbb ccc ddd fff</p> | ||
| 530 | </conbody> | ||
| 531 | </concept> | ||
| 532 | END | ||
| 533 | |||
| 534 | my $dictionary = fpf($outputFolder, qw(dictionary xml)); # Dictionary file name | ||
| 535 | |||
| 536 | my $r = Data::Edit::Xml::Reuse::𝗿𝗲𝘂𝘀𝗲 | ||
| 537 | (dictionary => $dictionary, # Reuse request | ||
| 538 | inputFolder => $inputFolder, | ||
| 539 | matchSimilarTagContent => 0.5, | ||
| 540 | outputFolder => $outputFolder, | ||
| 541 | reportsFolder => $reportsFolder, | ||
| 542 | tags => {map {$_=>1} qw(p table)}, | ||
| 543 | ); | ||
| 544 | |||
| 545 | ok readFile($dictionary) eq <<END; # Resulting dictionary | ||
| 546 | <concept id="dictionary"> | ||
| 547 | <title>Dictionary</title> | ||
| 548 | <conbody> | ||
| 549 | <p id="GUID-63271233-3e86-9ac1-5fe8-086bc8b37b51">For further information, please visit our web site or contact your local sales company.</p> | ||
| 550 | <table id="GUID-e7a012a5-8ff5-6f12-5b96-0137c5c0a0b4"> | ||
| 551 | <tbody> | ||
| 552 | <row> | ||
| 553 | <entry> | ||
| 554 | <p>Ice Associates</p> | ||
| 555 | </entry> | ||
| 556 | <entry> | ||
| 557 | <p>North Pole 1</p> | ||
| 558 | </entry> | ||
| 559 | </row> | ||
| 560 | </tbody> | ||
| 561 | </table> | ||
| 562 | </conbody> | ||
| 563 | </concept> | ||
| 564 | END | ||
| 565 | |||
| 566 | if (my $h = # Similar XML report | ||
| 567 | readFile(fpe($reportsFolder, qw(similar tag_blocks_by_vocabulary txt)))) | ||
| 568 | {ok index($h, <<END) > 0; | ||
| 569 | Similar Tag_Content Md5Sum | ||
| 570 | 1 2 aaa bbb ccc ddd eee GUID-3c8810e0-d8aa-0484-84b8-a57230b756de | ||
| 571 | 2 aaa bbb ccc ddd fff GUID-7472a890-4587-8393-9c34-0aa3859d2e21 | ||
| 572 | END | ||
| 573 | } | ||
| 574 | |||
| 575 | ok readFile(fpe($testFolder, qw(out 1 xml))) eq <<END; # Deduplicated XML file - Sweden | ||
| 576 | <concept id="c"> | ||
| 577 | <title>Ordering information</title> | ||
| 578 | <conbody> | ||
| 579 | <p conref="dictionary/xml#dictionary/GUID-63271233-3e86-9ac1-5fe8-086bc8b37b51"/> | ||
| 580 | <!-- For further information, please visit our web site or contact your local sales company. --> | ||
| 581 | <p>Made in Sweden</p> | ||
| 582 | <table conref="dictionary/xml#dictionary/GUID-e7a012a5-8ff5-6f12-5b96-0137c5c0a0b4"/> | ||
| 583 | <!-- <tbody><row><entry><p>Ice Associates</p></entry><entry><p>North Pole 1</p></entry></row></tbody> --> | ||
| 584 | <p id="GUID-3c8810e0-d8aa-0484-84b8-a57230b756de" xtrf="GUID-7472a890-4587-8393-9c34-0aa3859d2e21">aaa bbb ccc ddd eee</p> | ||
| 585 | </conbody> | ||
| 586 | </concept> | ||
| 587 | END | ||
| 588 | |||
| 589 | ok readFile(fpe($testFolder, qw(out 2 xml))) eq <<END; # Deduplicated XML file - Norway | ||
| 590 | <concept id="c"> | ||
| 591 | <title>Ordering information</title> | ||
| 592 | <conbody> | ||
| 593 | <p conref="dictionary/xml#dictionary/GUID-63271233-3e86-9ac1-5fe8-086bc8b37b51"/> | ||
| 594 | <!-- For further information, please visit our web site or contact your local sales company. --> | ||
| 595 | <p>Copyright © 2018 - 2019. All rights reserved.</p> | ||
| 596 | <p>Made in Norway</p> | ||
| 597 | <table conref="dictionary/xml#dictionary/GUID-e7a012a5-8ff5-6f12-5b96-0137c5c0a0b4"/> | ||
| 598 | <!-- <tbody><row><entry><p>Ice Associates</p></entry><entry><p>North Pole 1</p></entry></row></tbody> --> | ||
| 599 | <p id="GUID-7472a890-4587-8393-9c34-0aa3859d2e21" xtrf="GUID-3c8810e0-d8aa-0484-84b8-a57230b756de">aaa bbb ccc ddd fff</p> | ||
| 600 | </conbody> | ||
| 601 | </concept> | ||
| 602 | END | ||
| 603 | |||
| 604 | } | ||
| 605 | |||
| 606 | |||
| 607 | |||
| 608 | =head2 Data::Edit::Xml::Reuse Definition | ||
| 609 | |||
| 610 | |||
| 611 | Attributes used by the reuser. | ||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | |||
| 616 | =head3 Input fields | ||
| 617 | |||
| 618 | |||
| 619 | B<dictionary> - The dictionary file into which to store the duplicate L<Xml|https://en.wikipedia.org/wiki/XML>. | ||
| 620 | |||
| 621 | B<fileExtensions> - The extensions of the L<Xml|https://en.wikipedia.org/wiki/XML> files to examine in the L<inputFolder>. | ||
| 622 | |||
| 623 | B<getFileUrl> - An optional url to retrieve a specified file from the server running xref used in generating html reports. The complete url is obtained by appending the fully qualified file name to this value. | ||
| 624 | |||
| 625 | B<htmlFolder> - Folder into which to write reports as html. | ||
| 626 | |||
| 627 | B<inputFolder> - A folder containing the L<Xml|https://en.wikipedia.org/wiki/XML> files with extensions named in L<fileExtensions> to be analyzed for reuse. | ||
| 628 | |||
| 629 | B<matchSimilarTagContent> - Confidence level between 0 and 1: match content under L<tags> with this level of confidence. | ||
| 630 | |||
| 631 | B<maximumColumnWidth> - Truncate columns in text reports to this length or allow any width if B<undef>. | ||
| 632 | |||
| 633 | B<minimumLength> - The minimum length content must have to be considered for matching. | ||
| 634 | |||
| 635 | B<minimumReferences> - The minimum number of references content must have before it can be reused. | ||
| 636 | |||
| 637 | B<outputFolder> - A folder into which to write the deduplicated L<Xml|https://en.wikipedia.org/wiki/XML>. | ||
| 638 | |||
| 639 | B<reportsFolder> - A folder into which reports will be written. | ||
| 640 | |||
| 641 | B<tags> - {tag=>1} only consider tags that appear as keys in this hash with truthful values. | ||
| 642 | |||
| 643 | |||
| 644 | |||
| 645 | =head3 Output fields | ||
| 646 | |||
| 647 | |||
| 648 | B<inputFiles> - The files selected from L<inputFolder> for analysis because their extensions matched L<fileExtensions>. | ||
| 649 | |||
| 650 | B<matchBlocks> - [[md5, content]*] blocks of content that match with the confidence level expressed by L<matchSimilarContent> | ||
| 651 | |||
| 652 | B<matchInBlock> - {md5 => matchBlocks} : index into L<matchBlocks> by md5 sum. | ||
| 653 | |||
| 654 | B<reusableContent> - {tag}{md5sum}{content}++ potentially reusable content. | ||
| 655 | |||
| 656 | B<timeEnded> - Time the run ended. | ||
| 657 | |||
| 658 | B<timeStart> - Time the run started. | ||
| 659 | |||
| 660 | |||
| 661 | |||
| 662 | =head1 Private Methods | ||
| 663 | |||
| 664 | =head2 newReuse(%) | ||
| 665 | |||
| 666 | Create a new cross reuser. | ||
| 667 | |||
| 668 | Parameter Description | ||
| 669 | 1 %attributes Attributes | ||
| 670 | |||
| 671 | =head2 formatTables($$%) | ||
| 672 | |||
| 673 | Format reports. | ||
| 674 | |||
| 675 | Parameter Description | ||
| 676 | 1 $reuse Reuser | ||
| 677 | 2 $data Table to be formatted | ||
| 678 | 3 %options Options | ||
| 679 | |||
| 680 | =head2 loadInputFiles($) | ||
| 681 | |||
| 682 | Load the names of the files to be processed. | ||
| 683 | |||
| 684 | Parameter Description | ||
| 685 | 1 $reuse Cross referencer | ||
| 686 | |||
| 687 | =head2 ffc($$) | ||
| 688 | |||
| 689 | First few characters of a string with white space normalized. | ||
| 690 | |||
| 691 | Parameter Description | ||
| 692 | 1 $reuse Reuser | ||
| 693 | 2 $string String | ||
| 694 | |||
| 695 | =head2 reuseParams($) | ||
| 696 | |||
| 697 | Tabulate reuse parameters. | ||
| 698 | |||
| 699 | Parameter Description | ||
| 700 | 1 $reuse Reuser | ||
| 701 | |||
| 702 | =head2 analyzeOneFile($$) | ||
| 703 | |||
| 704 | Analyze one input file. | ||
| 705 | |||
| 706 | Parameter Description | ||
| 707 | 1 $reuse Reuser | ||
| 708 | 2 $file File to analyze | ||
| 709 | |||
| 710 | =head2 analyzeInputFiles($) | ||
| 711 | |||
| 712 | Analyze the input files. | ||
| 713 | |||
| 714 | Parameter Description | ||
| 715 | 1 $reuse Reuser | ||
| 716 | |||
| 717 | =head2 conRefOneFile($$) | ||
| 718 | |||
| 719 | Conref one file. | ||
| 720 | |||
| 721 | Parameter Description | ||
| 722 | 1 $reuse Reuser | ||
| 723 | 2 $file File to analyze | ||
| 724 | |||
| 725 | =head2 conRef($) | ||
| 726 | |||
| 727 | Replace common text with conrefs. | ||
| 728 | |||
| 729 | Parameter Description | ||
| 730 | 1 $reuse Cross referencer | ||
| 731 | |||
| 732 | =head2 reportSimilarContent($) | ||
| 733 | |||
| 734 | Report content likely to be similar on the basis of their vocabulary. | ||
| 735 | |||
| 736 | Parameter Description | ||
| 737 | 1 $reuse Reuser | ||
| 738 | |||
| 739 | |||
| 740 | =head1 Index | ||
| 741 | |||
| 742 | |||
| 743 | 1 L<analyzeInputFiles|/analyzeInputFiles> - Analyze the input files. | ||
| 744 | |||
| 745 | 2 L<analyzeOneFile|/analyzeOneFile> - Analyze one input file. | ||
| 746 | |||
| 747 | 3 L<conRef|/conRef> - Replace common text with conrefs. | ||
| 748 | |||
| 749 | 4 L<conRefOneFile|/conRefOneFile> - Conref one file. | ||
| 750 | |||
| 751 | 5 L<ffc|/ffc> - First few characters of a string with white space normalized. | ||
| 752 | |||
| 753 | 6 L<formatTables|/formatTables> - Format reports. | ||
| 754 | |||
| 755 | 7 L<loadInputFiles|/loadInputFiles> - Load the names of the files to be processed. | ||
| 756 | |||
| 757 | 8 L<newReuse|/newReuse> - Create a new cross reuser. | ||
| 758 | |||
| 759 | 9 L<reportSimilarContent|/reportSimilarContent> - Report content likely to be similar on the basis of their vocabulary. | ||
| 760 | |||
| 761 | 10 L<reuse|/reuse> - Check Xml for reuse opportunities. | ||
| 762 | |||
| 763 | 11 L<reuseParams|/reuseParams> - Tabulate reuse parameters. | ||
| 764 | |||
| 765 | =head1 Installation | ||
| 766 | |||
| 767 | This module is written in 100% Pure Perl and, thus, it is easy to read, | ||
| 768 | comprehend, use, modify and install via B<cpan>: | ||
| 769 | |||
| 770 | sudo cpan install Data::Edit::Xml::Reuse | ||
| 771 | |||
| 772 | =head1 Author | ||
| 773 | |||
| 774 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||
| 775 | |||
| 776 | L<http://www.appaapps.com|http://www.appaapps.com> | ||
| 777 | |||
| 778 | =head1 Copyright | ||
| 779 | |||
| 780 | Copyright (c) 2016-2019 Philip R Brenan. | ||
| 781 | |||
| 782 | This module is free software. It may be used, redistributed and/or modified | ||
| 783 | under the same terms as Perl itself. | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | |||
| 788 | |||
| 789 | # Tests and documentation | ||
| 790 | |||
| 791 | sub test | ||
| 792 | {my $p = __PACKAGE__; | ||
| 793 | binmode($_, ":utf8") for *STDOUT, *STDERR; | ||
| 794 | return if eval "eof(${p}::DATA)"; | ||
| 795 | my $s = eval "join('', <${p}::DATA>)"; | ||
| 796 | $@ and die $@; | ||
| 797 | eval $s; | ||
| 798 | $@ and die $@; | ||
| 799 | 1 | ||
| 800 | } | ||
| 801 | |||
| 802 | test unless caller; | ||
| 803 | |||
| 804 | 1; | ||
| 805 | # podDocumentation | ||
| 806 | __DATA__ | ||