Add option to put sanitized layer labels in tabular output. Fixes #13 again
[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
58 my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
59 is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
60 ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
61
62 my $sc = $st->collation;
63 my $numr = 17;
64 ok( $sc->reading('n131'), "Tradition has the affected reading" );
65 is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
66 is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
67
68 # Detach the erroneously collated reading
69 my( $newr, @del_rdgs ) = $sc->duplicate_reading( 'n131', 'Ba96' );
70 ok( $newr, "New reading was created" );
71 ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
72 is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
73 is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
74 my $csucc = $sc->common_successor( 'n131', 'n131_0' );
75 is( $csucc->id, 'n136', "Found correct common successor to duped reading" ); 
76
77 # Check that the bad transposition is gone
78 is( scalar @del_rdgs, 1, "Deleted reading was returned by API call" );
79 is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
80
81 # The collation should not be fixed
82 my @pairs = $sc->identical_readings();
83 is( scalar @pairs, 0, "Not re-collated yet" );
84 # Fix the collation
85 ok( $sc->merge_readings( 'n124', 'n131_0' ), "Collated the readings correctly" );
86 @pairs = $sc->identical_readings( start => 'n124', end => $csucc->id );
87 is( scalar @pairs, 3, "Found three more identical readings" );
88 is( $sc->end->rank, 11, "The ranks shifted appropriately" );
89 $sc->flatten_ranks();
90 is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
91 }
92
93
94
95 # =begin testing
96 {
97 use Text::Tradition;
98 use TryCatch;
99
100 my $READINGS = 311;
101 my $PATHS = 361;
102
103 my $datafile = 't/data/florilegium_tei_ps.xml';
104 my $tradition = Text::Tradition->new( 'input' => 'TEI',
105                                       'name' => 'test0',
106                                       'file' => $datafile,
107                                       'linear' => 1 );
108
109 ok( $tradition, "Got a tradition object" );
110 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
111 ok( $tradition->collation, "Tradition has a collation" );
112
113 my $c = $tradition->collation;
114 is( scalar $c->readings, $READINGS, "Collation has all readings" );
115 is( scalar $c->paths, $PATHS, "Collation has all paths" );
116 is( scalar $c->relationships, 0, "Collation has all relationships" );
117
118 # Add a few relationships
119 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
120 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
121 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
122
123 # Now write it to GraphML and parse it again.
124
125 my $graphml = $c->as_graphml;
126 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
127 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
128 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
129 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
130
131 # Now add a stemma, write to GraphML, and look at the output.
132 SKIP: {
133         skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
134         my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
135         is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
136         is( $tradition->stemmata, 1, "Tradition now has the stemma" );
137         $graphml = $c->as_graphml;
138         like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
139 }
140 }
141
142
143
144 # =begin testing
145 {
146 use Text::Tradition;
147 use Text::CSV;
148
149 my $READINGS = 311;
150 my $PATHS = 361;
151 my $WITS = 13;
152 my $WITAC = 4;
153
154 my $datafile = 't/data/florilegium_tei_ps.xml';
155 my $tradition = Text::Tradition->new( 'input' => 'TEI',
156                                       'name' => 'test0',
157                                       'file' => $datafile,
158                                       'linear' => 1 );
159
160 my $c = $tradition->collation;
161 # Export the thing to CSV
162 my $csvstr = $c->as_csv();
163 # Count the columns
164 my $csv = Text::CSV->new({ sep_char => ',', binary => 1 });
165 my @lines = split(/\n/, $csvstr );
166 ok( $csv->parse( $lines[0] ), "Successfully parsed first line of CSV" );
167 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
168 my @q_ac = grep { $_ eq 'Q'.$c->ac_label } $csv->fields;
169 ok( @q_ac, "Found a layered witness" );
170
171 my $t2 = Text::Tradition->new( input => 'Tabular',
172                                                            name => 'test2',
173                                                            string => $csvstr,
174                                                            sep_char => ',' );
175 is( scalar $t2->collation->readings, $READINGS, "Reparsed CSV collation has all readings" );
176 is( scalar $t2->collation->paths, $PATHS, "Reparsed CSV collation has all paths" );
177
178 # Now do it with TSV
179 my $tsvstr = $c->as_tsv();
180 my $t3 = Text::Tradition->new( input => 'Tabular',
181                                                            name => 'test3',
182                                                            string => $tsvstr,
183                                                            sep_char => "\t" );
184 is( scalar $t3->collation->readings, $READINGS, "Reparsed TSV collation has all readings" );
185 is( scalar $t3->collation->paths, $PATHS, "Reparsed TSV collation has all paths" );
186
187 my $table = $c->alignment_table;
188 my $noaccsv = $c->as_csv({ noac => 1 });
189 my @noaclines = split(/\n/, $noaccsv );
190 ok( $csv->parse( $noaclines[0] ), "Successfully parsed first line of no-ac CSV" );
191 is( scalar( $csv->fields ), $WITS, "CSV has correct number of witness columns" );
192 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
193
194 my $safecsv = $c->as_csv({ safe_ac => 1});
195 my @safelines = split(/\n/, $safecsv );
196 ok( $csv->parse( $safelines[0] ), "Successfully parsed first line of safe CSV" );
197 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
198 @q_ac = grep { $_ eq 'Q__L' } $csv->fields;
199 ok( @q_ac, "Found a sanitized layered witness" );
200 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
201 }
202
203
204
205 # =begin testing
206 {
207 use Text::Tradition;
208
209 my $cxfile = 't/data/Collatex-16.xml';
210 my $t = Text::Tradition->new( 
211     'name'  => 'inline', 
212     'input' => 'CollateX',
213     'file'  => $cxfile,
214     );
215 my $c = $t->collation;
216
217 # Make an svg
218 my $table = $c->alignment_table;
219 ok( $c->has_cached_table, "Alignment table was cached" );
220 is( $c->alignment_table, $table, "Cached table returned upon second call" );
221 $c->calculate_ranks;
222 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
223 $c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
224 is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
225 $c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
226 isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
227 }
228
229
230
231 # =begin testing
232 {
233 use Text::Tradition;
234
235 my $cxfile = 't/data/Collatex-16.xml';
236 my $t = Text::Tradition->new( 
237     'name'  => 'inline', 
238     'input' => 'CollateX',
239     'file'  => $cxfile,
240     );
241 my $c = $t->collation;
242
243 my @common = $c->calculate_common_readings();
244 is( scalar @common, 8, "Found correct number of common readings" );
245 my @marked = sort $c->common_readings();
246 is( scalar @common, 8, "All common readings got marked as such" );
247 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
248 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
249 }
250
251
252
253 # =begin testing
254 {
255 use Text::Tradition;
256
257 my $cxfile = 't/data/Collatex-16.xml';
258 my $t = Text::Tradition->new( 
259     'name'  => 'inline', 
260     'input' => 'CollateX',
261     'file'  => $cxfile,
262     );
263 my $c = $t->collation;
264
265 is( $c->common_predecessor( 'n24', 'n23' )->id, 
266     'n20', "Found correct common predecessor" );
267 is( $c->common_successor( 'n24', 'n23' )->id, 
268     '__END__', "Found correct common successor" );
269
270 is( $c->common_predecessor( 'n19', 'n17' )->id, 
271     'n16', "Found correct common predecessor for readings on same path" );
272 is( $c->common_successor( 'n21', 'n10' )->id, 
273     '__END__', "Found correct common successor for readings on same path" );
274 }
275
276
277
278
279 1;