add method to collapse tradition by relationship
[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
13 my $cxfile = 't/data/Collatex-16.xml';
14 my $t = Text::Tradition->new( 
15     'name'  => 'inline', 
16     'input' => 'CollateX',
17     'file'  => $cxfile,
18     );
19 my $c = $t->collation;
20
21 my $rno = scalar $c->readings;
22 # Split n21 ('unto') for testing purposes
23 my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
24 my $old_r = $c->reading( 'n21' );
25 $old_r->alter_text( 'to' );
26 $c->del_path( 'n20', 'n21', 'A' );
27 $c->add_path( 'n20', 'n21p0', 'A' );
28 $c->add_path( 'n21p0', 'n21', 'A' );
29 $c->add_relationship( 'n21', 'n22', { type => 'collated', scope => 'local' } );
30 $c->flatten_ranks();
31 ok( $c->reading( 'n21p0' ), "New reading exists" );
32 is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
33
34 # Combine n3 and n4 ( with his )
35 $c->merge_readings( 'n3', 'n4', 1 );
36 ok( !$c->reading('n4'), "Reading n4 is gone" );
37 is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
38
39 # Collapse n9 and n10 ( rood / root )
40 $c->merge_readings( 'n9', 'n10' );
41 ok( !$c->reading('n10'), "Reading n10 is gone" );
42 is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
43
44 # Combine n21 and n21p0
45 my $remaining = $c->reading('n21');
46 $remaining ||= $c->reading('n22');  # one of these should still exist
47 $c->merge_readings( 'n21p0', $remaining, 1 );
48 ok( !$c->reading('n21'), "Reading $remaining is gone" );
49 is( $c->reading('n21p0')->text, 'unto', "Reading n21p0 merged correctly" );
50 }
51
52
53
54 # =begin testing
55 {
56 use Text::Tradition;
57 use TryCatch;
58
59 my $t = Text::Tradition->new( 
60     'name'  => 'inline', 
61     'input' => 'Self',
62     'file'  => 't/data/legendfrag.xml',
63     );
64 my $c = $t->collation;
65
66 my %rdg_ids;
67 map { $rdg_ids{$_} = 1 } $c->readings;
68 $c->merge_related( 'orthographic' );
69 is( scalar( $c->readings ), keys( %rdg_ids ) - 8, 
70         "Successfully collapsed orthographic variation" );
71 map { $rdg_ids{$_} = undef } qw/ r13.3 r11.4 r8.5 r8.2 r7.7 r7.5 r7.4 r7.1 /;
72 foreach my $rid ( keys %rdg_ids ) {
73         my $exp = $rdg_ids{$rid};
74         is( !$c->reading( $rid ), !$exp, "Reading $rid correctly " . 
75                 ( $exp ? "retained" : "removed" ) );
76 }
77 ok( $c->linear, "Graph is still linear" );
78 try {
79         $c->calculate_ranks; # This should succeed
80         ok( 1, "Can still calculate ranks on the new graph" );
81 } catch {
82         ok( 0, "Rank calculation on merged graph failed: $@" );
83 }
84
85 # Now add some transpositions
86 $c->add_relationship( 'r8.4', 'r10.4', { type => 'transposition' } );
87 $c->merge_related( 'transposition' );
88 is( scalar( $c->readings ), keys( %rdg_ids ) - 9, 
89         "Transposed relationship is merged away" );
90 ok( !$c->reading('r8.4'), "Correct transposed reading removed" );
91 ok( !$c->linear, "Graph is no longer linear" );
92 try {
93         $c->calculate_ranks; # This should fail
94         ok( 0, "Rank calculation happened on nonlinear graph?!" );
95 } catch ( Text::Tradition::Error $e ) {
96         is( $e->message, 'Cannot calculate ranks on a non-linear graph', 
97                 "Rank calculation on merged graph threw an error" );
98 }
99 }
100
101
102
103 # =begin testing
104 {
105 use Test::More::UTF8;
106 use Text::Tradition;
107 use TryCatch;
108
109 my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
110 is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
111 ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
112
113 my $sc = $st->collation;
114 my $numr = 17;
115 ok( $sc->reading('n131'), "Tradition has the affected reading" );
116 is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
117 is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
118
119 # Detach the erroneously collated reading
120 my( $newr, @del_rdgs ) = $sc->duplicate_reading( 'n131', 'Ba96' );
121 ok( $newr, "New reading was created" );
122 ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
123 is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
124 is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
125 my $csucc = $sc->common_successor( 'n131', 'n131_0' );
126 is( $csucc->id, 'n136', "Found correct common successor to duped reading" ); 
127
128 # Check that the bad transposition is gone
129 is( scalar @del_rdgs, 1, "Deleted reading was returned by API call" );
130 is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
131
132 # The collation should not be fixed
133 my @pairs = $sc->identical_readings();
134 is( scalar @pairs, 0, "Not re-collated yet" );
135 # Fix the collation
136 ok( $sc->merge_readings( 'n124', 'n131_0' ), "Collated the readings correctly" );
137 @pairs = $sc->identical_readings( start => 'n124', end => $csucc->id );
138 is( scalar @pairs, 3, "Found three more identical readings" );
139 is( $sc->end->rank, 11, "The ranks shifted appropriately" );
140 $sc->flatten_ranks();
141 is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
142
143 # Check that we can't "duplicate" a reading with no wits or with all wits
144 try {
145         my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124' );
146         ok( 0, "Reading duplication without witnesses throws an error" );
147 } catch( Text::Tradition::Error $e ) {
148         like( $e->message, qr/Must specify one or more witnesses/, 
149                 "Reading duplication without witnesses throws the expected error" );
150 } catch {
151         ok( 0, "Reading duplication without witnesses threw the wrong error" );
152 }
153
154 try {
155         my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124', 'Ba96', 'Mü11475' );
156         ok( 0, "Reading duplication with all witnesses throws an error" );
157 } catch( Text::Tradition::Error $e ) {
158         like( $e->message, qr/Cannot join all witnesses/, 
159                 "Reading duplication with all witnesses throws the expected error" );
160 } catch {
161         ok( 0, "Reading duplication with all witnesses threw the wrong error" );
162 }
163 }
164
165
166
167 # =begin testing
168 {
169 use Text::Tradition;
170 use TryCatch;
171
172 my $READINGS = 311;
173 my $PATHS = 361;
174
175 my $datafile = 't/data/florilegium_tei_ps.xml';
176 my $tradition = Text::Tradition->new( 'input' => 'TEI',
177                                       'name' => 'test0',
178                                       'file' => $datafile,
179                                       'linear' => 1 );
180
181 ok( $tradition, "Got a tradition object" );
182 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
183 ok( $tradition->collation, "Tradition has a collation" );
184
185 my $c = $tradition->collation;
186 is( scalar $c->readings, $READINGS, "Collation has all readings" );
187 is( scalar $c->paths, $PATHS, "Collation has all paths" );
188 is( scalar $c->relationships, 0, "Collation has all relationships" );
189
190 # Add a few relationships
191 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
192 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
193 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
194
195 # Now write it to GraphML and parse it again.
196
197 my $graphml = $c->as_graphml;
198 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
199 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
200 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
201 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
202
203 # Now add a stemma, write to GraphML, and look at the output.
204 SKIP: {
205         skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
206         my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
207         is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
208         is( $tradition->stemmata, 1, "Tradition now has the stemma" );
209         $graphml = $c->as_graphml;
210         like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
211 }
212 }
213
214
215
216 # =begin testing
217 {
218 use Text::Tradition;
219 use Text::CSV;
220
221 my $READINGS = 311;
222 my $PATHS = 361;
223 my $WITS = 13;
224 my $WITAC = 4;
225
226 my $datafile = 't/data/florilegium_tei_ps.xml';
227 my $tradition = Text::Tradition->new( 'input' => 'TEI',
228                                       'name' => 'test0',
229                                       'file' => $datafile,
230                                       'linear' => 1 );
231
232 my $c = $tradition->collation;
233 # Export the thing to CSV
234 my $csvstr = $c->as_csv();
235 # Count the columns
236 my $csv = Text::CSV->new({ sep_char => ',', binary => 1 });
237 my @lines = split(/\n/, $csvstr );
238 ok( $csv->parse( $lines[0] ), "Successfully parsed first line of CSV" );
239 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
240 my @q_ac = grep { $_ eq 'Q'.$c->ac_label } $csv->fields;
241 ok( @q_ac, "Found a layered witness" );
242
243 my $t2 = Text::Tradition->new( input => 'Tabular',
244                                                            name => 'test2',
245                                                            string => $csvstr,
246                                                            sep_char => ',' );
247 is( scalar $t2->collation->readings, $READINGS, "Reparsed CSV collation has all readings" );
248 is( scalar $t2->collation->paths, $PATHS, "Reparsed CSV collation has all paths" );
249
250 # Now do it with TSV
251 my $tsvstr = $c->as_tsv();
252 my $t3 = Text::Tradition->new( input => 'Tabular',
253                                                            name => 'test3',
254                                                            string => $tsvstr,
255                                                            sep_char => "\t" );
256 is( scalar $t3->collation->readings, $READINGS, "Reparsed TSV collation has all readings" );
257 is( scalar $t3->collation->paths, $PATHS, "Reparsed TSV collation has all paths" );
258
259 my $table = $c->alignment_table;
260 my $noaccsv = $c->as_csv({ noac => 1 });
261 my @noaclines = split(/\n/, $noaccsv );
262 ok( $csv->parse( $noaclines[0] ), "Successfully parsed first line of no-ac CSV" );
263 is( scalar( $csv->fields ), $WITS, "CSV has correct number of witness columns" );
264 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
265
266 my $safecsv = $c->as_csv({ safe_ac => 1});
267 my @safelines = split(/\n/, $safecsv );
268 ok( $csv->parse( $safelines[0] ), "Successfully parsed first line of safe CSV" );
269 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
270 @q_ac = grep { $_ eq 'Q__L' } $csv->fields;
271 ok( @q_ac, "Found a sanitized layered witness" );
272 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
273 }
274
275
276
277 # =begin testing
278 {
279 use Text::Tradition;
280
281 my $cxfile = 't/data/Collatex-16.xml';
282 my $t = Text::Tradition->new( 
283     'name'  => 'inline', 
284     'input' => 'CollateX',
285     'file'  => $cxfile,
286     );
287 my $c = $t->collation;
288
289 # Make an svg
290 my $table = $c->alignment_table;
291 ok( $c->has_cached_table, "Alignment table was cached" );
292 is( $c->alignment_table, $table, "Cached table returned upon second call" );
293 $c->calculate_ranks;
294 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
295 $c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
296 is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
297 $c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
298 isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
299 }
300
301
302
303 # =begin testing
304 {
305 use Text::Tradition;
306
307 my $cxfile = 't/data/Collatex-16.xml';
308 my $t = Text::Tradition->new( 
309     'name'  => 'inline', 
310     'input' => 'CollateX',
311     'file'  => $cxfile,
312     );
313 my $c = $t->collation;
314
315 my @common = $c->calculate_common_readings();
316 is( scalar @common, 8, "Found correct number of common readings" );
317 my @marked = sort $c->common_readings();
318 is( scalar @common, 8, "All common readings got marked as such" );
319 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
320 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
321 }
322
323
324
325 # =begin testing
326 {
327 use Text::Tradition;
328
329 my $cxfile = 't/data/Collatex-16.xml';
330 my $t = Text::Tradition->new( 
331     'name'  => 'inline', 
332     'input' => 'CollateX',
333     'file'  => $cxfile,
334     );
335 my $c = $t->collation;
336
337 is( $c->common_predecessor( 'n24', 'n23' )->id, 
338     'n20', "Found correct common predecessor" );
339 is( $c->common_successor( 'n24', 'n23' )->id, 
340     '__END__', "Found correct common successor" );
341
342 is( $c->common_predecessor( 'n19', 'n17' )->id, 
343     'n16', "Found correct common predecessor for readings on same path" );
344 is( $c->common_successor( 'n21', 'n10' )->id, 
345     '__END__', "Found correct common successor for readings on same path" );
346 }
347
348
349
350
351 1;