1 package Text::Tradition;
3 use JSON qw / from_json /;
6 use Text::Tradition::Collation;
7 use Text::Tradition::Stemma;
8 use Text::Tradition::Witness;
9 use Text::Tradition::User;
11 use vars qw( $VERSION );
16 isa => 'Text::Tradition::Collation',
17 writer => '_save_collation',
20 has 'witness_hash' => (
22 isa => 'HashRef[Text::Tradition::Witness]',
26 del_witness => 'delete',
27 has_witness => 'exists',
28 witnesses => 'values',
30 default => sub { {} },
36 default => 'Tradition',
42 predicate => 'has_language',
47 isa => 'ArrayRef[Text::Tradition::Stemma]',
49 stemmata => 'elements',
50 _add_stemma => 'push',
52 stemma_count => 'count',
53 clear_stemmata => 'clear',
55 default => sub { [] },
60 isa => 'Text::Tradition::User',
62 predicate => 'has_user',
63 clearer => 'clear_user',
66 # Create the witness before trying to add it
67 around 'add_witness' => sub {
70 # TODO allow add of a Witness object?
71 my %args = @_ == 1 ? %{$_[0]} : @_;
72 $args{'tradition'} = $self;
73 $args{'language'} = $self->language
74 if( $self->language && !exists $args{'language'} );
75 my $new_wit = Text::Tradition::Witness->new( %args );
76 $self->$orig( $new_wit->sigil => $new_wit );
80 # Allow deletion of witness by object as well as by sigil
81 around 'del_witness' => sub {
85 foreach my $arg ( @_ ) {
87 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
89 return $self->$orig( @key_args );
92 # Don't allow an empty hash value
93 around 'witness' => sub {
94 my( $orig, $self, $arg ) = @_;
95 return unless $self->has_witness( $arg );
96 return $self->$orig( $arg );
101 Text::Tradition - a software model for a set of collated texts
106 my $t = Text::Tradition->new(
107 'name' => 'this is a text',
109 'file' => '/path/to/tei_parallel_seg_file.xml' );
111 my @text_wits = $t->witnesses();
112 my $manuscript_a = $t->witness( 'A' );
114 $t = Text::Tradition->new();
115 $t->add_witness( 'sourcetype' => 'xmldesc',
116 'file' => '/path/to/teitranscription.xml' );
117 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
118 'string' => 'The quick brown fox jumped over the lazy dogs' );
122 my $text_path_svg = $t->collation->as_svg();
123 ## See Text::Tradition::Collation for more on text collation itself
127 Text::Tradition is a library for representation and analysis of collated
128 texts, particularly medieval ones. A 'tradition' refers to the aggregation
129 of surviving versions of a text, generally preserved in multiple
130 manuscripts (or 'witnesses'). A Tradition object thus has one more more
131 Witnesses, as well as a Collation that represents the unity of all versions
138 Creates and returns a new text tradition object. The following options are
145 =item B<name> - The name of the text.
149 Initialization based on a collation file:
153 =item B<input> - The input format of the collation file. Can be one of the
158 =item * Self - a GraphML format produced by this module
160 =item * CollateX - a GraphML format produced by CollateX
162 =item * CTE - a TEI XML format produced by Classical Text Editor
164 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
166 =item * KUL - a specific CSV format for variants, not documented here
168 =item * TEI - a TEI parallel segmentation format file
170 =item * Tabular - a comma- or tab-separated collation. Takes an additional
171 option, 'sep_char', which defaults to the tab character.
175 =item B<file> - The name of the file that contains the data. One of 'file'
176 or 'string' should be specified.
178 =item B<string> - A text string that contains the data. One of 'file' or
179 'string' should be specified.
181 =item B<base> - The name of a text file that contains the base text, to be
182 used with input formats that require it (currently only KUL).
186 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
190 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
191 objects that carry the text to be collated.
193 =item B<collator> - A reference to a collation program that will accept
200 Return the Text::Tradition::Witness objects associated with this tradition,
203 =head2 B<witness>( $sigil )
205 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
206 if there is no such object within the tradition.
208 =head2 B<add_witness>( %opts )
210 Instantiate a new witness with the given options (see documentation for
211 Text::Tradition::Witness) and add it to the tradition.
213 =head2 B<del_witness>( $sigil )
215 Delete the witness with the given sigil from the tradition. Returns the
216 witness object for the deleted witness.
220 use_ok( 'Text::Tradition', "can use module" );
222 my $t = Text::Tradition->new( 'name' => 'empty' );
223 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
224 is( $t->name, 'empty', "object has the right name" );
225 is( scalar $t->witnesses, 0, "object has no witnesses" );
227 my $simple = 't/data/simple.txt';
228 my $s = Text::Tradition->new(
230 'input' => 'Tabular',
233 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
234 is( $s->name, 'inline', "object has the right name" );
235 is( scalar $s->witnesses, 3, "object has three witnesses" );
237 my $wit_a = $s->witness('A');
238 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
240 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
242 is( $s->witness('X'), undef, "There is no witness X" );
243 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
245 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
246 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
247 is( $wit_d->sigil, 'D', "witness has correct sigil" );
248 is( scalar $s->witnesses, 4, "object now has four witnesses" );
250 my $del = $s->del_witness( 'D' );
251 is( $del, $wit_d, "Deleted correct witness" );
252 is( scalar $s->witnesses, 3, "object has three witnesses again" );
254 # TODO test initialization by witness list when we have it
262 my( $self, $init_args ) = @_;
264 # First, make a collation object. This will use only those arguments in
265 # init_args that apply to the collation.
266 my $collation = Text::Tradition::Collation->new( %$init_args,
267 'tradition' => $self );
268 $self->_save_collation( $collation );
270 if( exists $init_args->{'input'} ) {
271 # Call the appropriate parser on the given data
272 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
273 my @format_basetext = qw/ KUL /;
275 my $format = $init_args->{'input'};
276 if( $format && !( grep { $_ eq $format } @format_standalone )
277 && !( grep { $_ eq $format } @format_basetext ) ) {
278 warn "Unrecognized input format $format; not parsing";
281 if( $format && grep { $_ eq $format } @format_basetext ) {
283 if( !exists $init_args->{'base'} ) {
284 warn "Cannot make a collation from $format without a base text";
289 # Now do the parsing.
292 $format = 'BaseText'; # Use the BaseText module for parsing,
293 # but retain the original input arg.
295 my $mod = "Text::Tradition::Parser::$format";
297 $mod->can('parse')->( $self, $init_args );
303 =head2 add_json_witnesses( $jsonstring, $options )
305 Adds a set of witnesses from a JSON array specification. This is a wrapper
306 to parse the JSON and call add_witness (with the specified $options) for
307 each element therein.
311 sub add_json_witnesses {
312 my( $self, $jsonstr, $extraopts ) = @_;
313 my $witarray = from_json( $jsonstr );
314 foreach my $witspec ( @{$witarray->{witnesses}} ) {
315 my $opts = $extraopts || {};
316 $opts->{'sourcetype'} = 'json';
317 $opts->{'object'} = $witspec;
318 $self->add_witness( $opts );
322 =head2 add_stemma( $dotfile )
324 Initializes a Text::Tradition::Stemma object from the given dotfile,
325 and associates it with the tradition.
331 my $t = Text::Tradition->new(
332 'name' => 'simple test',
333 'input' => 'Tabular',
334 'file' => 't/data/simple.txt',
337 is( $t->stemma_count, 0, "No stemmas added yet" );
339 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
340 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
341 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
342 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
352 if( $opts{'dotfile'} ) {
353 open $stemma_fh, '<', $opts{'dotfile'}
354 or warn "Could not open file " . $opts{'dotfile'};
355 } elsif( $opts{'dot'} ) {
356 my $str = $opts{'dot'};
357 open $stemma_fh, '<', \$str;
360 binmode $stemma_fh, ':utf8';
361 my $stemma = Text::Tradition::Stemma->new(
362 'collation' => $self->collation,
363 'dot' => $stemma_fh );
364 $self->_add_stemma( $stemma ) if $stemma;
370 unless( $self->has_language ) {
371 warn "Please set a language to lemmatize a tradition";
374 my $mod = "Text::Tradition::Language::" . $self->language;
376 $mod->can( 'lemmatize' )->( $self );
380 __PACKAGE__->meta->make_immutable;
387 =item * Allow tradition to be initialized via passing to a collator.
393 This package is free software and is provided "as is" without express
394 or implied warranty. You can redistribute it and/or modify it under
395 the same terms as Perl itself.
399 Tara L Andrews E<lt>aurum@cpan.orgE<gt>