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;
10 use vars qw( $VERSION );
15 isa => 'Text::Tradition::Collation',
16 writer => '_save_collation',
19 has 'witness_hash' => (
21 isa => 'HashRef[Text::Tradition::Witness]',
25 del_witness => 'delete',
26 has_witness => 'exists',
27 witnesses => 'values',
29 default => sub { {} },
35 default => 'Tradition',
41 predicate => 'has_language',
46 isa => 'ArrayRef[Text::Tradition::Stemma]',
48 stemmata => 'elements',
49 _add_stemma => 'push',
51 stemma_count => 'count',
52 clear_stemmata => 'clear',
54 default => sub { [] },
57 has '_initialized' => (
61 writer => '_init_done',
64 # Create the witness before trying to add it
65 around 'add_witness' => sub {
68 # TODO allow add of a Witness object?
69 my %args = @_ == 1 ? %{$_[0]} : @_;
70 $args{'tradition'} = $self;
71 $args{'language'} = $self->language
72 if( $self->language && !exists $args{'language'} );
73 my $new_wit = Text::Tradition::Witness->new( %args );
74 $self->$orig( $new_wit->sigil => $new_wit );
78 # Allow deletion of witness by object as well as by sigil
79 around 'del_witness' => sub {
83 foreach my $arg ( @_ ) {
85 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
87 return $self->$orig( @key_args );
90 # Don't allow an empty hash value
91 around 'witness' => sub {
92 my( $orig, $self, $arg ) = @_;
93 return unless $self->has_witness( $arg );
94 return $self->$orig( $arg );
99 Text::Tradition - a software model for a set of collated texts
104 my $t = Text::Tradition->new(
105 'name' => 'this is a text',
107 'file' => '/path/to/tei_parallel_seg_file.xml' );
109 my @text_wits = $t->witnesses();
110 my $manuscript_a = $t->witness( 'A' );
112 $t = Text::Tradition->new();
113 $t->add_witness( 'sourcetype' => 'xmldesc',
114 'file' => '/path/to/teitranscription.xml' );
115 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
116 'string' => 'The quick brown fox jumped over the lazy dogs' );
120 my $text_path_svg = $t->collation->as_svg();
121 ## See Text::Tradition::Collation for more on text collation itself
125 Text::Tradition is a library for representation and analysis of collated
126 texts, particularly medieval ones. A 'tradition' refers to the aggregation
127 of surviving versions of a text, generally preserved in multiple
128 manuscripts (or 'witnesses'). A Tradition object thus has one more more
129 Witnesses, as well as a Collation that represents the unity of all versions
136 Creates and returns a new text tradition object. The following options are
143 =item B<name> - The name of the text.
147 Initialization based on a collation file:
151 =item B<input> - The input format of the collation file. Can be one of the
156 =item * Self - a GraphML format produced by this module
158 =item * CollateX - a GraphML format produced by CollateX
160 =item * CTE - a TEI XML format produced by Classical Text Editor
162 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
164 =item * KUL - a specific CSV format for variants, not documented here
166 =item * TEI - a TEI parallel segmentation format file
168 =item * Tabular - a comma- or tab-separated collation. Takes an additional
169 option, 'sep_char', which defaults to the tab character.
173 =item B<file> - The name of the file that contains the data. One of 'file'
174 or 'string' should be specified.
176 =item B<string> - A text string that contains the data. One of 'file' or
177 'string' should be specified.
179 =item B<base> - The name of a text file that contains the base text, to be
180 used with input formats that require it (currently only KUL).
184 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
188 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
189 objects that carry the text to be collated.
191 =item B<collator> - A reference to a collation program that will accept
198 Return the Text::Tradition::Witness objects associated with this tradition,
201 =head2 B<witness>( $sigil )
203 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
204 if there is no such object within the tradition.
206 =head2 B<add_witness>( %opts )
208 Instantiate a new witness with the given options (see documentation for
209 Text::Tradition::Witness) and add it to the tradition.
211 =head2 B<del_witness>( $sigil )
213 Delete the witness with the given sigil from the tradition. Returns the
214 witness object for the deleted witness.
218 use_ok( 'Text::Tradition', "can use module" );
220 my $t = Text::Tradition->new( 'name' => 'empty' );
221 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
222 is( $t->name, 'empty', "object has the right name" );
223 is( scalar $t->witnesses, 0, "object has no witnesses" );
225 my $simple = 't/data/simple.txt';
226 my $s = Text::Tradition->new(
228 'input' => 'Tabular',
231 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
232 is( $s->name, 'inline', "object has the right name" );
233 is( scalar $s->witnesses, 3, "object has three witnesses" );
235 my $wit_a = $s->witness('A');
236 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
238 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
240 is( $s->witness('X'), undef, "There is no witness X" );
241 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
243 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
244 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
245 is( $wit_d->sigil, 'D', "witness has correct sigil" );
246 is( scalar $s->witnesses, 4, "object now has four witnesses" );
248 my $del = $s->del_witness( 'D' );
249 is( $del, $wit_d, "Deleted correct witness" );
250 is( scalar $s->witnesses, 3, "object has three witnesses again" );
252 # TODO test initialization by witness list when we have it
260 my( $self, $init_args ) = @_;
262 # First, make a collation object. This will use only those arguments in
263 # init_args that apply to the collation.
264 my $collation = Text::Tradition::Collation->new( %$init_args,
265 'tradition' => $self );
266 $self->_save_collation( $collation );
268 if( exists $init_args->{'input'} ) {
269 # Call the appropriate parser on the given data
270 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
271 my @format_basetext = qw/ KUL /;
273 my $format = $init_args->{'input'};
274 if( $format && !( grep { $_ eq $format } @format_standalone )
275 && !( grep { $_ eq $format } @format_basetext ) ) {
276 warn "Unrecognized input format $format; not parsing";
279 if( $format && grep { $_ eq $format } @format_basetext ) {
281 if( !exists $init_args->{'base'} ) {
282 warn "Cannot make a collation from $format without a base text";
287 # Now do the parsing.
290 $format = 'BaseText'; # Use the BaseText module for parsing,
291 # but retain the original input arg.
293 my $mod = "Text::Tradition::Parser::$format";
295 $mod->can('parse')->( $self, $init_args );
298 $self->_init_done( 1 );
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>