add method to duplicate a given relationship
[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' );
29$c->flatten_ranks();
30ok( $c->reading( 'n21p0' ), "New reading exists" );
31is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
32
679f17e1 33# Combine n3 and n4 ( with his )
4e483aa5 34$c->merge_readings( 'n3', 'n4', 1 );
35ok( !$c->reading('n4'), "Reading n4 is gone" );
36is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
37
679f17e1 38# Collapse n9 and n10 ( rood / root )
39$c->merge_readings( 'n9', 'n10' );
40ok( !$c->reading('n10'), "Reading n10 is gone" );
41is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
4e483aa5 42
43# Combine n21 and n21p0
44my $remaining = $c->reading('n21');
45$remaining ||= $c->reading('n22'); # one of these should still exist
46$c->merge_readings( 'n21p0', $remaining, 1 );
47ok( !$c->reading('n21'), "Reading $remaining is gone" );
48is( $c->reading('n21p0')->text, 'unto', "Reading n21p0 merged correctly" );
49}
50
51
52
53# =begin testing
54{
55use Text::Tradition;
f97ef19e 56
57my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
58is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
59ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
60
61my $sc = $st->collation;
62my $numr = 17;
63ok( $sc->reading('n131'), "Tradition has the affected reading" );
64is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
65is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
66
67# Detach the erroneously collated reading
68$sc->duplicate_reading( 'n131', 'Ba96' );
69ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
70is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
71is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
72
73# Check that the bad transposition is gone
74is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
75
76# Fix the collation
77ok( $sc->add_relationship( 'n124', 'n131_0', { type => 'collated', scope => 'local' } ),
78 "Collated the readings correctly" );
79$sc->calculate_ranks();
80$sc->flatten_ranks();
81is( $sc->end->rank, 11, "The ranks shifted appropriately" );
82is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
83}
84
85
86
87# =begin testing
88{
89use Text::Tradition;
951ddfe8 90use TryCatch;
4e483aa5 91
56eefa04 92my $READINGS = 311;
93my $PATHS = 361;
94
95my $datafile = 't/data/florilegium_tei_ps.xml';
96my $tradition = Text::Tradition->new( 'input' => 'TEI',
97 'name' => 'test0',
98 'file' => $datafile,
99 'linear' => 1 );
100
101ok( $tradition, "Got a tradition object" );
102is( scalar $tradition->witnesses, 13, "Found all witnesses" );
103ok( $tradition->collation, "Tradition has a collation" );
104
105my $c = $tradition->collation;
106is( scalar $c->readings, $READINGS, "Collation has all readings" );
107is( scalar $c->paths, $PATHS, "Collation has all paths" );
108is( scalar $c->relationships, 0, "Collation has all relationships" );
109
110# Add a few relationships
111$c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
112$c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
113$c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
114
115# Now write it to GraphML and parse it again.
116
117my $graphml = $c->as_graphml;
118my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
119is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
120is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
121is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
2a812726 122
9fef629b 123# Now add a stemma, write to GraphML, and look at the output.
951ddfe8 124SKIP: {
37bf09f4 125 skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
951ddfe8 126 my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
127 is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
128 is( $tradition->stemmata, 1, "Tradition now has the stemma" );
129 $graphml = $c->as_graphml;
130 like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
131}
56eefa04 132}
133
134
135
136# =begin testing
137{
138use Text::Tradition;
139
0e47f4f6 140my $cxfile = 't/data/Collatex-16.xml';
141my $t = Text::Tradition->new(
142 'name' => 'inline',
143 'input' => 'CollateX',
144 'file' => $cxfile,
145 );
146my $c = $t->collation;
4633f9e4 147
b365fbae 148# Make an svg
bfcbcecb 149my $table = $c->alignment_table;
150ok( $c->has_cached_table, "Alignment table was cached" );
151is( $c->alignment_table, $table, "Cached table returned upon second call" );
b365fbae 152$c->calculate_ranks;
bfcbcecb 153is( $c->alignment_table, $table, "Cached table retained with no rank change" );
864ee4bf 154$c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
155is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
156$c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
157isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
b365fbae 158}
159
160
161
162# =begin testing
163{
164use Text::Tradition;
165
166my $cxfile = 't/data/Collatex-16.xml';
167my $t = Text::Tradition->new(
168 'name' => 'inline',
169 'input' => 'CollateX',
170 'file' => $cxfile,
171 );
172my $c = $t->collation;
0e47f4f6 173
d4b75f44 174my @common = $c->calculate_common_readings();
175is( scalar @common, 8, "Found correct number of common readings" );
176my @marked = sort $c->common_readings();
177is( scalar @common, 8, "All common readings got marked as such" );
679f17e1 178my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
d4b75f44 179is_deeply( \@marked, \@expected, "Found correct list of common readings" );
180}
181
182
183
184# =begin testing
185{
186use Text::Tradition;
187
188my $cxfile = 't/data/Collatex-16.xml';
189my $t = Text::Tradition->new(
190 'name' => 'inline',
191 'input' => 'CollateX',
192 'file' => $cxfile,
193 );
194my $c = $t->collation;
195
679f17e1 196is( $c->common_predecessor( 'n24', 'n23' )->id,
0e47f4f6 197 'n20', "Found correct common predecessor" );
679f17e1 198is( $c->common_successor( 'n24', 'n23' )->id,
10e4b1ac 199 '__END__', "Found correct common successor" );
0e47f4f6 200
4e5a7b2c 201is( $c->common_predecessor( 'n19', 'n17' )->id,
0e47f4f6 202 'n16', "Found correct common predecessor for readings on same path" );
679f17e1 203is( $c->common_successor( 'n21', 'n10' )->id,
10e4b1ac 204 '__END__', "Found correct common successor for readings on same path" );
0e47f4f6 205}
206
207
208
209
2101;