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 * JSON - an alignment table in JSON format, as produced by CollateX and other tools
133 =item * KUL - a specific CSV format for variants, not documented here
135 =item * TEI - a TEI parallel segmentation format file
137 =item * Tabular - a comma- or tab-separated collation. Takes an additional
138 option, 'sep_char', which defaults to the tab character.
142 =item B<file> - The name of the file that contains the data. One of 'file'
143 or 'string' should be specified.
145 =item B<string> - A text string that contains the data. One of 'file' or
146 'string' should be specified.
148 =item B<base> - The name of a text file that contains the base text, to be
149 used with input formats that require it (currently only KUL).
153 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
157 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
158 objects that carry the text to be collated.
160 =item B<collator> - A reference to a collation program that will accept
167 Return the Text::Tradition::Witness objects associated with this tradition,
170 =head2 B<witness>( $sigil )
172 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
173 if there is no such object within the tradition.
175 =head2 B<add_witness>( %opts )
177 Instantiate a new witness with the given options (see documentation for
178 Text::Tradition::Witness) and add it to the tradition.
180 =head2 B<del_witness>( $sigil )
182 Delete the witness with the given sigil from the tradition. Returns the
183 witness object for the deleted witness.
187 use_ok( 'Text::Tradition', "can use module" );
189 my $t = Text::Tradition->new( 'name' => 'empty' );
190 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
191 is( $t->name, 'empty', "object has the right name" );
192 is( scalar $t->witnesses, 0, "object has no witnesses" );
194 my $simple = 't/data/simple.txt';
195 my $s = Text::Tradition->new(
197 'input' => 'Tabular',
200 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
201 is( $s->name, 'inline', "object has the right name" );
202 is( scalar $s->witnesses, 3, "object has three witnesses" );
204 my $wit_a = $s->witness('A');
205 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
207 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
209 is( $s->witness('X'), undef, "There is no witness X" );
210 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
212 my $wit_d = $s->add_witness( 'sigil' => 'D' );
213 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
214 is( $wit_d->sigil, 'D', "witness has correct sigil" );
215 is( scalar $s->witnesses, 4, "object now has four witnesses" );
217 my $del = $s->del_witness( 'D' );
218 is( $del, $wit_d, "Deleted correct witness" );
219 is( scalar $s->witnesses, 3, "object has three witnesses again" );
221 # TODO test initialization by witness list when we have it
229 my( $self, $init_args ) = @_;
231 if( exists $init_args->{'witnesses'} ) {
232 # We got passed an uncollated list of witnesses. Make a
233 # witness object for each witness, and then send them to the
236 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
237 # Each item in the list is either a string or an arrayref.
238 # If it's a string, it is a filename; if it's an arrayref,
239 # it is a tuple of 'sigil, file'. Handle either case.
241 if( ref( $wit ) eq 'ARRAY' ) {
242 $args = { 'sigil' => $wit->[0],
243 'file' => $wit->[1] };
245 $args = { 'sigil' => chr( $autosigil+65 ),
249 $self->witnesses->add_witness( $args );
250 # TODO Now how to collate these?
253 # Else we need to parse some collation data. Make a Collation object
254 my $collation = Text::Tradition::Collation->new( %$init_args,
255 'tradition' => $self );
256 $self->_save_collation( $collation );
258 # Call the appropriate parser on the given data
259 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
260 my @format_basetext = qw/ KUL /;
262 my $format = $init_args->{'input'};
263 if( $format && !( grep { $_ eq $format } @format_standalone )
264 && !( grep { $_ eq $format } @format_basetext ) ) {
265 warn "Unrecognized input format $format; not parsing";
268 if( $format && grep { $_ eq $format } @format_basetext ) {
270 if( !exists $init_args->{'base'} ) {
271 warn "Cannot make a collation from $format without a base text";
276 # Now do the parsing.
279 $format = 'BaseText'; # Use the BaseText module for parsing,
280 # but retain the original input arg.
282 my $mod = "Text::Tradition::Parser::$format";
284 $mod->can('parse')->( $self, $init_args );
289 =head2 add_stemma( $dotfile )
291 Initializes a Text::Tradition::Stemma object from the given dotfile,
292 and associates it with the tradition.
298 my $t = Text::Tradition->new(
299 'name' => 'simple test',
300 'input' => 'Tabular',
301 'file' => 't/data/simple.txt',
305 ok( $s = $t->add_stemma( 't/data/simple.dot' ), "Added a simple stemma" );
306 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
307 is( $t->stemma, $s, "Stemma is the right one" );
314 my( $self, $dot ) = @_;
315 open my $stemma_fh, '<', $dot or warn "Could not open file $dot";
316 my $stemma = Text::Tradition::Stemma->new(
317 'collation' => $self->collation,
318 'dot' => $stemma_fh );
319 $self->_add_stemma( $stemma ) if $stemma;
324 __PACKAGE__->meta->make_immutable;
331 =item * Allow tradition to be initialized via passing to a collator.
337 This package is free software and is provided "as is" without express
338 or implied warranty. You can redistribute it and/or modify it under
339 the same terms as Perl itself.
343 Tara L Andrews E<lt>aurum@cpan.orgE<gt>