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<add_witness>( %opts )
162 Instantiate a new witness with the given options (see documentation for
163 Text::Tradition::Witness) and add it to the tradition.
167 use_ok( 'Text::Tradition', "can use module" );
169 my $t = Text::Tradition->new( 'name' => 'empty' );
170 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
171 is( $t->name, 'empty', "object has the right name" );
172 is( scalar $t->witnesses, 0, "object has no witnesses" );
174 my $simple = 't/data/simple.txt';
175 my $s = Text::Tradition->new(
177 'input' => 'Tabular',
180 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
181 is( $s->name, 'inline', "object has the right name" );
182 is( scalar $s->witnesses, 3, "object has three witnesses" );
184 my $w = $s->add_witness( 'sigil' => 'D' );
185 is( ref( $w ), 'Text::Tradition::Witness', "new witness created" );
186 is( $w->sigil, 'D', "witness has correct sigil" );
187 is( scalar $s->witnesses, 4, "object now has four witnesses" );
189 my $del = $s->del_witness( 'D' );
190 is( $del, $w, "Deleted correct witness" );
191 is( scalar $s->witnesses, 3, "object has three witnesses again" );
193 # TODO test initialization by witness list when we have it
201 my( $self, $init_args ) = @_;
203 if( exists $init_args->{'witnesses'} ) {
204 # We got passed an uncollated list of witnesses. Make a
205 # witness object for each witness, and then send them to the
208 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
209 # Each item in the list is either a string or an arrayref.
210 # If it's a string, it is a filename; if it's an arrayref,
211 # it is a tuple of 'sigil, file'. Handle either case.
213 if( ref( $wit ) eq 'ARRAY' ) {
214 $args = { 'sigil' => $wit->[0],
215 'file' => $wit->[1] };
217 $args = { 'sigil' => chr( $autosigil+65 ),
221 $self->witnesses->add_witness( $args );
222 # TODO Now how to collate these?
225 # Else we need to parse some collation data. Make a Collation object
226 my $collation = Text::Tradition::Collation->new( %$init_args,
227 'tradition' => $self );
228 $self->_save_collation( $collation );
230 # Call the appropriate parser on the given data
231 my @format_standalone = qw/ Self CollateX CTE TEI Tabular /;
232 my @format_basetext = qw/ KUL /;
234 my $format = $init_args->{'input'};
235 if( $format && !( grep { $_ eq $format } @format_standalone )
236 && !( grep { $_ eq $format } @format_basetext ) ) {
237 warn "Unrecognized input format $format; not parsing";
240 if( $format && grep { $_ eq $format } @format_basetext ) {
242 if( !exists $init_args->{'base'} ) {
243 warn "Cannot make a collation from $format without a base text";
248 # Now do the parsing.
251 $format = 'BaseText'; # Use the BaseText module for parsing,
252 # but retain the original input arg.
254 my $mod = "Text::Tradition::Parser::$format";
256 $mod->can('parse')->( $self, $init_args );
261 =head2 B<witness>( $sigil )
263 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
264 if there is no such object within the tradition.
270 my $simple = 't/data/simple.txt';
271 my $s = Text::Tradition->new(
273 'input' => 'Tabular',
276 my $wit_a = $s->witness('A');
277 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
279 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
281 is( $s->witness('X'), undef, "There is no witness X" );
288 __PACKAGE__->meta->make_immutable;
295 =item * Allow tradition to be initialized via passing to a collator.
301 This package is free software and is provided "as is" without express
302 or implied warranty. You can redistribute it and/or modify it under
303 the same terms as Perl itself.
307 Tara L Andrews E<lt>aurum@cpan.orgE<gt>