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',
78 default => sub { 0; },
81 # Create the witness before trying to add it
82 around 'add_witness' => sub {
85 # TODO allow add of a Witness object?
86 my %args = @_ == 1 ? %{$_[0]} : @_;
87 $args{'tradition'} = $self;
88 $args{'language'} = $self->language
89 if( $self->language && !exists $args{'language'} );
90 my $new_wit = Text::Tradition::Witness->new( %args );
91 $self->$orig( $new_wit->sigil => $new_wit );
95 # Allow deletion of witness by object as well as by sigil
96 around 'del_witness' => sub {
100 foreach my $arg ( @_ ) {
102 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
104 return $self->$orig( @key_args );
107 # Don't allow an empty hash value
108 around 'witness' => sub {
109 my( $orig, $self, $arg ) = @_;
110 return unless $self->has_witness( $arg );
111 return $self->$orig( $arg );
116 Text::Tradition - a software model for a set of collated texts
121 my $t = Text::Tradition->new(
122 'name' => 'this is a text',
124 'file' => '/path/to/tei_parallel_seg_file.xml' );
126 my @text_wits = $t->witnesses();
127 my $manuscript_a = $t->witness( 'A' );
129 $t = Text::Tradition->new();
130 $t->add_witness( 'sourcetype' => 'xmldesc',
131 'file' => '/path/to/teitranscription.xml' );
132 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
133 'string' => 'The quick brown fox jumped over the lazy dogs' );
137 my $text_path_svg = $t->collation->as_svg();
138 ## See Text::Tradition::Collation for more on text collation itself
142 Text::Tradition is a library for representation and analysis of collated
143 texts, particularly medieval ones. A 'tradition' refers to the aggregation
144 of surviving versions of a text, generally preserved in multiple
145 manuscripts (or 'witnesses'). A Tradition object thus has one more more
146 Witnesses, as well as a Collation that represents the unity of all versions
153 Creates and returns a new text tradition object. The following options are
160 =item B<name> - The name of the text.
164 Initialization based on a collation file:
168 =item B<input> - The input format of the collation file. Can be one of the
173 =item * Self - a GraphML format produced by this module
175 =item * CollateX - a GraphML format produced by CollateX
177 =item * CTE - a TEI XML format produced by Classical Text Editor
179 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
181 =item * KUL - a specific CSV format for variants, not documented here
183 =item * TEI - a TEI parallel segmentation format file
185 =item * Tabular - a comma- or tab-separated collation. Takes an additional
186 option, 'sep_char', which defaults to the tab character.
190 =item B<file> - The name of the file that contains the data. One of 'file'
191 or 'string' should be specified.
193 =item B<string> - A text string that contains the data. One of 'file' or
194 'string' should be specified.
196 =item B<base> - The name of a text file that contains the base text, to be
197 used with input formats that require it (currently only KUL).
201 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
205 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
206 objects that carry the text to be collated.
208 =item B<collator> - A reference to a collation program that will accept
215 Return the Text::Tradition::Witness objects associated with this tradition,
218 =head2 B<witness>( $sigil )
220 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
221 if there is no such object within the tradition.
223 =head2 B<add_witness>( %opts )
225 Instantiate a new witness with the given options (see documentation for
226 Text::Tradition::Witness) and add it to the tradition.
228 =head2 B<del_witness>( $sigil )
230 Delete the witness with the given sigil from the tradition. Returns the
231 witness object for the deleted witness.
235 use_ok( 'Text::Tradition', "can use module" );
237 my $t = Text::Tradition->new( 'name' => 'empty' );
238 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
239 is( $t->name, 'empty', "object has the right name" );
240 is( scalar $t->witnesses, 0, "object has no witnesses" );
242 my $simple = 't/data/simple.txt';
243 my $s = Text::Tradition->new(
245 'input' => 'Tabular',
248 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
249 is( $s->name, 'inline', "object has the right name" );
250 is( scalar $s->witnesses, 3, "object has three witnesses" );
252 my $wit_a = $s->witness('A');
253 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
255 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
257 is( $s->witness('X'), undef, "There is no witness X" );
258 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
260 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
261 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
262 is( $wit_d->sigil, 'D', "witness has correct sigil" );
263 is( scalar $s->witnesses, 4, "object now has four witnesses" );
265 my $del = $s->del_witness( 'D' );
266 is( $del, $wit_d, "Deleted correct witness" );
267 is( scalar $s->witnesses, 3, "object has three witnesses again" );
269 # TODO test initialization by witness list when we have it
277 my( $self, $init_args ) = @_;
279 # First, make a collation object. This will use only those arguments in
280 # init_args that apply to the collation.
281 my $collation = Text::Tradition::Collation->new( %$init_args,
282 'tradition' => $self );
283 $self->_save_collation( $collation );
285 if( exists $init_args->{'input'} ) {
286 # Call the appropriate parser on the given data
287 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
288 my @format_basetext = qw/ KUL /;
290 my $format = $init_args->{'input'};
291 if( $format && !( grep { $_ eq $format } @format_standalone )
292 && !( grep { $_ eq $format } @format_basetext ) ) {
293 warn "Unrecognized input format $format; not parsing";
296 if( $format && grep { $_ eq $format } @format_basetext ) {
298 if( !exists $init_args->{'base'} ) {
299 warn "Cannot make a collation from $format without a base text";
304 # Now do the parsing.
307 $format = 'BaseText'; # Use the BaseText module for parsing,
308 # but retain the original input arg.
310 my $mod = "Text::Tradition::Parser::$format";
312 $mod->can('parse')->( $self, $init_args );
315 $self->_init_done( 1 );
319 =head2 add_json_witnesses( $jsonstring, $options )
321 Adds a set of witnesses from a JSON array specification. This is a wrapper
322 to parse the JSON and call add_witness (with the specified $options) for
323 each element therein.
327 sub add_json_witnesses {
328 my( $self, $jsonstr, $extraopts ) = @_;
329 my $witarray = from_json( $jsonstr );
330 foreach my $witspec ( @{$witarray->{witnesses}} ) {
331 my $opts = $extraopts || {};
332 $opts->{'sourcetype'} = 'json';
333 $opts->{'object'} = $witspec;
334 $self->add_witness( $opts );
338 =head2 add_stemma( $dotfile )
340 Initializes a Text::Tradition::Stemma object from the given dotfile,
341 and associates it with the tradition.
347 my $t = Text::Tradition->new(
348 'name' => 'simple test',
349 'input' => 'Tabular',
350 'file' => 't/data/simple.txt',
353 is( $t->stemma_count, 0, "No stemmas added yet" );
355 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
356 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
357 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
358 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
368 if( $opts{'dotfile'} ) {
369 open $stemma_fh, '<', $opts{'dotfile'}
370 or warn "Could not open file " . $opts{'dotfile'};
371 } elsif( $opts{'dot'} ) {
372 my $str = $opts{'dot'};
373 open $stemma_fh, '<', \$str;
376 binmode $stemma_fh, ':utf8';
377 my $stemma = Text::Tradition::Stemma->new(
378 'collation' => $self->collation,
379 'dot' => $stemma_fh );
380 $self->_add_stemma( $stemma ) if $stemma;
386 unless( $self->has_language ) {
387 warn "Please set a language to lemmatize a tradition";
390 my $mod = "Text::Tradition::Language::" . $self->language;
392 $mod->can( 'lemmatize' )->( $self );
396 __PACKAGE__->meta->make_immutable;
403 =item * Allow tradition to be initialized via passing to a collator.
409 This package is free software and is provided "as is" without express
410 or implied warranty. You can redistribute it and/or modify it under
411 the same terms as Perl itself.
415 Tara L Andrews E<lt>aurum@cpan.orgE<gt>