b5e3ed1a52778785114716d900748cddd6222340
[scpubgit/stemmatology.git] / base / t / text_tradition_collation.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use Text::Tradition;
12 use TryCatch;
13
14 my $cxfile = 't/data/Collatex-16.xml';
15 my $t = Text::Tradition->new( 
16     'name'  => 'inline', 
17     'input' => 'CollateX',
18     'file'  => $cxfile,
19     );
20 my $c = $t->collation;
21
22 my $rno = scalar $c->readings;
23 # Split n21 ('unto') for testing purposes
24 my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
25 my $old_r = $c->reading( 'n21' );
26 $old_r->alter_text( 'to' );
27 $c->del_path( 'n20', 'n21', 'A' );
28 $c->add_path( 'n20', 'n21p0', 'A' );
29 $c->add_path( 'n21p0', 'n21', 'A' );
30 $c->add_relationship( 'n21', 'n22', { type => 'collated', scope => 'local' } );
31 $c->flatten_ranks();
32 ok( $c->reading( 'n21p0' ), "New reading exists" );
33 is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
34
35 # Combine n3 and n4 ( with his )
36 $c->merge_readings( 'n3', 'n4', 1 );
37 ok( !$c->reading('n4'), "Reading n4 is gone" );
38 is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
39
40 # Collapse n9 and n10 ( rood / root )
41 $c->merge_readings( 'n9', 'n10' );
42 ok( !$c->reading('n10'), "Reading n10 is gone" );
43 is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
44
45 # Try to combine n21 and n21p0. This should break.
46 my $remaining = $c->reading('n21');
47 $remaining ||= $c->reading('n22');  # one of these should still exist
48 try {
49         $c->merge_readings( 'n21p0', $remaining, 1 );
50         ok( 0, "Bad reading merge changed the graph" );
51 } catch( Text::Tradition::Error $e ) {
52         like( $e->message, qr/neither concatenated nor collated/, "Expected exception from bad concatenation" );
53 } catch {
54         ok( 0, "Unexpected error on bad reading merge: $@" );
55 }
56
57 try {
58         $c->calculate_ranks();
59         ok( 1, "Graph is still evidently whole" );
60 } catch( Text::Tradition::Error $e ) {
61         ok( 0, "Caught a rank exception: " . $e->message );
62 }
63 }
64
65
66
67 # =begin testing
68 {
69 use Test::Warn;
70 use Text::Tradition;
71 use TryCatch;
72
73 my $t;
74 warnings_exist {
75         $t = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
76 } [qr/Cannot set relationship on a meta reading/],
77         "Got expected relationship drop warning on parse";
78
79 my $c = $t->collation;
80 # Force the transitive propagation of all existing relationships.
81 $c->relations->propagate_all_relationships();
82
83 my %rdg_ids;
84 map { $rdg_ids{$_} = 1 } $c->readings;
85 $c->merge_related( 'orthographic' );
86 is( scalar( $c->readings ), keys( %rdg_ids ) - 9, 
87         "Successfully collapsed orthographic variation" );
88 map { $rdg_ids{$_} = undef } qw/ r13.3 r11.4 r8.5 r8.2 r7.7 r7.5 r7.4 r7.3 r7.1 /;
89 foreach my $rid ( keys %rdg_ids ) {
90         my $exp = $rdg_ids{$rid};
91         is( !$c->reading( $rid ), !$exp, "Reading $rid correctly " . 
92                 ( $exp ? "retained" : "removed" ) );
93 }
94 ok( $c->linear, "Graph is still linear" );
95 try {
96         $c->calculate_ranks; # This should succeed
97         ok( 1, "Can still calculate ranks on the new graph" );
98 } catch {
99         ok( 0, "Rank calculation on merged graph failed: $@" );
100 }
101
102 # Now add some transpositions
103 $c->add_relationship( 'r8.4', 'r10.4', { type => 'transposition' } );
104 $c->merge_related( 'transposition' );
105 is( scalar( $c->readings ), keys( %rdg_ids ) - 10, 
106         "Transposed relationship is merged away" );
107 ok( !$c->reading('r8.4'), "Correct transposed reading removed" );
108 ok( !$c->linear, "Graph is no longer linear" );
109 try {
110         $c->calculate_ranks; # This should fail
111         ok( 0, "Rank calculation happened on nonlinear graph?!" );
112 } catch ( Text::Tradition::Error $e ) {
113         is( $e->message, 'Cannot calculate ranks on a non-linear graph', 
114                 "Rank calculation on merged graph threw an error" );
115 }
116 }
117
118
119
120 # =begin testing
121 {
122 use Text::Tradition;
123
124 my $t = Text::Tradition->new( input => 'CollateX', file => 't/data/Collatex-16.xml' );
125 my $c = $t->collation;
126 my $n = scalar $c->readings;
127 $c->compress_readings();
128 is( scalar $c->readings, $n - 6, "Compressing readings seems to work" );
129
130 # Now put in a join-word and make sure the thing still works.
131 my $t2 = Text::Tradition->new( input => 'CollateX', file => 't/data/Collatex-16.xml' );
132 my $c2 = $t2->collation;
133 # Split n21 ('unto') for testing purposes
134 my $new_r = $c2->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
135 my $old_r = $c2->reading( 'n21' );
136 $old_r->alter_text( 'to' );
137 $c2->del_path( 'n20', 'n21', 'A' );
138 $c2->add_path( 'n20', 'n21p0', 'A' );
139 $c2->add_path( 'n21p0', 'n21', 'A' );
140 $c2->calculate_ranks();
141 is( scalar $c2->readings, $n + 1, "We have our extra test reading" );
142 $c2->compress_readings();
143 is( scalar $c2->readings, $n - 6, "Compressing readings also works with join_next" );
144 is( $c2->reading( 'n21p0' )->text, 'unto', "The joined word has no space" );
145 }
146
147
148
149 # =begin testing
150 {
151 use Test::More::UTF8;
152 use Text::Tradition;
153 use TryCatch;
154
155 my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
156 is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
157 ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
158
159 my $sc = $st->collation;
160 my $numr = 17;
161 ok( $sc->reading('n131'), "Tradition has the affected reading" );
162 is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
163 is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
164
165 # Detach the erroneously collated reading
166 my( $newr, @del_rdgs ) = $sc->duplicate_reading( 'n131', 'Ba96' );
167 ok( $newr, "New reading was created" );
168 ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
169 is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
170 is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
171 my $csucc = $sc->common_successor( 'n131', 'n131_0' );
172 is( $csucc->id, 'n136', "Found correct common successor to duped reading" ); 
173
174 # Check that the bad transposition is gone
175 is( scalar @del_rdgs, 1, "Deleted reading was returned by API call" );
176 is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
177
178 # The collation should not be fixed
179 my @pairs = $sc->identical_readings();
180 is( scalar @pairs, 0, "Not re-collated yet" );
181 # Fix the collation
182 ok( $sc->merge_readings( 'n124', 'n131_0' ), "Collated the readings correctly" );
183 @pairs = $sc->identical_readings( start => 'n124', end => $csucc->id );
184 is( scalar @pairs, 3, "Found three more identical readings" );
185 is( $sc->end->rank, 11, "The ranks shifted appropriately" );
186 $sc->flatten_ranks();
187 is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
188
189 # Check that we can't "duplicate" a reading with no wits or with all wits
190 try {
191         my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124' );
192         ok( 0, "Reading duplication without witnesses throws an error" );
193 } catch( Text::Tradition::Error $e ) {
194         like( $e->message, qr/Must specify one or more witnesses/, 
195                 "Reading duplication without witnesses throws the expected error" );
196 } catch {
197         ok( 0, "Reading duplication without witnesses threw the wrong error" );
198 }
199
200 try {
201         my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124', 'Ba96', 'Mü11475' );
202         ok( 0, "Reading duplication with all witnesses throws an error" );
203 } catch( Text::Tradition::Error $e ) {
204         like( $e->message, qr/Cannot join all witnesses/, 
205                 "Reading duplication with all witnesses throws the expected error" );
206 } catch {
207         ok( 0, "Reading duplication with all witnesses threw the wrong error" );
208 }
209
210 try {
211         $sc->calculate_ranks();
212         ok( 1, "Graph is still evidently whole" );
213 } catch( Text::Tradition::Error $e ) {
214         ok( 0, "Caught a rank exception: " . $e->message );
215 }
216 }
217
218
219
220 # =begin testing
221 {
222 use JSON qw/ from_json /;
223 use Text::Tradition;
224
225 my $t = Text::Tradition->new( 
226         'input' => 'Self',
227         'file' => 't/data/florilegium_graphml.xml' );
228 my $c = $t->collation;
229         
230 # Make a connection so we can test rank preservation
231 $c->add_relationship( 'w91', 'w92', { type => 'grammatical' } );
232
233 # Create an adjacency list of the whole thing; test the output.
234 my $adj_whole = from_json( $c->as_adjacency_list() );
235 is( scalar @$adj_whole, scalar $c->readings(), 
236         "Same number of nodes in graph and adjacency list" );
237 my @adj_whole_edges;
238 map { push( @adj_whole_edges, @{$_->{adjacent}} ) } @$adj_whole;
239 is( scalar @adj_whole_edges, scalar $c->sequence->edges,
240         "Same number of edges in graph and adjacency list" );
241 # Find the reading whose rank should be preserved
242 my( $test_rdg ) = grep { $_->{id} eq 'w89' } @$adj_whole;
243 my( $test_edge ) = grep { $_->{id} eq 'w92' } @{$test_rdg->{adjacent}};
244 is( $test_edge->{minlen}, 2, "Rank of test reading is preserved" );
245
246 # Now create an adjacency list of just a portion. w76 to w122
247 my $adj_part = from_json( $c->as_adjacency_list(
248         { from => $c->reading('w76')->rank,
249           to   => $c->reading('w122')->rank }));
250 is( scalar @$adj_part, 48, "Correct number of nodes in partial graph" );
251 my @adj_part_edges;
252 map { push( @adj_part_edges, @{$_->{adjacent}} ) } @$adj_part;
253 is( scalar @adj_part_edges, 58,
254         "Same number of edges in partial graph and adjacency list" );
255 # Check for consistency
256 my %part_nodes;
257 map { $part_nodes{$_->{id}} = 1 } @$adj_part;
258 foreach my $edge ( @adj_part_edges ) {
259         my $testid = $edge->{id};
260         ok( $part_nodes{$testid}, "ID $testid referenced in edge is given as node" );
261 }
262 }
263
264
265
266 # =begin testing
267 {
268 use Text::Tradition;
269 use TryCatch;
270
271 my $READINGS = 311;
272 my $PATHS = 361;
273
274 my $datafile = 't/data/florilegium_tei_ps.xml';
275 my $tradition = Text::Tradition->new( 'input' => 'TEI',
276                                       'name' => 'test0',
277                                       'file' => $datafile,
278                                       'linear' => 1 );
279
280 ok( $tradition, "Got a tradition object" );
281 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
282 ok( $tradition->collation, "Tradition has a collation" );
283
284 my $c = $tradition->collation;
285 is( scalar $c->readings, $READINGS, "Collation has all readings" );
286 is( scalar $c->paths, $PATHS, "Collation has all paths" );
287 is( scalar $c->relationships, 0, "Collation has all relationships" );
288
289 # Add a few relationships
290 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
291 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
292 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition', 
293                                           'is_significant' => 'yes' } );
294
295 # Now write it to GraphML and parse it again.
296
297 my $graphml = $c->as_graphml;
298 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
299 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
300 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
301 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
302 my $sigrel = $st->collation->get_relationship( 'w257', 'w262' );
303 is( $sigrel->is_significant, 'yes', "Ternary attribute value was restored" );
304
305 # Now add a stemma, write to GraphML, and look at the output.
306 SKIP: {
307         skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
308         my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
309         is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
310         is( $tradition->stemmata, 1, "Tradition now has the stemma" );
311         $graphml = $c->as_graphml;
312         like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
313 }
314 }
315
316
317
318 # =begin testing
319 {
320 use Text::Tradition;
321 use Text::CSV;
322
323 my $READINGS = 311;
324 my $PATHS = 361;
325 my $WITS = 13;
326 my $WITAC = 4;
327
328 my $datafile = 't/data/florilegium_tei_ps.xml';
329 my $tradition = Text::Tradition->new( 'input' => 'TEI',
330                                       'name' => 'test0',
331                                       'file' => $datafile,
332                                       'linear' => 1 );
333
334 my $c = $tradition->collation;
335 # Export the thing to CSV
336 my $csvstr = $c->as_csv();
337 # Count the columns
338 my $csv = Text::CSV->new({ sep_char => ',', binary => 1 });
339 my @lines = split(/\n/, $csvstr );
340 ok( $csv->parse( $lines[0] ), "Successfully parsed first line of CSV" );
341 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
342 my @q_ac = grep { $_ eq 'Q'.$c->ac_label } $csv->fields;
343 ok( @q_ac, "Found a layered witness" );
344
345 my $t2 = Text::Tradition->new( input => 'Tabular',
346                                                            name => 'test2',
347                                                            string => $csvstr,
348                                                            sep_char => ',' );
349 is( scalar $t2->collation->readings, $READINGS, "Reparsed CSV collation has all readings" );
350 is( scalar $t2->collation->paths, $PATHS, "Reparsed CSV collation has all paths" );
351
352 # Now do it with TSV
353 my $tsvstr = $c->as_tsv();
354 my $t3 = Text::Tradition->new( input => 'Tabular',
355                                                            name => 'test3',
356                                                            string => $tsvstr,
357                                                            sep_char => "\t" );
358 is( scalar $t3->collation->readings, $READINGS, "Reparsed TSV collation has all readings" );
359 is( scalar $t3->collation->paths, $PATHS, "Reparsed TSV collation has all paths" );
360
361 my $table = $c->alignment_table;
362 my $noaccsv = $c->as_csv({ noac => 1 });
363 my @noaclines = split(/\n/, $noaccsv );
364 ok( $csv->parse( $noaclines[0] ), "Successfully parsed first line of no-ac CSV" );
365 is( scalar( $csv->fields ), $WITS, "CSV has correct number of witness columns" );
366 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
367
368 my $safecsv = $c->as_csv({ safe_ac => 1});
369 my @safelines = split(/\n/, $safecsv );
370 ok( $csv->parse( $safelines[0] ), "Successfully parsed first line of safe CSV" );
371 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
372 @q_ac = grep { $_ eq 'Q__L' } $csv->fields;
373 ok( @q_ac, "Found a sanitized layered witness" );
374 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
375
376 # Test relationship collapse
377 $c->add_relationship( $c->readings_at_rank( 37 ), { type => 'spelling' } );
378 $c->add_relationship( $c->readings_at_rank( 60 ), { type => 'spelling' } );
379
380 my $mergedtsv = $c->as_tsv({mergetypes => [ 'spelling', 'orthographic' ] });
381 my $t4 = Text::Tradition->new( input => 'Tabular',
382                                                            name => 'test4',
383                                                            string => $mergedtsv,
384                                                            sep_char => "\t" );
385 is( scalar $t4->collation->readings, $READINGS - 2, "Reparsed TSV merge collation has fewer readings" );
386 is( scalar $t4->collation->paths, $PATHS - 4, "Reparsed TSV merge collation has fewer paths" );
387
388 # Test non-ASCII sigla
389 my $t5 = Text::Tradition->new( input => 'Tabular',
390                                                            name => 'nonascii',
391                                                            file => 't/data/armexample.xlsx',
392                                                            excel => 'xlsx' );
393 my $awittsv = $t5->collation->as_tsv({ noac => 1, ascii => 1 });
394 my @awitlines = split( /\n/, $awittsv );
395 like( $awitlines[0], qr/_A_5315622/, "Found ASCII sigil variant in TSV" );
396 }
397
398
399
400 # =begin testing
401 {
402 use Text::Tradition;
403
404 my $cxfile = 't/data/Collatex-16.xml';
405 my $t = Text::Tradition->new( 
406     'name'  => 'inline', 
407     'input' => 'CollateX',
408     'file'  => $cxfile,
409     );
410 my $c = $t->collation;
411
412 # Make an svg
413 my $table = $c->alignment_table;
414 ok( $c->has_cached_table, "Alignment table was cached" );
415 is( $c->alignment_table, $table, "Cached table returned upon second call" );
416 $c->calculate_ranks;
417 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
418 $c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
419 is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
420 $c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
421 isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
422 }
423
424
425
426 # =begin testing
427 {
428 use Text::Tradition;
429
430 my $cxfile = 't/data/Collatex-16.xml';
431 my $t = Text::Tradition->new( 
432     'name'  => 'inline', 
433     'input' => 'CollateX',
434     'file'  => $cxfile,
435     );
436 my $c = $t->collation;
437
438 my @common = $c->calculate_common_readings();
439 is( scalar @common, 8, "Found correct number of common readings" );
440 my @marked = sort $c->common_readings();
441 is( scalar @common, 8, "All common readings got marked as such" );
442 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
443 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
444 }
445
446
447
448 # =begin testing
449 {
450 use Text::Tradition;
451
452 my $cxfile = 't/data/Collatex-16.xml';
453 my $t = Text::Tradition->new( 
454     'name'  => 'inline', 
455     'input' => 'CollateX',
456     'file'  => $cxfile,
457     );
458 my $c = $t->collation;
459
460 is( $c->common_predecessor( 'n24', 'n23' )->id, 
461     'n20', "Found correct common predecessor" );
462 is( $c->common_successor( 'n24', 'n23' )->id, 
463     '__END__', "Found correct common successor" );
464
465 is( $c->common_predecessor( 'n19', 'n17' )->id, 
466     'n16', "Found correct common predecessor for readings on same path" );
467 is( $c->common_successor( 'n21', 'n10' )->id, 
468     '__END__', "Found correct common successor for readings on same path" );
469 }
470
471
472
473
474 1;