1 package Text::Tradition;
5 use Text::Tradition::Collation;
6 use Text::Tradition::Stemma;
7 use Text::Tradition::Witness;
9 use vars qw( $VERSION );
14 isa => 'Text::Tradition::Collation',
15 writer => '_save_collation',
18 has 'witness_hash' => (
20 isa => 'HashRef[Text::Tradition::Witness]',
24 del_witness => 'delete',
25 has_witness => 'exists',
26 witnesses => 'values',
28 default => sub { {} },
34 default => 'Tradition',
39 isa => 'Text::Tradition::Stemma',
40 writer => '_add_stemma',
41 predicate => 'has_stemma',
44 # Create the witness before trying to add it
45 around 'add_witness' => sub {
48 # TODO allow add of a Witness object?
49 my $new_wit = Text::Tradition::Witness->new( @_ );
50 $self->$orig( $new_wit->sigil => $new_wit );
54 # Allow deletion of witness by object as well as by sigil
55 around 'del_witness' => sub {
59 foreach my $arg ( @_ ) {
61 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
63 return $self->$orig( @key_args );
66 # Don't allow an empty hash value
67 around 'witness' => sub {
68 my( $orig, $self, $arg ) = @_;
69 return unless $self->has_witness( $arg );
70 return $self->$orig( $arg );
75 Text::Tradition - a software model for a set of collated texts
80 my $t = Text::Tradition->new(
81 'name' => 'this is a text',
83 'file' => '/path/to/tei_parallel_seg_file.xml' );
85 my @text_wits = $t->witnesses();
86 my $manuscript_a = $t->witness( 'A' );
87 my $new_ms = $t->add_witness( 'sigil' => 'B' );
89 my $text_path_svg = $t->collation->as_svg();
90 ## See Text::Tradition::Collation for more on text collation itself
94 Text::Tradition is a library for representation and analysis of collated
95 texts, particularly medieval ones. A 'tradition' refers to the aggregation
96 of surviving versions of a text, generally preserved in multiple
97 manuscripts (or 'witnesses'). A Tradition object thus has one more more
98 Witnesses, as well as a Collation that represents the unity of all versions
105 Creates and returns a new text tradition object. The following options are
112 =item B<name> - The name of the text.
116 Initialization based on a collation file:
120 =item B<input> - The input format of the collation file. Can be one of the
125 =item * Self - a GraphML format produced by this module
127 =item * CollateX - a GraphML format produced by CollateX
129 =item * CTE - a TEI XML format produced by Classical Text Editor
131 =item * KUL - a specific CSV format for variants, not documented here
133 =item * TEI - a TEI parallel segmentation format file
135 =item * Tabular - a comma- or tab-separated collation. Takes an additional
136 option, 'sep_char', which defaults to the tab character.
140 =item B<file> - The name of the file that contains the data. One of 'file'
141 or 'string' should be specified.
143 =item B<string> - A text string that contains the data. One of 'file' or
144 'string' should be specified.
146 =item B<base> - The name of a text file that contains the base text, to be
147 used with input formats that require it (currently only KUL).
151 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
155 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
156 objects that carry the text to be collated.
158 =item B<collator> - A reference to a collation program that will accept
165 Return the Text::Tradition::Witness objects associated with this tradition,
168 =head2 B<witness>( $sigil )
170 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
171 if there is no such object within the tradition.
173 =head2 B<add_witness>( %opts )
175 Instantiate a new witness with the given options (see documentation for
176 Text::Tradition::Witness) and add it to the tradition.
178 =head2 B<del_witness>( $sigil )
180 Delete the witness with the given sigil from the tradition. Returns the
181 witness object for the deleted witness.
185 use_ok( 'Text::Tradition', "can use module" );
187 my $t = Text::Tradition->new( 'name' => 'empty' );
188 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
189 is( $t->name, 'empty', "object has the right name" );
190 is( scalar $t->witnesses, 0, "object has no witnesses" );
192 my $simple = 't/data/simple.txt';
193 my $s = Text::Tradition->new(
195 'input' => 'Tabular',
198 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
199 is( $s->name, 'inline', "object has the right name" );
200 is( scalar $s->witnesses, 3, "object has three witnesses" );
202 my $wit_a = $s->witness('A');
203 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
205 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
207 is( $s->witness('X'), undef, "There is no witness X" );
208 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
210 my $wit_d = $s->add_witness( 'sigil' => 'D' );
211 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
212 is( $wit_d->sigil, 'D', "witness has correct sigil" );
213 is( scalar $s->witnesses, 4, "object now has four witnesses" );
215 my $del = $s->del_witness( 'D' );
216 is( $del, $wit_d, "Deleted correct witness" );
217 is( scalar $s->witnesses, 3, "object has three witnesses again" );
219 # TODO test initialization by witness list when we have it
227 my( $self, $init_args ) = @_;
229 if( exists $init_args->{'witnesses'} ) {
230 # We got passed an uncollated list of witnesses. Make a
231 # witness object for each witness, and then send them to the
234 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
235 # Each item in the list is either a string or an arrayref.
236 # If it's a string, it is a filename; if it's an arrayref,
237 # it is a tuple of 'sigil, file'. Handle either case.
239 if( ref( $wit ) eq 'ARRAY' ) {
240 $args = { 'sigil' => $wit->[0],
241 'file' => $wit->[1] };
243 $args = { 'sigil' => chr( $autosigil+65 ),
247 $self->witnesses->add_witness( $args );
248 # TODO Now how to collate these?
251 # Else we need to parse some collation data. Make a Collation object
252 my $collation = Text::Tradition::Collation->new( %$init_args,
253 'tradition' => $self );
254 $self->_save_collation( $collation );
256 # Call the appropriate parser on the given data
257 my @format_standalone = qw/ Self CollateText CollateX CTE TEI Tabular /;
258 my @format_basetext = qw/ KUL /;
260 my $format = $init_args->{'input'};
261 if( $format && !( grep { $_ eq $format } @format_standalone )
262 && !( grep { $_ eq $format } @format_basetext ) ) {
263 warn "Unrecognized input format $format; not parsing";
266 if( $format && grep { $_ eq $format } @format_basetext ) {
268 if( !exists $init_args->{'base'} ) {
269 warn "Cannot make a collation from $format without a base text";
274 # Now do the parsing.
277 $format = 'BaseText'; # Use the BaseText module for parsing,
278 # but retain the original input arg.
280 my $mod = "Text::Tradition::Parser::$format";
282 $mod->can('parse')->( $self, $init_args );
287 =head2 add_stemma( $dotfile )
289 Initializes a Text::Tradition::Stemma object from the given dotfile,
290 and associates it with the tradition.
296 my $t = Text::Tradition->new(
297 'name' => 'simple test',
298 'input' => 'Tabular',
299 'file' => 't/data/simple.txt',
303 ok( $s = $t->add_stemma( 't/data/simple.dot' ), "Added a simple stemma" );
304 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
305 is( $t->stemma, $s, "Stemma is the right one" );
312 my( $self, $dot ) = @_;
313 open my $stemma_fh, '<', $dot or warn "Could not open file $dot";
314 my $stemma = Text::Tradition::Stemma->new(
315 'collation' => $self->collation,
316 'dot' => $stemma_fh );
317 $self->_add_stemma( $stemma ) if $stemma;
322 __PACKAGE__->meta->make_immutable;
329 =item * Allow tradition to be initialized via passing to a collator.
335 This package is free software and is provided "as is" without express
336 or implied warranty. You can redistribute it and/or modify it under
337 the same terms as Perl itself.
341 Tara L Andrews E<lt>aurum@cpan.orgE<gt>