allow multiple stemmas on a tradition
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
CommitLineData
dd3b58b0 1package Text::Tradition;
2
4a8828f0 3use Module::Load;
dd3b58b0 4use Moose;
8e1394aa 5use Text::Tradition::Collation;
56cf65bd 6use Text::Tradition::Stemma;
8e1394aa 7use Text::Tradition::Witness;
dd3b58b0 8
331c2dbf 9use vars qw( $VERSION );
10$VERSION = "0.1";
11
dd3b58b0 12has 'collation' => (
8e1394aa 13 is => 'ro',
14 isa => 'Text::Tradition::Collation',
15 writer => '_save_collation',
16 );
dd3b58b0 17
3b853983 18has 'witness_hash' => (
19 traits => ['Hash'],
20 isa => 'HashRef[Text::Tradition::Witness]',
8e1394aa 21 handles => {
3b853983 22 witness => 'get',
23 add_witness => 'set',
24 del_witness => 'delete',
25 has_witness => 'exists',
26 witnesses => 'values',
8e1394aa 27 },
3b853983 28 default => sub { {} },
8e1394aa 29 );
c5104dc0 30
df6d9812 31has 'name' => (
32 is => 'rw',
33 isa => 'Str',
34 default => 'Tradition',
35 );
56cf65bd 36
e0d617e6 37has 'stemmata' => (
38 traits => ['Array'],
39 isa => 'ArrayRef[Text::Tradition::Stemma]',
40 handles => {
41 all_stemmata => 'elements',
42 _add_stemma => 'push',
43 stemma => 'get',
44 stemma_count => 'count',
45 clear_stemmata => 'clear',
46 },
56cf65bd 47 );
3b853983 48
49# Create the witness before trying to add it
910a0a6d 50around 'add_witness' => sub {
51 my $orig = shift;
52 my $self = shift;
331c2dbf 53 # TODO allow add of a Witness object?
910a0a6d 54 my $new_wit = Text::Tradition::Witness->new( @_ );
3b853983 55 $self->$orig( $new_wit->sigil => $new_wit );
910a0a6d 56 return $new_wit;
57};
331c2dbf 58
3b853983 59# Allow deletion of witness by object as well as by sigil
60around 'del_witness' => sub {
61 my $orig = shift;
62 my $self = shift;
63 my @key_args;
64 foreach my $arg ( @_ ) {
65 push( @key_args,
66 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
67 }
68 return $self->$orig( @key_args );
69};
70
71# Don't allow an empty hash value
72around 'witness' => sub {
73 my( $orig, $self, $arg ) = @_;
74 return unless $self->has_witness( $arg );
75 return $self->$orig( $arg );
76};
77
331c2dbf 78=head1 NAME
79
80Text::Tradition - a software model for a set of collated texts
81
82=head1 SYNOPSIS
83
84 use Text::Tradition;
85 my $t = Text::Tradition->new(
86 'name' => 'this is a text',
87 'input' => 'TEI',
88 'file' => '/path/to/tei_parallel_seg_file.xml' );
89
90 my @text_wits = $t->witnesses();
91 my $manuscript_a = $t->witness( 'A' );
92 my $new_ms = $t->add_witness( 'sigil' => 'B' );
93
94 my $text_path_svg = $t->collation->as_svg();
95 ## See Text::Tradition::Collation for more on text collation itself
96
97=head1 DESCRIPTION
98
99Text::Tradition is a library for representation and analysis of collated
100texts, particularly medieval ones. A 'tradition' refers to the aggregation
101of surviving versions of a text, generally preserved in multiple
102manuscripts (or 'witnesses'). A Tradition object thus has one more more
103Witnesses, as well as a Collation that represents the unity of all versions
104of the text.
105
106=head1 METHODS
107
108=head2 new
109
110Creates and returns a new text tradition object. The following options are
111accepted.
112
113General options:
114
115=over 4
116
117=item B<name> - The name of the text.
118
119=back
120
121Initialization based on a collation file:
122
123=over 4
124
125=item B<input> - The input format of the collation file. Can be one of the
126following:
127
128=over 4
129
130=item * Self - a GraphML format produced by this module
131
132=item * CollateX - a GraphML format produced by CollateX
133
134=item * CTE - a TEI XML format produced by Classical Text Editor
135
a731e73a 136=item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
137
331c2dbf 138=item * KUL - a specific CSV format for variants, not documented here
139
140=item * TEI - a TEI parallel segmentation format file
141
142=item * Tabular - a comma- or tab-separated collation. Takes an additional
143option, 'sep_char', which defaults to the tab character.
144
145=back
146
147=item B<file> - The name of the file that contains the data. One of 'file'
148or 'string' should be specified.
149
150=item B<string> - A text string that contains the data. One of 'file' or
151'string' should be specified.
152
153=item B<base> - The name of a text file that contains the base text, to be
154used with input formats that require it (currently only KUL).
155
156=back
157
158Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
159
160=over 4
161
162=item B<witnesses> - A reference to an array of Text::Tradition::Witness
163objects that carry the text to be collated.
164
165=item B<collator> - A reference to a collation program that will accept
166Witness objects.
167
168=back
169
170=head2 B<witnesses>
171
172Return the Text::Tradition::Witness objects associated with this tradition,
173as an array.
174
044d1e45 175=head2 B<witness>( $sigil )
176
177Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
178if there is no such object within the tradition.
179
331c2dbf 180=head2 B<add_witness>( %opts )
181
182Instantiate a new witness with the given options (see documentation for
183Text::Tradition::Witness) and add it to the tradition.
184
044d1e45 185=head2 B<del_witness>( $sigil )
186
187Delete the witness with the given sigil from the tradition. Returns the
188witness object for the deleted witness.
189
331c2dbf 190=begin testing
191
192use_ok( 'Text::Tradition', "can use module" );
193
194my $t = Text::Tradition->new( 'name' => 'empty' );
195is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
196is( $t->name, 'empty', "object has the right name" );
197is( scalar $t->witnesses, 0, "object has no witnesses" );
198
199my $simple = 't/data/simple.txt';
200my $s = Text::Tradition->new(
201 'name' => 'inline',
202 'input' => 'Tabular',
203 'file' => $simple,
204 );
205is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
206is( $s->name, 'inline', "object has the right name" );
207is( scalar $s->witnesses, 3, "object has three witnesses" );
208
044d1e45 209my $wit_a = $s->witness('A');
210is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
211if( $wit_a ) {
212 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
213}
214is( $s->witness('X'), undef, "There is no witness X" );
215ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
216
217my $wit_d = $s->add_witness( 'sigil' => 'D' );
218is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
219is( $wit_d->sigil, 'D', "witness has correct sigil" );
331c2dbf 220is( scalar $s->witnesses, 4, "object now has four witnesses" );
221
3b853983 222my $del = $s->del_witness( 'D' );
044d1e45 223is( $del, $wit_d, "Deleted correct witness" );
3b853983 224is( scalar $s->witnesses, 3, "object has three witnesses again" );
225
331c2dbf 226# TODO test initialization by witness list when we have it
227
228=end testing
229
230=cut
910a0a6d 231
df6d9812 232
8e1394aa 233sub BUILD {
234 my( $self, $init_args ) = @_;
c5104dc0 235
8e1394aa 236 if( exists $init_args->{'witnesses'} ) {
910a0a6d 237 # We got passed an uncollated list of witnesses. Make a
238 # witness object for each witness, and then send them to the
239 # collator.
240 my $autosigil = 0;
241 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
242 # Each item in the list is either a string or an arrayref.
243 # If it's a string, it is a filename; if it's an arrayref,
244 # it is a tuple of 'sigil, file'. Handle either case.
245 my $args;
246 if( ref( $wit ) eq 'ARRAY' ) {
247 $args = { 'sigil' => $wit->[0],
248 'file' => $wit->[1] };
249 } else {
250 $args = { 'sigil' => chr( $autosigil+65 ),
251 'file' => $wit };
252 $autosigil++;
253 }
254 $self->witnesses->add_witness( $args );
255 # TODO Now how to collate these?
256 }
c5104dc0 257 } else {
910a0a6d 258 # Else we need to parse some collation data. Make a Collation object
259 my $collation = Text::Tradition::Collation->new( %$init_args,
260 'tradition' => $self );
261 $self->_save_collation( $collation );
4a8828f0 262
910a0a6d 263 # Call the appropriate parser on the given data
a731e73a 264 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
dfc37e38 265 my @format_basetext = qw/ KUL /;
266 my $use_base;
267 my $format = $init_args->{'input'};
dfc37e38 268 if( $format && !( grep { $_ eq $format } @format_standalone )
269 && !( grep { $_ eq $format } @format_basetext ) ) {
270 warn "Unrecognized input format $format; not parsing";
910a0a6d 271 return;
272 }
dfc37e38 273 if( $format && grep { $_ eq $format } @format_basetext ) {
274 $use_base = 1;
275 if( !exists $init_args->{'base'} ) {
276 warn "Cannot make a collation from $format without a base text";
277 return;
278 }
279 }
4a8828f0 280
910a0a6d 281 # Now do the parsing.
910a0a6d 282 if( $format ) {
dfc37e38 283 if( $use_base ) {
284 $format = 'BaseText'; # Use the BaseText module for parsing,
285 # but retain the original input arg.
910a0a6d 286 }
287 my $mod = "Text::Tradition::Parser::$format";
288 load( $mod );
dfc37e38 289 $mod->can('parse')->( $self, $init_args );
910a0a6d 290 }
c5104dc0 291 }
8e1394aa 292}
c5104dc0 293
56cf65bd 294=head2 add_stemma( $dotfile )
295
296Initializes a Text::Tradition::Stemma object from the given dotfile,
297and associates it with the tradition.
298
299=begin testing
300
301use Text::Tradition;
302
303my $t = Text::Tradition->new(
304 'name' => 'simple test',
305 'input' => 'Tabular',
306 'file' => 't/data/simple.txt',
307 );
308
309my $s;
9ba651b9 310ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
56cf65bd 311is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
e0d617e6 312is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
313is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
56cf65bd 314
315=end testing
316
317=cut
318
319sub add_stemma {
bffafb73 320 my $self = shift;
321 my %opts = @_;
322 my $stemma_fh;
323 if( $opts{'dotfile'} ) {
324 open $stemma_fh, '<', $opts{'dotfile'}
325 or warn "Could not open file " . $opts{'dotfile'};
326 } elsif( $opts{'dot'} ) {
327 my $str = $opts{'dot'};
328 open $stemma_fh, '<', \$str;
329 }
330 # Assume utf-8
331 binmode $stemma_fh, ':utf8';
56cf65bd 332 my $stemma = Text::Tradition::Stemma->new(
333 'collation' => $self->collation,
334 'dot' => $stemma_fh );
335 $self->_add_stemma( $stemma ) if $stemma;
336 return $stemma;
337}
338
dd3b58b0 339no Moose;
340__PACKAGE__->meta->make_immutable;
331c2dbf 341
342
343=head1 BUGS / TODO
344
345=over
346
347=item * Allow tradition to be initialized via passing to a collator.
348
349=back
350
351=head1 LICENSE
352
353This package is free software and is provided "as is" without express
354or implied warranty. You can redistribute it and/or modify it under
355the same terms as Perl itself.
356
357=head1 AUTHOR
358
359Tara L Andrews E<lt>aurum@cpan.orgE<gt>