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',
43 # Create the witness before trying to add it
44 around 'add_witness' => sub {
47 # TODO allow add of a Witness object?
48 my $new_wit = Text::Tradition::Witness->new( @_ );
49 $self->$orig( $new_wit->sigil => $new_wit );
53 # Allow deletion of witness by object as well as by sigil
54 around 'del_witness' => sub {
58 foreach my $arg ( @_ ) {
60 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
62 return $self->$orig( @key_args );
65 # Don't allow an empty hash value
66 around 'witness' => sub {
67 my( $orig, $self, $arg ) = @_;
68 return unless $self->has_witness( $arg );
69 return $self->$orig( $arg );
74 Text::Tradition - a software model for a set of collated texts
79 my $t = Text::Tradition->new(
80 'name' => 'this is a text',
82 'file' => '/path/to/tei_parallel_seg_file.xml' );
84 my @text_wits = $t->witnesses();
85 my $manuscript_a = $t->witness( 'A' );
86 my $new_ms = $t->add_witness( 'sigil' => 'B' );
88 my $text_path_svg = $t->collation->as_svg();
89 ## See Text::Tradition::Collation for more on text collation itself
93 Text::Tradition is a library for representation and analysis of collated
94 texts, particularly medieval ones. A 'tradition' refers to the aggregation
95 of surviving versions of a text, generally preserved in multiple
96 manuscripts (or 'witnesses'). A Tradition object thus has one more more
97 Witnesses, as well as a Collation that represents the unity of all versions
104 Creates and returns a new text tradition object. The following options are
111 =item B<name> - The name of the text.
115 Initialization based on a collation file:
119 =item B<input> - The input format of the collation file. Can be one of the
124 =item * Self - a GraphML format produced by this module
126 =item * CollateX - a GraphML format produced by CollateX
128 =item * CTE - a TEI XML format produced by Classical Text Editor
130 =item * KUL - a specific CSV format for variants, not documented here
132 =item * TEI - a TEI parallel segmentation format file
134 =item * Tabular - a comma- or tab-separated collation. Takes an additional
135 option, 'sep_char', which defaults to the tab character.
139 =item B<file> - The name of the file that contains the data. One of 'file'
140 or 'string' should be specified.
142 =item B<string> - A text string that contains the data. One of 'file' or
143 'string' should be specified.
145 =item B<base> - The name of a text file that contains the base text, to be
146 used with input formats that require it (currently only KUL).
150 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
154 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
155 objects that carry the text to be collated.
157 =item B<collator> - A reference to a collation program that will accept
164 Return the Text::Tradition::Witness objects associated with this tradition,
167 =head2 B<witness>( $sigil )
169 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
170 if there is no such object within the tradition.
172 =head2 B<add_witness>( %opts )
174 Instantiate a new witness with the given options (see documentation for
175 Text::Tradition::Witness) and add it to the tradition.
177 =head2 B<del_witness>( $sigil )
179 Delete the witness with the given sigil from the tradition. Returns the
180 witness object for the deleted witness.
184 use_ok( 'Text::Tradition', "can use module" );
186 my $t = Text::Tradition->new( 'name' => 'empty' );
187 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
188 is( $t->name, 'empty', "object has the right name" );
189 is( scalar $t->witnesses, 0, "object has no witnesses" );
191 my $simple = 't/data/simple.txt';
192 my $s = Text::Tradition->new(
194 'input' => 'Tabular',
197 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
198 is( $s->name, 'inline', "object has the right name" );
199 is( scalar $s->witnesses, 3, "object has three witnesses" );
201 my $wit_a = $s->witness('A');
202 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
204 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
206 is( $s->witness('X'), undef, "There is no witness X" );
207 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
209 my $wit_d = $s->add_witness( 'sigil' => 'D' );
210 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
211 is( $wit_d->sigil, 'D', "witness has correct sigil" );
212 is( scalar $s->witnesses, 4, "object now has four witnesses" );
214 my $del = $s->del_witness( 'D' );
215 is( $del, $wit_d, "Deleted correct witness" );
216 is( scalar $s->witnesses, 3, "object has three witnesses again" );
218 # TODO test initialization by witness list when we have it
226 my( $self, $init_args ) = @_;
228 if( exists $init_args->{'witnesses'} ) {
229 # We got passed an uncollated list of witnesses. Make a
230 # witness object for each witness, and then send them to the
233 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
234 # Each item in the list is either a string or an arrayref.
235 # If it's a string, it is a filename; if it's an arrayref,
236 # it is a tuple of 'sigil, file'. Handle either case.
238 if( ref( $wit ) eq 'ARRAY' ) {
239 $args = { 'sigil' => $wit->[0],
240 'file' => $wit->[1] };
242 $args = { 'sigil' => chr( $autosigil+65 ),
246 $self->witnesses->add_witness( $args );
247 # TODO Now how to collate these?
250 # Else we need to parse some collation data. Make a Collation object
251 my $collation = Text::Tradition::Collation->new( %$init_args,
252 'tradition' => $self );
253 $self->_save_collation( $collation );
255 # Call the appropriate parser on the given data
256 my @format_standalone = qw/ Self CollateText CollateX CTE TEI Tabular /;
257 my @format_basetext = qw/ KUL /;
259 my $format = $init_args->{'input'};
260 if( $format && !( grep { $_ eq $format } @format_standalone )
261 && !( grep { $_ eq $format } @format_basetext ) ) {
262 warn "Unrecognized input format $format; not parsing";
265 if( $format && grep { $_ eq $format } @format_basetext ) {
267 if( !exists $init_args->{'base'} ) {
268 warn "Cannot make a collation from $format without a base text";
273 # Now do the parsing.
276 $format = 'BaseText'; # Use the BaseText module for parsing,
277 # but retain the original input arg.
279 my $mod = "Text::Tradition::Parser::$format";
281 $mod->can('parse')->( $self, $init_args );
286 =head2 add_stemma( $dotfile )
288 Initializes a Text::Tradition::Stemma object from the given dotfile,
289 and associates it with the tradition.
295 my $t = Text::Tradition->new(
296 'name' => 'simple test',
297 'input' => 'Tabular',
298 'file' => 't/data/simple.txt',
302 ok( $s = $t->add_stemma( 't/data/simple.dot' ), "Added a simple stemma" );
303 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
304 is( $t->stemma, $s, "Stemma is the right one" );
311 my( $self, $dot ) = @_;
312 open my $stemma_fh, '<', $dot or warn "Could not open file $dot";
313 my $stemma = Text::Tradition::Stemma->new(
314 'collation' => $self->collation,
315 'dot' => $stemma_fh );
316 $self->_add_stemma( $stemma ) if $stemma;
321 __PACKAGE__->meta->make_immutable;
328 =item * Allow tradition to be initialized via passing to a collator.
334 This package is free software and is provided "as is" without express
335 or implied warranty. You can redistribute it and/or modify it under
336 the same terms as Perl itself.
340 Tara L Andrews E<lt>aurum@cpan.orgE<gt>