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' );
105 $t = Text::Tradition->new();
106 $t->add_witness( 'sourcetype' => 'xmldesc',
107 'file' => '/path/to/teitranscription.xml' );
108 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
109 'string' => 'The quick brown fox jumped over the lazy dogs' );
113 my $text_path_svg = $t->collation->as_svg();
114 ## See Text::Tradition::Collation for more on text collation itself
118 Text::Tradition is a library for representation and analysis of collated
119 texts, particularly medieval ones. A 'tradition' refers to the aggregation
120 of surviving versions of a text, generally preserved in multiple
121 manuscripts (or 'witnesses'). A Tradition object thus has one more more
122 Witnesses, as well as a Collation that represents the unity of all versions
129 Creates and returns a new text tradition object. The following options are
136 =item B<name> - The name of the text.
140 Initialization based on a collation file:
144 =item B<input> - The input format of the collation file. Can be one of the
149 =item * Self - a GraphML format produced by this module
151 =item * CollateX - a GraphML format produced by CollateX
153 =item * CTE - a TEI XML format produced by Classical Text Editor
155 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
157 =item * KUL - a specific CSV format for variants, not documented here
159 =item * TEI - a TEI parallel segmentation format file
161 =item * Tabular - a comma- or tab-separated collation. Takes an additional
162 option, 'sep_char', which defaults to the tab character.
166 =item B<file> - The name of the file that contains the data. One of 'file'
167 or 'string' should be specified.
169 =item B<string> - A text string that contains the data. One of 'file' or
170 'string' should be specified.
172 =item B<base> - The name of a text file that contains the base text, to be
173 used with input formats that require it (currently only KUL).
177 Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
181 =item B<witnesses> - A reference to an array of Text::Tradition::Witness
182 objects that carry the text to be collated.
184 =item B<collator> - A reference to a collation program that will accept
191 Return the Text::Tradition::Witness objects associated with this tradition,
194 =head2 B<witness>( $sigil )
196 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
197 if there is no such object within the tradition.
199 =head2 B<add_witness>( %opts )
201 Instantiate a new witness with the given options (see documentation for
202 Text::Tradition::Witness) and add it to the tradition.
204 =head2 B<del_witness>( $sigil )
206 Delete the witness with the given sigil from the tradition. Returns the
207 witness object for the deleted witness.
211 use_ok( 'Text::Tradition', "can use module" );
213 my $t = Text::Tradition->new( 'name' => 'empty' );
214 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
215 is( $t->name, 'empty', "object has the right name" );
216 is( scalar $t->witnesses, 0, "object has no witnesses" );
218 my $simple = 't/data/simple.txt';
219 my $s = Text::Tradition->new(
221 'input' => 'Tabular',
224 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
225 is( $s->name, 'inline', "object has the right name" );
226 is( scalar $s->witnesses, 3, "object has three witnesses" );
228 my $wit_a = $s->witness('A');
229 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
231 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
233 is( $s->witness('X'), undef, "There is no witness X" );
234 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
236 my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
237 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
238 is( $wit_d->sigil, 'D', "witness has correct sigil" );
239 is( scalar $s->witnesses, 4, "object now has four witnesses" );
241 my $del = $s->del_witness( 'D' );
242 is( $del, $wit_d, "Deleted correct witness" );
243 is( scalar $s->witnesses, 3, "object has three witnesses again" );
245 # TODO test initialization by witness list when we have it
253 my( $self, $init_args ) = @_;
255 # First, make a collation object. This will use only those arguments in
256 # init_args that apply to the collation.
257 my $collation = Text::Tradition::Collation->new( %$init_args,
258 'tradition' => $self );
259 $self->_save_collation( $collation );
261 if( exists $init_args->{'input'} ) {
262 # Call the appropriate parser on the given data
263 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
264 my @format_basetext = qw/ KUL /;
266 my $format = $init_args->{'input'};
267 if( $format && !( grep { $_ eq $format } @format_standalone )
268 && !( grep { $_ eq $format } @format_basetext ) ) {
269 warn "Unrecognized input format $format; not parsing";
272 if( $format && grep { $_ eq $format } @format_basetext ) {
274 if( !exists $init_args->{'base'} ) {
275 warn "Cannot make a collation from $format without a base text";
280 # Now do the parsing.
283 $format = 'BaseText'; # Use the BaseText module for parsing,
284 # but retain the original input arg.
286 my $mod = "Text::Tradition::Parser::$format";
288 $mod->can('parse')->( $self, $init_args );
294 =head2 add_json_witnesses( $jsonstring, $options )
296 Adds a set of witnesses from a JSON array specification. This is a wrapper
297 to parse the JSON and call add_witness (with the specified $options) for
298 each element therein.
302 sub add_json_witnesses {
303 my( $self, $jsonstr, $extraopts ) = @_;
304 my $witarray = from_json( $jsonstr );
305 foreach my $witspec ( @{$witarray->{witnesses}} ) {
306 my $opts = $extraopts || {};
307 $opts->{'sourcetype'} = 'json';
308 $opts->{'object'} = $witspec;
309 $self->add_witness( $opts );
313 =head2 add_stemma( $dotfile )
315 Initializes a Text::Tradition::Stemma object from the given dotfile,
316 and associates it with the tradition.
322 my $t = Text::Tradition->new(
323 'name' => 'simple test',
324 'input' => 'Tabular',
325 'file' => 't/data/simple.txt',
328 is( $t->stemma_count, 0, "No stemmas added yet" );
330 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
331 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
332 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
333 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
343 if( $opts{'dotfile'} ) {
344 open $stemma_fh, '<', $opts{'dotfile'}
345 or warn "Could not open file " . $opts{'dotfile'};
346 } elsif( $opts{'dot'} ) {
347 my $str = $opts{'dot'};
348 open $stemma_fh, '<', \$str;
351 binmode $stemma_fh, ':utf8';
352 my $stemma = Text::Tradition::Stemma->new(
353 'collation' => $self->collation,
354 'dot' => $stemma_fh );
355 $self->_add_stemma( $stemma ) if $stemma;
360 __PACKAGE__->meta->make_immutable;
367 =item * Allow tradition to be initialized via passing to a collator.
373 This package is free software and is provided "as is" without express
374 or implied warranty. You can redistribute it and/or modify it under
375 the same terms as Perl itself.
379 Tara L Andrews E<lt>aurum@cpan.orgE<gt>