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 # Create the witness before trying to add it
58 around 'add_witness' => sub {
61 # TODO allow add of a Witness object?
62 my %args = @_ == 1 ? %{$_[0]} : @_;
63 $args{'tradition'} = $self;
64 $args{'language'} = $self->language
65 if( $self->language && !exists $args{'language'} );
66 my $new_wit = Text::Tradition::Witness->new( %args );
67 $self->$orig( $new_wit->sigil => $new_wit );
71 # Allow deletion of witness by object as well as by sigil
72 around 'del_witness' => sub {
76 foreach my $arg ( @_ ) {
78 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
80 return $self->$orig( @key_args );
83 # Don't allow an empty hash value
84 around 'witness' => sub {
85 my( $orig, $self, $arg ) = @_;
86 return unless $self->has_witness( $arg );
87 return $self->$orig( $arg );
92 Text::Tradition - a software model for a set of collated texts
97 my $t = Text::Tradition->new(
98 'name' => 'this is a text',
100 'file' => '/path/to/tei_parallel_seg_file.xml' );
102 my @text_wits = $t->witnesses();
103 my $manuscript_a = $t->witness( 'A' );
104 my $new_ms = $t->add_witness( 'sigil' => 'B' );
106 my $text_path_svg = $t->collation->as_svg();
107 ## See Text::Tradition::Collation for more on text collation itself
111 Text::Tradition is a library for representation and analysis of collated
112 texts, particularly medieval ones. A 'tradition' refers to the aggregation
113 of surviving versions of a text, generally preserved in multiple
114 manuscripts (or 'witnesses'). A Tradition object thus has one more more
115 Witnesses, as well as a Collation that represents the unity of all versions
122 Creates and returns a new text tradition object. The following options are
129 =item B<name> - The name of the text.
133 Initialization based on a collation file:
137 =item B<input> - The input format of the collation file. Can be one of the
142 =item * Self - a GraphML format produced by this module
144 =item * CollateX - a GraphML format produced by CollateX
146 =item * CTE - a TEI XML format produced by Classical Text Editor
148 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
150 =item * KUL - a specific CSV format for variants, not documented here
152 =item * TEI - a TEI parallel segmentation format file
154 =item * Tabular - a comma- or tab-separated collation. Takes an additional
155 option, 'sep_char', which defaults to the tab character.
159 =item B<file> - The name of the file that contains the data. One of 'file'
160 or 'string' should be specified.
162 =item B<string> - A text string that contains the data. One of 'file' or
163 'string' should be specified.
165 =item B<base> - The name of a text file that contains the base text, to be
166 used with input formats that require it (currently only KUL).
170 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
174 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
175 objects that carry the text to be collated.
177 =item B<collator> - A reference to a collation program that will accept
184 Return the Text::Tradition::Witness objects associated with this tradition,
187 =head2 B<witness>( $sigil )
189 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
190 if there is no such object within the tradition.
192 =head2 B<add_witness>( %opts )
194 Instantiate a new witness with the given options (see documentation for
195 Text::Tradition::Witness) and add it to the tradition.
197 =head2 B<del_witness>( $sigil )
199 Delete the witness with the given sigil from the tradition. Returns the
200 witness object for the deleted witness.
204 use_ok( 'Text::Tradition', "can use module" );
206 my $t = Text::Tradition->new( 'name' => 'empty' );
207 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
208 is( $t->name, 'empty', "object has the right name" );
209 is( scalar $t->witnesses, 0, "object has no witnesses" );
211 my $simple = 't/data/simple.txt';
212 my $s = Text::Tradition->new(
214 'input' => 'Tabular',
217 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
218 is( $s->name, 'inline', "object has the right name" );
219 is( scalar $s->witnesses, 3, "object has three witnesses" );
221 my $wit_a = $s->witness('A');
222 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
224 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
226 is( $s->witness('X'), undef, "There is no witness X" );
227 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
229 my $wit_d = $s->add_witness( 'sigil' => 'D' );
230 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
231 is( $wit_d->sigil, 'D', "witness has correct sigil" );
232 is( scalar $s->witnesses, 4, "object now has four witnesses" );
234 my $del = $s->del_witness( 'D' );
235 is( $del, $wit_d, "Deleted correct witness" );
236 is( scalar $s->witnesses, 3, "object has three witnesses again" );
238 # TODO test initialization by witness list when we have it
246 my( $self, $init_args ) = @_;
248 # First, make a collation object. This will use only those arguments in
249 # init_args that apply to the collation.
250 my $collation = Text::Tradition::Collation->new( %$init_args,
251 'tradition' => $self );
252 $self->_save_collation( $collation );
254 if( exists $init_args->{'input'} ) {
255 # Call the appropriate parser on the given data
256 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
257 my @format_basetext = qw/ KUL /;
259 my $format = $init_args->{'input'};
260 if( $format && !( grep { $_ eq $format } @format_standalone )
261 && !( grep { $_ eq $format } @format_basetext ) ) {
262 warn "Unrecognized input format $format; not parsing";
265 if( $format && grep { $_ eq $format } @format_basetext ) {
267 if( !exists $init_args->{'base'} ) {
268 warn "Cannot make a collation from $format without a base text";
273 # Now do the parsing.
276 $format = 'BaseText'; # Use the BaseText module for parsing,
277 # but retain the original input arg.
279 my $mod = "Text::Tradition::Parser::$format";
281 $mod->can('parse')->( $self, $init_args );
287 =head2 add_json_witnesses( $jsonstring, $options )
289 Adds a set of witnesses from a JSON array specification. This is a wrapper
290 to parse the JSON and call add_witness (with the specified $options) for
291 each element therein.
295 sub add_json_witnesses {
296 my( $self, $jsonstr, $extraopts ) = @_;
297 my $witarray = from_json( $jsonstr );
298 foreach my $witspec ( @{$witarray->{witnesses}} ) {
299 my $opts = $extraopts || {};
300 $opts->{'sourcetype'} = 'json';
301 $opts->{'object'} = $witspec;
302 $self->add_witness( $opts );
306 =head2 add_stemma( $dotfile )
308 Initializes a Text::Tradition::Stemma object from the given dotfile,
309 and associates it with the tradition.
315 my $t = Text::Tradition->new(
316 'name' => 'simple test',
317 'input' => 'Tabular',
318 'file' => 't/data/simple.txt',
321 is( $t->stemma_count, 0, "No stemmas added yet" );
323 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
324 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
325 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
326 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
336 if( $opts{'dotfile'} ) {
337 open $stemma_fh, '<', $opts{'dotfile'}
338 or warn "Could not open file " . $opts{'dotfile'};
339 } elsif( $opts{'dot'} ) {
340 my $str = $opts{'dot'};
341 open $stemma_fh, '<', \$str;
344 binmode $stemma_fh, ':utf8';
345 my $stemma = Text::Tradition::Stemma->new(
346 'collation' => $self->collation,
347 'dot' => $stemma_fh );
348 $self->_add_stemma( $stemma ) if $stemma;
353 __PACKAGE__->meta->make_immutable;
360 =item * Allow tradition to be initialized via passing to a collator.
366 This package is free software and is provided "as is" without express
367 or implied warranty. You can redistribute it and/or modify it under
368 the same terms as Perl itself.
372 Tara L Andrews E<lt>aurum@cpan.orgE<gt>