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',
44 isa => 'ArrayRef[Text::Tradition::Stemma]',
46 stemmata => 'elements',
47 _add_stemma => 'push',
49 stemma_count => 'count',
50 clear_stemmata => 'clear',
52 default => sub { [] },
55 # Create the witness before trying to add it
56 around 'add_witness' => sub {
59 # TODO allow add of a Witness object?
60 my $new_wit = Text::Tradition::Witness->new( @_ );
61 $self->$orig( $new_wit->sigil => $new_wit );
65 # Allow deletion of witness by object as well as by sigil
66 around 'del_witness' => sub {
70 foreach my $arg ( @_ ) {
72 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
74 return $self->$orig( @key_args );
77 # Don't allow an empty hash value
78 around 'witness' => sub {
79 my( $orig, $self, $arg ) = @_;
80 return unless $self->has_witness( $arg );
81 return $self->$orig( $arg );
86 Text::Tradition - a software model for a set of collated texts
91 my $t = Text::Tradition->new(
92 'name' => 'this is a text',
94 'file' => '/path/to/tei_parallel_seg_file.xml' );
96 my @text_wits = $t->witnesses();
97 my $manuscript_a = $t->witness( 'A' );
98 my $new_ms = $t->add_witness( 'sigil' => 'B' );
100 my $text_path_svg = $t->collation->as_svg();
101 ## See Text::Tradition::Collation for more on text collation itself
105 Text::Tradition is a library for representation and analysis of collated
106 texts, particularly medieval ones. A 'tradition' refers to the aggregation
107 of surviving versions of a text, generally preserved in multiple
108 manuscripts (or 'witnesses'). A Tradition object thus has one more more
109 Witnesses, as well as a Collation that represents the unity of all versions
116 Creates and returns a new text tradition object. The following options are
123 =item B<name> - The name of the text.
127 Initialization based on a collation file:
131 =item B<input> - The input format of the collation file. Can be one of the
136 =item * Self - a GraphML format produced by this module
138 =item * CollateX - a GraphML format produced by CollateX
140 =item * CTE - a TEI XML format produced by Classical Text Editor
142 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
144 =item * KUL - a specific CSV format for variants, not documented here
146 =item * TEI - a TEI parallel segmentation format file
148 =item * Tabular - a comma- or tab-separated collation. Takes an additional
149 option, 'sep_char', which defaults to the tab character.
153 =item B<file> - The name of the file that contains the data. One of 'file'
154 or 'string' should be specified.
156 =item B<string> - A text string that contains the data. One of 'file' or
157 'string' should be specified.
159 =item B<base> - The name of a text file that contains the base text, to be
160 used with input formats that require it (currently only KUL).
164 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
168 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
169 objects that carry the text to be collated.
171 =item B<collator> - A reference to a collation program that will accept
178 Return the Text::Tradition::Witness objects associated with this tradition,
181 =head2 B<witness>( $sigil )
183 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
184 if there is no such object within the tradition.
186 =head2 B<add_witness>( %opts )
188 Instantiate a new witness with the given options (see documentation for
189 Text::Tradition::Witness) and add it to the tradition.
191 =head2 B<del_witness>( $sigil )
193 Delete the witness with the given sigil from the tradition. Returns the
194 witness object for the deleted witness.
198 use_ok( 'Text::Tradition', "can use module" );
200 my $t = Text::Tradition->new( 'name' => 'empty' );
201 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
202 is( $t->name, 'empty', "object has the right name" );
203 is( scalar $t->witnesses, 0, "object has no witnesses" );
205 my $simple = 't/data/simple.txt';
206 my $s = Text::Tradition->new(
208 'input' => 'Tabular',
211 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
212 is( $s->name, 'inline', "object has the right name" );
213 is( scalar $s->witnesses, 3, "object has three witnesses" );
215 my $wit_a = $s->witness('A');
216 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
218 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
220 is( $s->witness('X'), undef, "There is no witness X" );
221 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
223 my $wit_d = $s->add_witness( 'sigil' => 'D' );
224 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
225 is( $wit_d->sigil, 'D', "witness has correct sigil" );
226 is( scalar $s->witnesses, 4, "object now has four witnesses" );
228 my $del = $s->del_witness( 'D' );
229 is( $del, $wit_d, "Deleted correct witness" );
230 is( scalar $s->witnesses, 3, "object has three witnesses again" );
232 # TODO test initialization by witness list when we have it
240 my( $self, $init_args ) = @_;
242 if( exists $init_args->{'witnesses'} ) {
243 # We got passed an uncollated list of witnesses. Make a
244 # witness object for each witness, and then send them to the
247 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
248 # Each item in the list is either a string or an arrayref.
249 # If it's a string, it is a filename; if it's an arrayref,
250 # it is a tuple of 'sigil, file'. Handle either case.
252 if( ref( $wit ) eq 'ARRAY' ) {
253 $args = { 'sigil' => $wit->[0],
254 'file' => $wit->[1] };
256 $args = { 'sigil' => chr( $autosigil+65 ),
260 $self->witnesses->add_witness( $args );
261 # TODO Now how to collate these?
264 # Else we need to parse some collation data. Make a Collation object
265 my $collation = Text::Tradition::Collation->new( %$init_args,
266 'tradition' => $self );
267 $self->_save_collation( $collation );
269 # Call the appropriate parser on the given data
270 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
271 my @format_basetext = qw/ KUL /;
273 my $format = $init_args->{'input'};
274 if( $format && !( grep { $_ eq $format } @format_standalone )
275 && !( grep { $_ eq $format } @format_basetext ) ) {
276 warn "Unrecognized input format $format; not parsing";
279 if( $format && grep { $_ eq $format } @format_basetext ) {
281 if( !exists $init_args->{'base'} ) {
282 warn "Cannot make a collation from $format without a base text";
287 # Now do the parsing.
290 $format = 'BaseText'; # Use the BaseText module for parsing,
291 # but retain the original input arg.
293 my $mod = "Text::Tradition::Parser::$format";
295 $mod->can('parse')->( $self, $init_args );
300 =head2 add_stemma( $dotfile )
302 Initializes a Text::Tradition::Stemma object from the given dotfile,
303 and associates it with the tradition.
309 my $t = Text::Tradition->new(
310 'name' => 'simple test',
311 'input' => 'Tabular',
312 'file' => 't/data/simple.txt',
315 is( $t->stemma_count, 0, "No stemmas added yet" );
317 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
318 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
319 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
320 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
330 if( $opts{'dotfile'} ) {
331 open $stemma_fh, '<', $opts{'dotfile'}
332 or warn "Could not open file " . $opts{'dotfile'};
333 } elsif( $opts{'dot'} ) {
334 my $str = $opts{'dot'};
335 open $stemma_fh, '<', \$str;
338 binmode $stemma_fh, ':utf8';
339 my $stemma = Text::Tradition::Stemma->new(
340 'collation' => $self->collation,
341 'dot' => $stemma_fh );
342 $self->_add_stemma( $stemma ) if $stemma;
347 __PACKAGE__->meta->make_immutable;
354 =item * Allow tradition to be initialized via passing to a collator.
360 This package is free software and is provided "as is" without express
361 or implied warranty. You can redistribute it and/or modify it under
362 the same terms as Perl itself.
366 Tara L Andrews E<lt>aurum@cpan.orgE<gt>