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