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',
63 clearer => 'clear_user',
71 default => sub { 0; },
74 # Create the witness before trying to add it
75 around 'add_witness' => sub {
78 # TODO allow add of a Witness object?
79 my %args = @_ == 1 ? %{$_[0]} : @_;
80 $args{'tradition'} = $self;
81 $args{'language'} = $self->language
82 if( $self->language && !exists $args{'language'} );
83 my $new_wit = Text::Tradition::Witness->new( %args );
84 $self->$orig( $new_wit->sigil => $new_wit );
88 # Allow deletion of witness by object as well as by sigil
89 around 'del_witness' => sub {
93 foreach my $arg ( @_ ) {
95 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
97 return $self->$orig( @key_args );
100 # Don't allow an empty hash value
101 around 'witness' => sub {
102 my( $orig, $self, $arg ) = @_;
103 return unless $self->has_witness( $arg );
104 return $self->$orig( $arg );
109 Text::Tradition - a software model for a set of collated texts
114 my $t = Text::Tradition->new(
115 'name' => 'this is a text',
117 'file' => '/path/to/tei_parallel_seg_file.xml' );
119 my @text_wits = $t->witnesses();
120 my $manuscript_a = $t->witness( 'A' );
122 $t = Text::Tradition->new();
123 $t->add_witness( 'sourcetype' => 'xmldesc',
124 'file' => '/path/to/teitranscription.xml' );
125 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
126 'string' => 'The quick brown fox jumped over the lazy dogs' );
130 my $text_path_svg = $t->collation->as_svg();
131 ## See Text::Tradition::Collation for more on text collation itself
135 Text::Tradition is a library for representation and analysis of collated
136 texts, particularly medieval ones. A 'tradition' refers to the aggregation
137 of surviving versions of a text, generally preserved in multiple
138 manuscripts (or 'witnesses'). A Tradition object thus has one more more
139 Witnesses, as well as a Collation that represents the unity of all versions
146 Creates and returns a new text tradition object. The following options are
153 =item B<name> - The name of the text.
157 Initialization based on a collation file:
161 =item B<input> - The input format of the collation file. Can be one of the
166 =item * Self - a GraphML format produced by this module
168 =item * CollateX - a GraphML format produced by CollateX
170 =item * CTE - a TEI XML format produced by Classical Text Editor
172 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
174 =item * KUL - a specific CSV format for variants, not documented here
176 =item * TEI - a TEI parallel segmentation format file
178 =item * Tabular - a comma- or tab-separated collation. Takes an additional
179 option, 'sep_char', which defaults to the tab character.
183 =item B<file> - The name of the file that contains the data. One of 'file'
184 or 'string' should be specified.
186 =item B<string> - A text string that contains the data. One of 'file' or
187 'string' should be specified.
189 =item B<base> - The name of a text file that contains the base text, to be
190 used with input formats that require it (currently only KUL).
194 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
198 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
199 objects that carry the text to be collated.
201 =item B<collator> - A reference to a collation program that will accept
208 Return the Text::Tradition::Witness objects associated with this tradition,
211 =head2 B<witness>( $sigil )
213 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
214 if there is no such object within the tradition.
216 =head2 B<add_witness>( %opts )
218 Instantiate a new witness with the given options (see documentation for
219 Text::Tradition::Witness) and add it to the tradition.
221 =head2 B<del_witness>( $sigil )
223 Delete the witness with the given sigil from the tradition. Returns the
224 witness object for the deleted witness.
228 use_ok( 'Text::Tradition', "can use module" );
230 my $t = Text::Tradition->new( 'name' => 'empty' );
231 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
232 is( $t->name, 'empty', "object has the right name" );
233 is( scalar $t->witnesses, 0, "object has no witnesses" );
235 my $simple = 't/data/simple.txt';
236 my $s = Text::Tradition->new(
238 'input' => 'Tabular',
241 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
242 is( $s->name, 'inline', "object has the right name" );
243 is( scalar $s->witnesses, 3, "object has three witnesses" );
245 my $wit_a = $s->witness('A');
246 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
248 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
250 is( $s->witness('X'), undef, "There is no witness X" );
251 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
253 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
254 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
255 is( $wit_d->sigil, 'D', "witness has correct sigil" );
256 is( scalar $s->witnesses, 4, "object now has four witnesses" );
258 my $del = $s->del_witness( 'D' );
259 is( $del, $wit_d, "Deleted correct witness" );
260 is( scalar $s->witnesses, 3, "object has three witnesses again" );
262 # TODO test initialization by witness list when we have it
270 my( $self, $init_args ) = @_;
272 # First, make a collation object. This will use only those arguments in
273 # init_args that apply to the collation.
274 my $collation = Text::Tradition::Collation->new( %$init_args,
275 'tradition' => $self );
276 $self->_save_collation( $collation );
278 if( exists $init_args->{'input'} ) {
279 # Call the appropriate parser on the given data
280 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
281 my @format_basetext = qw/ KUL /;
283 my $format = $init_args->{'input'};
284 if( $format && !( grep { $_ eq $format } @format_standalone )
285 && !( grep { $_ eq $format } @format_basetext ) ) {
286 warn "Unrecognized input format $format; not parsing";
289 if( $format && grep { $_ eq $format } @format_basetext ) {
291 if( !exists $init_args->{'base'} ) {
292 warn "Cannot make a collation from $format without a base text";
297 # Now do the parsing.
300 $format = 'BaseText'; # Use the BaseText module for parsing,
301 # but retain the original input arg.
303 my $mod = "Text::Tradition::Parser::$format";
305 $mod->can('parse')->( $self, $init_args );
311 =head2 add_json_witnesses( $jsonstring, $options )
313 Adds a set of witnesses from a JSON array specification. This is a wrapper
314 to parse the JSON and call add_witness (with the specified $options) for
315 each element therein.
319 sub add_json_witnesses {
320 my( $self, $jsonstr, $extraopts ) = @_;
321 my $witarray = from_json( $jsonstr );
322 foreach my $witspec ( @{$witarray->{witnesses}} ) {
323 my $opts = $extraopts || {};
324 $opts->{'sourcetype'} = 'json';
325 $opts->{'object'} = $witspec;
326 $self->add_witness( $opts );
330 =head2 add_stemma( $dotfile )
332 Initializes a Text::Tradition::Stemma object from the given dotfile,
333 and associates it with the tradition.
339 my $t = Text::Tradition->new(
340 'name' => 'simple test',
341 'input' => 'Tabular',
342 'file' => 't/data/simple.txt',
345 is( $t->stemma_count, 0, "No stemmas added yet" );
347 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
348 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
349 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
350 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
360 if( $opts{'dotfile'} ) {
361 open $stemma_fh, '<', $opts{'dotfile'}
362 or warn "Could not open file " . $opts{'dotfile'};
363 } elsif( $opts{'dot'} ) {
364 my $str = $opts{'dot'};
365 open $stemma_fh, '<', \$str;
368 binmode $stemma_fh, ':utf8';
369 my $stemma = Text::Tradition::Stemma->new(
370 'collation' => $self->collation,
371 'dot' => $stemma_fh );
372 $self->_add_stemma( $stemma ) if $stemma;
378 unless( $self->has_language ) {
379 warn "Please set a language to lemmatize a tradition";
382 my $mod = "Text::Tradition::Language::" . $self->language;
384 $mod->can( 'lemmatize' )->( $self );
388 __PACKAGE__->meta->make_immutable;
395 =item * Allow tradition to be initialized via passing to a collator.
401 This package is free software and is provided "as is" without express
402 or implied warranty. You can redistribute it and/or modify it under
403 the same terms as Perl itself.
407 Tara L Andrews E<lt>aurum@cpan.orgE<gt>