1 package Text::Tradition;
5 use Text::Tradition::Collation;
6 use Text::Tradition::Witness;
8 use vars qw( $VERSION );
13 isa => 'Text::Tradition::Collation',
14 writer => '_save_collation',
17 has 'witness_hash' => (
19 isa => 'HashRef[Text::Tradition::Witness]',
23 del_witness => 'delete',
24 has_witness => 'exists',
25 witnesses => 'values',
27 default => sub { {} },
33 default => 'Tradition',
36 # Create the witness before trying to add it
37 around 'add_witness' => sub {
40 # TODO allow add of a Witness object?
41 my $new_wit = Text::Tradition::Witness->new( @_ );
42 $self->$orig( $new_wit->sigil => $new_wit );
46 # Allow deletion of witness by object as well as by sigil
47 around 'del_witness' => sub {
51 foreach my $arg ( @_ ) {
53 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
55 return $self->$orig( @key_args );
58 # Don't allow an empty hash value
59 around 'witness' => sub {
60 my( $orig, $self, $arg ) = @_;
61 return unless $self->has_witness( $arg );
62 return $self->$orig( $arg );
67 Text::Tradition - a software model for a set of collated texts
72 my $t = Text::Tradition->new(
73 'name' => 'this is a text',
75 'file' => '/path/to/tei_parallel_seg_file.xml' );
77 my @text_wits = $t->witnesses();
78 my $manuscript_a = $t->witness( 'A' );
79 my $new_ms = $t->add_witness( 'sigil' => 'B' );
81 my $text_path_svg = $t->collation->as_svg();
82 ## See Text::Tradition::Collation for more on text collation itself
86 Text::Tradition is a library for representation and analysis of collated
87 texts, particularly medieval ones. A 'tradition' refers to the aggregation
88 of surviving versions of a text, generally preserved in multiple
89 manuscripts (or 'witnesses'). A Tradition object thus has one more more
90 Witnesses, as well as a Collation that represents the unity of all versions
97 Creates and returns a new text tradition object. The following options are
104 =item B<name> - The name of the text.
108 Initialization based on a collation file:
112 =item B<input> - The input format of the collation file. Can be one of the
117 =item * Self - a GraphML format produced by this module
119 =item * CollateX - a GraphML format produced by CollateX
121 =item * CTE - a TEI XML format produced by Classical Text Editor
123 =item * KUL - a specific CSV format for variants, not documented here
125 =item * TEI - a TEI parallel segmentation format file
127 =item * Tabular - a comma- or tab-separated collation. Takes an additional
128 option, 'sep_char', which defaults to the tab character.
132 =item B<file> - The name of the file that contains the data. One of 'file'
133 or 'string' should be specified.
135 =item B<string> - A text string that contains the data. One of 'file' or
136 'string' should be specified.
138 =item B<base> - The name of a text file that contains the base text, to be
139 used with input formats that require it (currently only KUL).
143 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
147 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
148 objects that carry the text to be collated.
150 =item B<collator> - A reference to a collation program that will accept
157 Return the Text::Tradition::Witness objects associated with this tradition,
160 =head2 B<witness>( $sigil )
162 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
163 if there is no such object within the tradition.
165 =head2 B<add_witness>( %opts )
167 Instantiate a new witness with the given options (see documentation for
168 Text::Tradition::Witness) and add it to the tradition.
170 =head2 B<del_witness>( $sigil )
172 Delete the witness with the given sigil from the tradition. Returns the
173 witness object for the deleted witness.
177 use_ok( 'Text::Tradition', "can use module" );
179 my $t = Text::Tradition->new( 'name' => 'empty' );
180 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
181 is( $t->name, 'empty', "object has the right name" );
182 is( scalar $t->witnesses, 0, "object has no witnesses" );
184 my $simple = 't/data/simple.txt';
185 my $s = Text::Tradition->new(
187 'input' => 'Tabular',
190 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
191 is( $s->name, 'inline', "object has the right name" );
192 is( scalar $s->witnesses, 3, "object has three witnesses" );
194 my $wit_a = $s->witness('A');
195 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
197 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
199 is( $s->witness('X'), undef, "There is no witness X" );
200 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
202 my $wit_d = $s->add_witness( 'sigil' => 'D' );
203 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
204 is( $wit_d->sigil, 'D', "witness has correct sigil" );
205 is( scalar $s->witnesses, 4, "object now has four witnesses" );
207 my $del = $s->del_witness( 'D' );
208 is( $del, $wit_d, "Deleted correct witness" );
209 is( scalar $s->witnesses, 3, "object has three witnesses again" );
211 # TODO test initialization by witness list when we have it
219 my( $self, $init_args ) = @_;
221 if( exists $init_args->{'witnesses'} ) {
222 # We got passed an uncollated list of witnesses. Make a
223 # witness object for each witness, and then send them to the
226 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
227 # Each item in the list is either a string or an arrayref.
228 # If it's a string, it is a filename; if it's an arrayref,
229 # it is a tuple of 'sigil, file'. Handle either case.
231 if( ref( $wit ) eq 'ARRAY' ) {
232 $args = { 'sigil' => $wit->[0],
233 'file' => $wit->[1] };
235 $args = { 'sigil' => chr( $autosigil+65 ),
239 $self->witnesses->add_witness( $args );
240 # TODO Now how to collate these?
243 # Else we need to parse some collation data. Make a Collation object
244 my $collation = Text::Tradition::Collation->new( %$init_args,
245 'tradition' => $self );
246 $self->_save_collation( $collation );
248 # Call the appropriate parser on the given data
249 my @format_standalone = qw/ Self CollateX CTE TEI Tabular /;
250 my @format_basetext = qw/ KUL /;
252 my $format = $init_args->{'input'};
253 if( $format && !( grep { $_ eq $format } @format_standalone )
254 && !( grep { $_ eq $format } @format_basetext ) ) {
255 warn "Unrecognized input format $format; not parsing";
258 if( $format && grep { $_ eq $format } @format_basetext ) {
260 if( !exists $init_args->{'base'} ) {
261 warn "Cannot make a collation from $format without a base text";
266 # Now do the parsing.
269 $format = 'BaseText'; # Use the BaseText module for parsing,
270 # but retain the original input arg.
272 my $mod = "Text::Tradition::Parser::$format";
274 $mod->can('parse')->( $self, $init_args );
280 __PACKAGE__->meta->make_immutable;
287 =item * Allow tradition to be initialized via passing to a collator.
293 This package is free software and is provided "as is" without express
294 or implied warranty. You can redistribute it and/or modify it under
295 the same terms as Perl itself.
299 Tara L Andrews E<lt>aurum@cpan.orgE<gt>