allow multiple stemmas on a tradition
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
1 package Text::Tradition;
2
3 use Module::Load;
4 use Moose;
5 use Text::Tradition::Collation;
6 use Text::Tradition::Stemma;
7 use Text::Tradition::Witness;
8
9 use vars qw( $VERSION );
10 $VERSION = "0.1";
11
12 has 'collation' => (
13     is => 'ro',
14     isa => 'Text::Tradition::Collation',
15     writer => '_save_collation',
16     );
17
18 has 'witness_hash' => (
19     traits => ['Hash'],
20     isa => 'HashRef[Text::Tradition::Witness]',
21     handles => {
22         witness     => 'get',
23         add_witness => 'set',
24         del_witness => 'delete',
25         has_witness => 'exists',
26         witnesses   => 'values',
27     },
28     default => sub { {} },
29     );
30
31 has 'name' => (
32     is => 'rw',
33     isa => 'Str',
34     default => 'Tradition',
35     );
36     
37 has '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         },
47         );
48   
49 # Create the witness before trying to add it
50 around 'add_witness' => sub {
51     my $orig = shift;
52     my $self = shift;
53     # TODO allow add of a Witness object?
54     my $new_wit = Text::Tradition::Witness->new( @_ );
55     $self->$orig( $new_wit->sigil => $new_wit );
56     return $new_wit;
57 };
58
59 # Allow deletion of witness by object as well as by sigil
60 around '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
72 around 'witness' => sub {
73     my( $orig, $self, $arg ) = @_;
74     return unless $self->has_witness( $arg );
75     return $self->$orig( $arg );
76 };
77
78 =head1 NAME
79
80 Text::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
99 Text::Tradition is a library for representation and analysis of collated
100 texts, particularly medieval ones.  A 'tradition' refers to the aggregation
101 of surviving versions of a text, generally preserved in multiple
102 manuscripts (or 'witnesses').  A Tradition object thus has one more more
103 Witnesses, as well as a Collation that represents the unity of all versions
104 of the text.
105
106 =head1 METHODS
107
108 =head2 new
109
110 Creates and returns a new text tradition object.  The following options are
111 accepted.
112
113 General options:
114
115 =over 4
116
117 =item B<name> - The name of the text.
118
119 =back
120
121 Initialization 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
126 following:
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
136 =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
137
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
143 option, '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'
148 or '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
154 used with input formats that require it (currently only KUL).
155
156 =back
157
158 Initialization 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
163 objects that carry the text to be collated.
164
165 =item B<collator> - A reference to a collation program that will accept
166 Witness objects.
167
168 =back
169
170 =head2 B<witnesses>
171
172 Return the Text::Tradition::Witness objects associated with this tradition,
173 as an array.
174
175 =head2 B<witness>( $sigil )
176
177 Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
178 if there is no such object within the tradition.
179
180 =head2 B<add_witness>( %opts )
181
182 Instantiate a new witness with the given options (see documentation for
183 Text::Tradition::Witness) and add it to the tradition.
184
185 =head2 B<del_witness>( $sigil )
186
187 Delete the witness with the given sigil from the tradition.  Returns the
188 witness object for the deleted witness.
189
190 =begin testing
191
192 use_ok( 'Text::Tradition', "can use module" );
193
194 my $t = Text::Tradition->new( 'name' => 'empty' );
195 is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
196 is( $t->name, 'empty', "object has the right name" );
197 is( scalar $t->witnesses, 0, "object has no witnesses" );
198
199 my $simple = 't/data/simple.txt';
200 my $s = Text::Tradition->new( 
201     'name'  => 'inline', 
202     'input' => 'Tabular',
203     'file'  => $simple,
204     );
205 is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
206 is( $s->name, 'inline', "object has the right name" );
207 is( scalar $s->witnesses, 3, "object has three witnesses" );
208
209 my $wit_a = $s->witness('A');
210 is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
211 if( $wit_a ) {
212     is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
213 }
214 is( $s->witness('X'), undef, "There is no witness X" );
215 ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
216
217 my $wit_d = $s->add_witness( 'sigil' => 'D' );
218 is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
219 is( $wit_d->sigil, 'D', "witness has correct sigil" );
220 is( scalar $s->witnesses, 4, "object now has four witnesses" );
221
222 my $del = $s->del_witness( 'D' );
223 is( $del, $wit_d, "Deleted correct witness" );
224 is( scalar $s->witnesses, 3, "object has three witnesses again" );
225
226 # TODO test initialization by witness list when we have it
227
228 =end testing
229
230 =cut
231     
232
233 sub BUILD {
234     my( $self, $init_args ) = @_;
235
236     if( exists $init_args->{'witnesses'} ) {
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         }
257     } else {
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 );
262
263         # Call the appropriate parser on the given data
264         my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
265         my @format_basetext = qw/ KUL /;
266         my $use_base;
267         my $format = $init_args->{'input'};
268         if( $format && !( grep { $_ eq $format } @format_standalone )
269             && !( grep { $_ eq $format } @format_basetext ) ) {
270             warn "Unrecognized input format $format; not parsing";
271             return;
272         }
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         }
280
281         # Now do the parsing. 
282         if( $format ) {
283             if( $use_base ) { 
284                 $format = 'BaseText';   # Use the BaseText module for parsing,
285                                         # but retain the original input arg.
286             }
287             my $mod = "Text::Tradition::Parser::$format";
288             load( $mod );
289             $mod->can('parse')->( $self, $init_args );
290         }
291     }
292 }
293
294 =head2 add_stemma( $dotfile )
295
296 Initializes a Text::Tradition::Stemma object from the given dotfile,
297 and associates it with the tradition.
298
299 =begin testing
300
301 use Text::Tradition;
302
303 my $t = Text::Tradition->new( 
304     'name'  => 'simple test', 
305     'input' => 'Tabular',
306     'file'  => 't/data/simple.txt',
307     );
308
309 my $s;
310 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
311 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
312 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
313 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
314
315 =end testing
316
317 =cut
318
319 sub add_stemma {
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';
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
339 no Moose;
340 __PACKAGE__->meta->make_immutable;
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
353 This package is free software and is provided "as is" without express
354 or implied warranty.  You can redistribute it and/or modify it under
355 the same terms as Perl itself.
356
357 =head1 AUTHOR
358
359 Tara L Andrews E<lt>aurum@cpan.orgE<gt>