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