1 package Text::Tradition;
3 use JSON qw / from_json /;
6 use Moose::Util qw/ does_role apply_all_roles /;
8 use Text::Tradition::Collation;
9 use Text::Tradition::Error;
10 use Text::Tradition::Witness;
13 use vars qw( $VERSION );
16 # Enable plugin(s) if available
17 eval { with 'Text::Tradition::HasStemma'; };
20 # warn "Text::Tradition::Analysis not found. Disabling stemma analysis functionality";
22 eval { with 'Text::Tradition::Language'; };
23 eval { with 'Text::Tradition::Ownership'; };
27 isa => 'Text::Tradition::Collation',
28 writer => '_save_collation',
31 has 'witness_hash' => (
33 isa => 'HashRef[Text::Tradition::Witness]',
37 del_witness => 'delete',
38 has_witness => 'exists',
39 witnesses => 'values',
41 default => sub { {} },
47 default => 'Tradition',
50 has '_initialized' => (
54 writer => '_init_done',
57 # Create the witness if necessary before trying to add it
58 around 'add_witness' => sub {
62 if( @_ == 1 && $_[0]->$_isa( 'Text::Tradition::Witness' ) ) {
65 my %args = @_ == 1 ? %{$_[0]} : @_;
66 $args{'tradition'} = $self;
67 $new_wit = Text::Tradition::Witness->new( %args );
69 $self->$orig( $new_wit->sigil => $new_wit );
73 # Allow deletion of witness by object as well as by sigil
74 around 'del_witness' => sub {
78 foreach my $arg ( @_ ) {
80 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
82 return $self->$orig( @key_args );
85 # Don't allow an empty hash value
86 around 'witness' => sub {
87 my( $orig, $self, $arg ) = @_;
88 return unless $self->has_witness( $arg );
89 return $self->$orig( $arg );
92 # Cope with witness sigil changes
94 my( $self, $sig, $newsig ) = @_;
95 my $wit = $self->witness( $sig );
96 $self->throw( "No such witness $sig" ) unless $wit;
97 $self->throw( "Cannot rename witness that has already been collated" )
99 $wit = $self->del_witness( $sig );
101 $wit->_set_sigil( $newsig );
103 # Don't lose the witness if the rename failed
104 $self->add_witness( $wit );
107 $self->add_witness( $wit );
112 Text::Tradition - a software model for a set of collated texts
117 my $t = Text::Tradition->new(
118 'name' => 'this is a text',
120 'file' => '/path/to/tei_parallel_seg_file.xml' );
122 my @text_wits = $t->witnesses();
123 my $manuscript_a = $t->witness( 'A' );
125 $t = Text::Tradition->new();
126 $t->add_witness( 'sourcetype' => 'xmldesc',
127 'file' => '/path/to/teitranscription.xml' );
128 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
129 'string' => 'The quick brown fox jumped over the lazy dogs' );
133 my $text_path_svg = $t->collation->as_svg();
134 ## See Text::Tradition::Collation for more on text collation itself
138 Text::Tradition is a library for representation and analysis of collated
139 texts, particularly medieval ones. A 'tradition' refers to the aggregation
140 of surviving versions of a text, generally preserved in multiple
141 manuscripts (or 'witnesses'). A Tradition object thus has one more more
142 Witnesses, as well as a Collation that represents the unity of all versions
149 Creates and returns a new text tradition object. The following options are
156 =item B<name> - The name of the text.
160 Initialization based on a collation file:
164 =item B<input> - The input format of the collation file. Can be one of the
169 =item * Self - a GraphML format produced by this module
171 =item * CollateX - a GraphML format produced by CollateX
173 =item * CTE - a TEI XML format produced by Classical Text Editor
175 =item * JSON - an alignment table in JSON format, as produced by CollateX and
178 =item * TEI - a TEI parallel segmentation format file
180 =item * Tabular - a spreadsheet collation. See the documentation for
181 L<Text::Tradition::Parser::Tabular> for an explanation of additional options.
185 =item B<file> - The name of the file that contains the data. One of 'file'
186 or 'string' should be specified.
188 =item B<string> - A text string that contains the data. One of 'file' or
189 'string' should be specified.
193 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
197 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
198 objects that carry the text to be collated.
200 =item B<collator> - A reference to a collation program that will accept
207 Return the Text::Tradition::Witness objects associated with this tradition,
210 =head2 B<witness>( $sigil )
212 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
213 if there is no such object within the tradition.
215 =head2 B<add_witness>( %opts )
217 Instantiate a new witness with the given options (see documentation for
218 Text::Tradition::Witness) and add it to the tradition.
220 =head2 B<del_witness>( $sigil )
222 Delete the witness with the given sigil from the tradition. Returns the
223 witness object for the deleted witness.
228 use_ok( 'Text::Tradition', "can use module" );
230 my $t = Text::Tradition->new( 'name' => 'empty' );
231 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
232 is( $t->name, 'empty', "object has the right name" );
233 is( scalar $t->witnesses, 0, "object has no witnesses" );
235 my $simple = 't/data/simple.txt';
236 my $s = Text::Tradition->new(
238 'input' => 'Tabular',
241 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
242 is( $s->name, 'inline', "object has the right name" );
243 is( scalar $s->witnesses, 3, "object has three witnesses" );
245 my $wit_a = $s->witness('A');
246 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
248 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
250 is( $s->witness('X'), undef, "There is no witness X" );
251 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
253 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'plaintext',
254 'string' => 'je suis depourvu de foi' );
255 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
256 is( $wit_d->sigil, 'D', "witness has correct sigil" );
257 is( scalar $s->witnesses, 4, "object now has four witnesses" );
260 $s->rename_witness( 'D', 'Invalid Sigil' );
261 ok( 0, "Renamed witness with bad sigil" );
262 } catch ( Text::Tradition::Error $e ) {
263 is( $s->witness('D'), $wit_d, "Held onto witness during bad rename" );
267 $s->rename_witness( 'D', 'Q' );
268 ok( 1, "Rename of witness succeeded" );
269 is( $s->witness('Q'), $wit_d, "Witness available under new sigil" );
270 ok( !$s->has_witness('D'), "Witness no longer available under old sigil" );
271 } catch ( Text::Tradition::Error $e ) {
272 ok( 0, "Failed to rename witness: " . $e->message );
275 my $del = $s->del_witness( 'Q' );
276 is( $del, $wit_d, "Deleted correct witness" );
277 is( scalar $s->witnesses, 3, "object has three witnesses again" );
280 $s->rename_witness( 'A', 'WitA' );
281 ok( 0, "Successfully renamed an already collated witness" );
282 } catch ( Text::Tradition::Error $e ) {
283 is( $e->message, 'Cannot rename witness that has already been collated',
284 "Refused to rename an already-collated witness" );
293 my( $self, $init_args ) = @_;
295 # First, make a collation object. This will use only those arguments in
296 # init_args that apply to the collation.
297 my $collation = Text::Tradition::Collation->new( %$init_args,
298 'tradition' => $self );
299 $self->_save_collation( $collation );
301 if( exists $init_args->{'input'} ) {
302 # Call the appropriate parser on the given data
303 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
304 my @format_basetext = qw/ KUL /;
306 my $format = $init_args->{'input'};
307 if( $format && !( grep { $_ eq $format } @format_standalone )
308 && !( grep { $_ eq $format } @format_basetext ) ) {
309 warn "Unrecognized input format $format; not parsing";
312 if( $format && grep { $_ eq $format } @format_basetext ) {
314 if( !exists $init_args->{'base'} ) {
315 warn "Cannot make a collation from $format without a base text";
320 # Now do the parsing.
323 $format = 'BaseText'; # Use the BaseText module for parsing,
324 # but retain the original input arg.
326 my $mod = "Text::Tradition::Parser::$format";
328 $mod->can('parse')->( $self, $init_args );
331 $self->_init_done( 1 );
335 =head2 clear_collation
337 Blow away the existing collation object and mark all witnesses as uncollated.
338 Not to be used lightly.
342 sub clear_collation {
344 $self->_save_collation( Text::Tradition::Collation->new( tradition => $self ) );
345 map { $_->is_collated( 0 ) } $self->witnesses;
348 =head2 add_json_witnesses( $jsonstring, $options )
350 Adds a set of witnesses from a JSON array specification. This is a wrapper
351 to parse the JSON and call add_witness (with the specified $options) for
352 each element therein.
356 sub add_json_witnesses {
357 my( $self, $jsonstr, $extraopts ) = @_;
358 my $witarray = from_json( $jsonstr );
359 foreach my $witspec ( @{$witarray->{witnesses}} ) {
360 my $opts = $extraopts || {};
361 $opts->{'sourcetype'} = 'json';
362 $opts->{'object'} = $witspec;
363 $self->add_witness( $opts );
369 Text::Tradition::Error->throw(
370 'ident' => 'Tradition error',
376 __PACKAGE__->meta->make_immutable;
383 =item * Allow tradition to be initialized via passing to a collator.
389 This package is free software and is provided "as is" without express
390 or implied warranty. You can redistribute it and/or modify it under
391 the same terms as Perl itself.
395 Tara L Andrews E<lt>aurum@cpan.orgE<gt>