pass through args from flatten_ranks to identical_readings
[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;
22# Split n21 for testing purposes
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
ef73c20a 69my $newr = $sc->duplicate_reading( 'n131', 'Ba96' );
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" );
74
75# Check that the bad transposition is gone
76is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
77
78# Fix the collation
79ok( $sc->add_relationship( 'n124', 'n131_0', { type => 'collated', scope => 'local' } ),
80 "Collated the readings correctly" );
81$sc->calculate_ranks();
82$sc->flatten_ranks();
83is( $sc->end->rank, 11, "The ranks shifted appropriately" );
84is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
85}
86
87
88
89# =begin testing
90{
91use Text::Tradition;
951ddfe8 92use TryCatch;
4e483aa5 93
56eefa04 94my $READINGS = 311;
95my $PATHS = 361;
96
97my $datafile = 't/data/florilegium_tei_ps.xml';
98my $tradition = Text::Tradition->new( 'input' => 'TEI',
99 'name' => 'test0',
100 'file' => $datafile,
101 'linear' => 1 );
102
103ok( $tradition, "Got a tradition object" );
104is( scalar $tradition->witnesses, 13, "Found all witnesses" );
105ok( $tradition->collation, "Tradition has a collation" );
106
107my $c = $tradition->collation;
108is( scalar $c->readings, $READINGS, "Collation has all readings" );
109is( scalar $c->paths, $PATHS, "Collation has all paths" );
110is( scalar $c->relationships, 0, "Collation has all relationships" );
111
112# Add a few relationships
113$c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
114$c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
115$c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
116
117# Now write it to GraphML and parse it again.
118
119my $graphml = $c->as_graphml;
120my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
121is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
122is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
123is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
2a812726 124
9fef629b 125# Now add a stemma, write to GraphML, and look at the output.
951ddfe8 126SKIP: {
37bf09f4 127 skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
951ddfe8 128 my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
129 is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
130 is( $tradition->stemmata, 1, "Tradition now has the stemma" );
131 $graphml = $c->as_graphml;
132 like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
133}
56eefa04 134}
135
136
137
138# =begin testing
139{
140use Text::Tradition;
141
0e47f4f6 142my $cxfile = 't/data/Collatex-16.xml';
143my $t = Text::Tradition->new(
144 'name' => 'inline',
145 'input' => 'CollateX',
146 'file' => $cxfile,
147 );
148my $c = $t->collation;
4633f9e4 149
b365fbae 150# Make an svg
bfcbcecb 151my $table = $c->alignment_table;
152ok( $c->has_cached_table, "Alignment table was cached" );
153is( $c->alignment_table, $table, "Cached table returned upon second call" );
b365fbae 154$c->calculate_ranks;
bfcbcecb 155is( $c->alignment_table, $table, "Cached table retained with no rank change" );
864ee4bf 156$c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
157is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
158$c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
159isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
b365fbae 160}
161
162
163
164# =begin testing
165{
166use Text::Tradition;
167
168my $cxfile = 't/data/Collatex-16.xml';
169my $t = Text::Tradition->new(
170 'name' => 'inline',
171 'input' => 'CollateX',
172 'file' => $cxfile,
173 );
174my $c = $t->collation;
0e47f4f6 175
d4b75f44 176my @common = $c->calculate_common_readings();
177is( scalar @common, 8, "Found correct number of common readings" );
178my @marked = sort $c->common_readings();
179is( scalar @common, 8, "All common readings got marked as such" );
679f17e1 180my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
d4b75f44 181is_deeply( \@marked, \@expected, "Found correct list of common readings" );
182}
183
184
185
186# =begin testing
187{
188use Text::Tradition;
189
190my $cxfile = 't/data/Collatex-16.xml';
191my $t = Text::Tradition->new(
192 'name' => 'inline',
193 'input' => 'CollateX',
194 'file' => $cxfile,
195 );
196my $c = $t->collation;
197
679f17e1 198is( $c->common_predecessor( 'n24', 'n23' )->id,
0e47f4f6 199 'n20', "Found correct common predecessor" );
679f17e1 200is( $c->common_successor( 'n24', 'n23' )->id,
10e4b1ac 201 '__END__', "Found correct common successor" );
0e47f4f6 202
4e5a7b2c 203is( $c->common_predecessor( 'n19', 'n17' )->id,
0e47f4f6 204 'n16', "Found correct common predecessor for readings on same path" );
679f17e1 205is( $c->common_successor( 'n21', 'n10' )->id,
10e4b1ac 206 '__END__', "Found correct common successor for readings on same path" );
0e47f4f6 207}
208
209
210
211
2121;