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