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',
65 # Create the witness before trying to add it
66 around 'add_witness' => sub {
69 # TODO allow add of a Witness object?
70 my %args = @_ == 1 ? %{$_[0]} : @_;
71 $args{'tradition'} = $self;
72 $args{'language'} = $self->language
73 if( $self->language && !exists $args{'language'} );
74 my $new_wit = Text::Tradition::Witness->new( %args );
75 $self->$orig( $new_wit->sigil => $new_wit );
79 # Allow deletion of witness by object as well as by sigil
80 around 'del_witness' => sub {
84 foreach my $arg ( @_ ) {
86 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
88 return $self->$orig( @key_args );
91 # Don't allow an empty hash value
92 around 'witness' => sub {
93 my( $orig, $self, $arg ) = @_;
94 return unless $self->has_witness( $arg );
95 return $self->$orig( $arg );
100 Text::Tradition - a software model for a set of collated texts
105 my $t = Text::Tradition->new(
106 'name' => 'this is a text',
108 'file' => '/path/to/tei_parallel_seg_file.xml' );
110 my @text_wits = $t->witnesses();
111 my $manuscript_a = $t->witness( 'A' );
113 $t = Text::Tradition->new();
114 $t->add_witness( 'sourcetype' => 'xmldesc',
115 'file' => '/path/to/teitranscription.xml' );
116 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
117 'string' => 'The quick brown fox jumped over the lazy dogs' );
121 my $text_path_svg = $t->collation->as_svg();
122 ## See Text::Tradition::Collation for more on text collation itself
126 Text::Tradition is a library for representation and analysis of collated
127 texts, particularly medieval ones. A 'tradition' refers to the aggregation
128 of surviving versions of a text, generally preserved in multiple
129 manuscripts (or 'witnesses'). A Tradition object thus has one more more
130 Witnesses, as well as a Collation that represents the unity of all versions
137 Creates and returns a new text tradition object. The following options are
144 =item B<name> - The name of the text.
148 Initialization based on a collation file:
152 =item B<input> - The input format of the collation file. Can be one of the
157 =item * Self - a GraphML format produced by this module
159 =item * CollateX - a GraphML format produced by CollateX
161 =item * CTE - a TEI XML format produced by Classical Text Editor
163 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
165 =item * KUL - a specific CSV format for variants, not documented here
167 =item * TEI - a TEI parallel segmentation format file
169 =item * Tabular - a comma- or tab-separated collation. Takes an additional
170 option, 'sep_char', which defaults to the tab character.
174 =item B<file> - The name of the file that contains the data. One of 'file'
175 or 'string' should be specified.
177 =item B<string> - A text string that contains the data. One of 'file' or
178 'string' should be specified.
180 =item B<base> - The name of a text file that contains the base text, to be
181 used with input formats that require it (currently only KUL).
185 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
189 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
190 objects that carry the text to be collated.
192 =item B<collator> - A reference to a collation program that will accept
199 Return the Text::Tradition::Witness objects associated with this tradition,
202 =head2 B<witness>( $sigil )
204 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
205 if there is no such object within the tradition.
207 =head2 B<add_witness>( %opts )
209 Instantiate a new witness with the given options (see documentation for
210 Text::Tradition::Witness) and add it to the tradition.
212 =head2 B<del_witness>( $sigil )
214 Delete the witness with the given sigil from the tradition. Returns the
215 witness object for the deleted witness.
219 use_ok( 'Text::Tradition', "can use module" );
221 my $t = Text::Tradition->new( 'name' => 'empty' );
222 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
223 is( $t->name, 'empty', "object has the right name" );
224 is( scalar $t->witnesses, 0, "object has no witnesses" );
226 my $simple = 't/data/simple.txt';
227 my $s = Text::Tradition->new(
229 'input' => 'Tabular',
232 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
233 is( $s->name, 'inline', "object has the right name" );
234 is( scalar $s->witnesses, 3, "object has three witnesses" );
236 my $wit_a = $s->witness('A');
237 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
239 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
241 is( $s->witness('X'), undef, "There is no witness X" );
242 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
244 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
245 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
246 is( $wit_d->sigil, 'D', "witness has correct sigil" );
247 is( scalar $s->witnesses, 4, "object now has four witnesses" );
249 my $del = $s->del_witness( 'D' );
250 is( $del, $wit_d, "Deleted correct witness" );
251 is( scalar $s->witnesses, 3, "object has three witnesses again" );
253 # TODO test initialization by witness list when we have it
261 my( $self, $init_args ) = @_;
263 # First, make a collation object. This will use only those arguments in
264 # init_args that apply to the collation.
265 my $collation = Text::Tradition::Collation->new( %$init_args,
266 'tradition' => $self );
267 $self->_save_collation( $collation );
269 if( exists $init_args->{'input'} ) {
270 # Call the appropriate parser on the given data
271 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
272 my @format_basetext = qw/ KUL /;
274 my $format = $init_args->{'input'};
275 if( $format && !( grep { $_ eq $format } @format_standalone )
276 && !( grep { $_ eq $format } @format_basetext ) ) {
277 warn "Unrecognized input format $format; not parsing";
280 if( $format && grep { $_ eq $format } @format_basetext ) {
282 if( !exists $init_args->{'base'} ) {
283 warn "Cannot make a collation from $format without a base text";
288 # Now do the parsing.
291 $format = 'BaseText'; # Use the BaseText module for parsing,
292 # but retain the original input arg.
294 my $mod = "Text::Tradition::Parser::$format";
296 $mod->can('parse')->( $self, $init_args );
302 =head2 add_json_witnesses( $jsonstring, $options )
304 Adds a set of witnesses from a JSON array specification. This is a wrapper
305 to parse the JSON and call add_witness (with the specified $options) for
306 each element therein.
310 sub add_json_witnesses {
311 my( $self, $jsonstr, $extraopts ) = @_;
312 my $witarray = from_json( $jsonstr );
313 foreach my $witspec ( @{$witarray->{witnesses}} ) {
314 my $opts = $extraopts || {};
315 $opts->{'sourcetype'} = 'json';
316 $opts->{'object'} = $witspec;
317 $self->add_witness( $opts );
321 =head2 add_stemma( $dotfile )
323 Initializes a Text::Tradition::Stemma object from the given dotfile,
324 and associates it with the tradition.
330 my $t = Text::Tradition->new(
331 'name' => 'simple test',
332 'input' => 'Tabular',
333 'file' => 't/data/simple.txt',
336 is( $t->stemma_count, 0, "No stemmas added yet" );
338 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
339 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
340 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
341 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
351 if( $opts{'dotfile'} ) {
352 open $stemma_fh, '<', $opts{'dotfile'}
353 or warn "Could not open file " . $opts{'dotfile'};
354 } elsif( $opts{'dot'} ) {
355 my $str = $opts{'dot'};
356 open $stemma_fh, '<', \$str;
359 binmode $stemma_fh, ':utf8';
360 my $stemma = Text::Tradition::Stemma->new(
361 'collation' => $self->collation,
362 'dot' => $stemma_fh );
363 $self->_add_stemma( $stemma ) if $stemma;
369 unless( $self->has_language ) {
370 warn "Please set a language to lemmatize a tradition";
373 my $mod = "Text::Tradition::Language::" . $self->language;
375 $mod->can( 'lemmatize' )->( $self );
379 __PACKAGE__->meta->make_immutable;
386 =item * Allow tradition to be initialized via passing to a collator.
392 This package is free software and is provided "as is" without express
393 or implied warranty. You can redistribute it and/or modify it under
394 the same terms as Perl itself.
398 Tara L Andrews E<lt>aurum@cpan.orgE<gt>