enable output of CSV without witness layers. Fixes #13
[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;
12
4e483aa5 13my $cxfile = 't/data/Collatex-16.xml';
14my $t = Text::Tradition->new(
15 'name' => 'inline',
16 'input' => 'CollateX',
17 'file' => $cxfile,
18 );
19my $c = $t->collation;
20
21my $rno = scalar $c->readings;
3c234eb6 22# Split n21 ('unto') for testing purposes
4e483aa5 23my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
24my $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' );
7a0956c1 29$c->add_relationship( 'n21', 'n22', { type => 'collated', scope => 'local' } );
4e483aa5 30$c->flatten_ranks();
31ok( $c->reading( 'n21p0' ), "New reading exists" );
32is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
33
679f17e1 34# Combine n3 and n4 ( with his )
4e483aa5 35$c->merge_readings( 'n3', 'n4', 1 );
36ok( !$c->reading('n4'), "Reading n4 is gone" );
37is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
38
679f17e1 39# Collapse n9 and n10 ( rood / root )
40$c->merge_readings( 'n9', 'n10' );
41ok( !$c->reading('n10'), "Reading n10 is gone" );
42is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
4e483aa5 43
44# Combine n21 and n21p0
45my $remaining = $c->reading('n21');
46$remaining ||= $c->reading('n22'); # one of these should still exist
47$c->merge_readings( 'n21p0', $remaining, 1 );
48ok( !$c->reading('n21'), "Reading $remaining is gone" );
49is( $c->reading('n21p0')->text, 'unto', "Reading n21p0 merged correctly" );
50}
51
52
53
54# =begin testing
55{
56use Text::Tradition;
f97ef19e 57
58my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
59is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
60ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
61
62my $sc = $st->collation;
63my $numr = 17;
64ok( $sc->reading('n131'), "Tradition has the affected reading" );
65is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
66is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
67
68# Detach the erroneously collated reading
2dcb5d11 69my( $newr, @del_rdgs ) = $sc->duplicate_reading( 'n131', 'Ba96' );
ef73c20a 70ok( $newr, "New reading was created" );
f97ef19e 71ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
72is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
73is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
3c234eb6 74my $csucc = $sc->common_successor( 'n131', 'n131_0' );
75is( $csucc->id, 'n136', "Found correct common successor to duped reading" );
f97ef19e 76
77# Check that the bad transposition is gone
2dcb5d11 78is( scalar @del_rdgs, 1, "Deleted reading was returned by API call" );
f97ef19e 79is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
80
e19635f8 81# The collation should not be fixed
82my @pairs = $sc->identical_readings();
83is( scalar @pairs, 0, "Not re-collated yet" );
f97ef19e 84# Fix the collation
3c234eb6 85ok( $sc->merge_readings( 'n124', 'n131_0' ), "Collated the readings correctly" );
e19635f8 86@pairs = $sc->identical_readings( start => 'n124', end => $csucc->id );
3c234eb6 87is( scalar @pairs, 3, "Found three more identical readings" );
e19635f8 88is( $sc->end->rank, 11, "The ranks shifted appropriately" );
3c234eb6 89$sc->flatten_ranks();
f97ef19e 90is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
91}
92
93
94
95# =begin testing
96{
97use Text::Tradition;
951ddfe8 98use TryCatch;
4e483aa5 99
56eefa04 100my $READINGS = 311;
101my $PATHS = 361;
102
103my $datafile = 't/data/florilegium_tei_ps.xml';
104my $tradition = Text::Tradition->new( 'input' => 'TEI',
105 'name' => 'test0',
106 'file' => $datafile,
107 'linear' => 1 );
108
109ok( $tradition, "Got a tradition object" );
110is( scalar $tradition->witnesses, 13, "Found all witnesses" );
111ok( $tradition->collation, "Tradition has a collation" );
112
113my $c = $tradition->collation;
114is( scalar $c->readings, $READINGS, "Collation has all readings" );
115is( scalar $c->paths, $PATHS, "Collation has all paths" );
116is( 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
125my $graphml = $c->as_graphml;
126my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
127is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
128is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
129is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
2a812726 130
9fef629b 131# Now add a stemma, write to GraphML, and look at the output.
951ddfe8 132SKIP: {
37bf09f4 133 skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
951ddfe8 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}
56eefa04 140}
141
142
143
144# =begin testing
145{
16203db5 146use Text::Tradition;
34ca808b 147use Text::CSV;
16203db5 148
149my $READINGS = 311;
150my $PATHS = 361;
34ca808b 151my $WITS = 13;
152my $WITAC = 4;
16203db5 153
154my $datafile = 't/data/florilegium_tei_ps.xml';
155my $tradition = Text::Tradition->new( 'input' => 'TEI',
156 'name' => 'test0',
157 'file' => $datafile,
158 'linear' => 1 );
159
160my $c = $tradition->collation;
161# Export the thing to CSV
162my $csvstr = $c->as_csv();
34ca808b 163# Count the columns
164my $csv = Text::CSV->new({ sep_char => ',', binary => 1 });
165my @lines = split(/\n/, $csvstr );
166ok( $csv->parse( $lines[0] ), "Successfully parsed first line of CSV" );
167is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
16203db5 168my $t2 = Text::Tradition->new( input => 'Tabular',
169 name => 'test2',
170 string => $csvstr,
171 sep_char => ',' );
172is( scalar $t2->collation->readings, $READINGS, "Reparsed CSV collation has all readings" );
173is( scalar $t2->collation->paths, $PATHS, "Reparsed CSV collation has all paths" );
174
175# Now do it with TSV
176my $tsvstr = $c->as_tsv();
177my $t3 = Text::Tradition->new( input => 'Tabular',
178 name => 'test3',
179 string => $tsvstr,
180 sep_char => "\t" );
181is( scalar $t3->collation->readings, $READINGS, "Reparsed TSV collation has all readings" );
182is( scalar $t3->collation->paths, $PATHS, "Reparsed TSV collation has all paths" );
34ca808b 183
184my $noaccsv = $c->as_csv({ noac => 1 });
185my @noaclines = split(/\n/, $noaccsv );
186ok( $csv->parse( $noaclines[0] ), "Successfully parsed first line of no-ac CSV" );
187is( scalar( $csv->fields ), $WITS, "CSV has correct number of witness columns" );
16203db5 188}
189
190
191
192# =begin testing
193{
56eefa04 194use Text::Tradition;
195
0e47f4f6 196my $cxfile = 't/data/Collatex-16.xml';
197my $t = Text::Tradition->new(
198 'name' => 'inline',
199 'input' => 'CollateX',
200 'file' => $cxfile,
201 );
202my $c = $t->collation;
4633f9e4 203
b365fbae 204# Make an svg
bfcbcecb 205my $table = $c->alignment_table;
206ok( $c->has_cached_table, "Alignment table was cached" );
207is( $c->alignment_table, $table, "Cached table returned upon second call" );
b365fbae 208$c->calculate_ranks;
bfcbcecb 209is( $c->alignment_table, $table, "Cached table retained with no rank change" );
864ee4bf 210$c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
211is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
212$c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
213isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
b365fbae 214}
215
216
217
218# =begin testing
219{
220use Text::Tradition;
221
222my $cxfile = 't/data/Collatex-16.xml';
223my $t = Text::Tradition->new(
224 'name' => 'inline',
225 'input' => 'CollateX',
226 'file' => $cxfile,
227 );
228my $c = $t->collation;
0e47f4f6 229
d4b75f44 230my @common = $c->calculate_common_readings();
231is( scalar @common, 8, "Found correct number of common readings" );
232my @marked = sort $c->common_readings();
233is( scalar @common, 8, "All common readings got marked as such" );
679f17e1 234my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
d4b75f44 235is_deeply( \@marked, \@expected, "Found correct list of common readings" );
236}
237
238
239
240# =begin testing
241{
242use Text::Tradition;
243
244my $cxfile = 't/data/Collatex-16.xml';
245my $t = Text::Tradition->new(
246 'name' => 'inline',
247 'input' => 'CollateX',
248 'file' => $cxfile,
249 );
250my $c = $t->collation;
251
679f17e1 252is( $c->common_predecessor( 'n24', 'n23' )->id,
0e47f4f6 253 'n20', "Found correct common predecessor" );
679f17e1 254is( $c->common_successor( 'n24', 'n23' )->id,
10e4b1ac 255 '__END__', "Found correct common successor" );
0e47f4f6 256
4e5a7b2c 257is( $c->common_predecessor( 'n19', 'n17' )->id,
0e47f4f6 258 'n16', "Found correct common predecessor for readings on same path" );
679f17e1 259is( $c->common_successor( 'n21', 'n10' )->id,
10e4b1ac 260 '__END__', "Found correct common successor for readings on same path" );
0e47f4f6 261}
262
263
264
265
2661;