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