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