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 { [] },
58 has '_initialized' => (
62 writer => '_init_done',
67 isa => 'Text::Tradition::User',
69 predicate => 'has_user',
70 clearer => 'clear_user',
77 default => sub { 0; },
80 # Create the witness before trying to add it
81 around 'add_witness' => sub {
84 # TODO allow add of a Witness object?
85 my %args = @_ == 1 ? %{$_[0]} : @_;
86 $args{'tradition'} = $self;
87 $args{'language'} = $self->language
88 if( $self->language && !exists $args{'language'} );
89 my $new_wit = Text::Tradition::Witness->new( %args );
90 $self->$orig( $new_wit->sigil => $new_wit );
94 # Allow deletion of witness by object as well as by sigil
95 around 'del_witness' => sub {
99 foreach my $arg ( @_ ) {
101 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
103 return $self->$orig( @key_args );
106 # Don't allow an empty hash value
107 around 'witness' => sub {
108 my( $orig, $self, $arg ) = @_;
109 return unless $self->has_witness( $arg );
110 return $self->$orig( $arg );
115 Text::Tradition - a software model for a set of collated texts
120 my $t = Text::Tradition->new(
121 'name' => 'this is a text',
123 'file' => '/path/to/tei_parallel_seg_file.xml' );
125 my @text_wits = $t->witnesses();
126 my $manuscript_a = $t->witness( 'A' );
128 $t = Text::Tradition->new();
129 $t->add_witness( 'sourcetype' => 'xmldesc',
130 'file' => '/path/to/teitranscription.xml' );
131 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
132 'string' => 'The quick brown fox jumped over the lazy dogs' );
136 my $text_path_svg = $t->collation->as_svg();
137 ## See Text::Tradition::Collation for more on text collation itself
141 Text::Tradition is a library for representation and analysis of collated
142 texts, particularly medieval ones. A 'tradition' refers to the aggregation
143 of surviving versions of a text, generally preserved in multiple
144 manuscripts (or 'witnesses'). A Tradition object thus has one more more
145 Witnesses, as well as a Collation that represents the unity of all versions
152 Creates and returns a new text tradition object. The following options are
159 =item B<name> - The name of the text.
163 Initialization based on a collation file:
167 =item B<input> - The input format of the collation file. Can be one of the
172 =item * Self - a GraphML format produced by this module
174 =item * CollateX - a GraphML format produced by CollateX
176 =item * CTE - a TEI XML format produced by Classical Text Editor
178 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
180 =item * KUL - a specific CSV format for variants, not documented here
182 =item * TEI - a TEI parallel segmentation format file
184 =item * Tabular - a comma- or tab-separated collation. Takes an additional
185 option, 'sep_char', which defaults to the tab character.
189 =item B<file> - The name of the file that contains the data. One of 'file'
190 or 'string' should be specified.
192 =item B<string> - A text string that contains the data. One of 'file' or
193 'string' should be specified.
195 =item B<base> - The name of a text file that contains the base text, to be
196 used with input formats that require it (currently only KUL).
200 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
204 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
205 objects that carry the text to be collated.
207 =item B<collator> - A reference to a collation program that will accept
214 Return the Text::Tradition::Witness objects associated with this tradition,
217 =head2 B<witness>( $sigil )
219 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
220 if there is no such object within the tradition.
222 =head2 B<add_witness>( %opts )
224 Instantiate a new witness with the given options (see documentation for
225 Text::Tradition::Witness) and add it to the tradition.
227 =head2 B<del_witness>( $sigil )
229 Delete the witness with the given sigil from the tradition. Returns the
230 witness object for the deleted witness.
234 use_ok( 'Text::Tradition', "can use module" );
236 my $t = Text::Tradition->new( 'name' => 'empty' );
237 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
238 is( $t->name, 'empty', "object has the right name" );
239 is( scalar $t->witnesses, 0, "object has no witnesses" );
241 my $simple = 't/data/simple.txt';
242 my $s = Text::Tradition->new(
244 'input' => 'Tabular',
247 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
248 is( $s->name, 'inline', "object has the right name" );
249 is( scalar $s->witnesses, 3, "object has three witnesses" );
251 my $wit_a = $s->witness('A');
252 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
254 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
256 is( $s->witness('X'), undef, "There is no witness X" );
257 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
259 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
260 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
261 is( $wit_d->sigil, 'D', "witness has correct sigil" );
262 is( scalar $s->witnesses, 4, "object now has four witnesses" );
264 my $del = $s->del_witness( 'D' );
265 is( $del, $wit_d, "Deleted correct witness" );
266 is( scalar $s->witnesses, 3, "object has three witnesses again" );
268 # TODO test initialization by witness list when we have it
276 my( $self, $init_args ) = @_;
278 # First, make a collation object. This will use only those arguments in
279 # init_args that apply to the collation.
280 my $collation = Text::Tradition::Collation->new( %$init_args,
281 'tradition' => $self );
282 $self->_save_collation( $collation );
284 if( exists $init_args->{'input'} ) {
285 # Call the appropriate parser on the given data
286 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
287 my @format_basetext = qw/ KUL /;
289 my $format = $init_args->{'input'};
290 if( $format && !( grep { $_ eq $format } @format_standalone )
291 && !( grep { $_ eq $format } @format_basetext ) ) {
292 warn "Unrecognized input format $format; not parsing";
295 if( $format && grep { $_ eq $format } @format_basetext ) {
297 if( !exists $init_args->{'base'} ) {
298 warn "Cannot make a collation from $format without a base text";
303 # Now do the parsing.
306 $format = 'BaseText'; # Use the BaseText module for parsing,
307 # but retain the original input arg.
309 my $mod = "Text::Tradition::Parser::$format";
311 $mod->can('parse')->( $self, $init_args );
314 $self->_init_done( 1 );
318 =head2 add_json_witnesses( $jsonstring, $options )
320 Adds a set of witnesses from a JSON array specification. This is a wrapper
321 to parse the JSON and call add_witness (with the specified $options) for
322 each element therein.
326 sub add_json_witnesses {
327 my( $self, $jsonstr, $extraopts ) = @_;
328 my $witarray = from_json( $jsonstr );
329 foreach my $witspec ( @{$witarray->{witnesses}} ) {
330 my $opts = $extraopts || {};
331 $opts->{'sourcetype'} = 'json';
332 $opts->{'object'} = $witspec;
333 $self->add_witness( $opts );
337 =head2 add_stemma( $dotfile )
339 Initializes a Text::Tradition::Stemma object from the given dotfile,
340 and associates it with the tradition.
346 my $t = Text::Tradition->new(
347 'name' => 'simple test',
348 'input' => 'Tabular',
349 'file' => 't/data/simple.txt',
352 is( $t->stemma_count, 0, "No stemmas added yet" );
354 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
355 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
356 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
357 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
367 if( $opts{'dotfile'} ) {
368 open $stemma_fh, '<', $opts{'dotfile'}
369 or warn "Could not open file " . $opts{'dotfile'};
370 } elsif( $opts{'dot'} ) {
371 my $str = $opts{'dot'};
372 open $stemma_fh, '<', \$str;
375 binmode $stemma_fh, ':utf8';
376 my $stemma = Text::Tradition::Stemma->new(
377 'collation' => $self->collation,
378 'dot' => $stemma_fh );
379 $self->_add_stemma( $stemma ) if $stemma;
385 unless( $self->has_language ) {
386 warn "Please set a language to lemmatize a tradition";
389 my $mod = "Text::Tradition::Language::" . $self->language;
391 $mod->can( 'lemmatize' )->( $self );
395 __PACKAGE__->meta->make_immutable;
402 =item * Allow tradition to be initialized via passing to a collator.
408 This package is free software and is provided "as is" without express
409 or implied warranty. You can redistribute it and/or modify it under
410 the same terms as Perl itself.
414 Tara L Andrews E<lt>aurum@cpan.orgE<gt>