ease validation rules during collation init; fix bug in reading relationship merge
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
CommitLineData
dd3b58b0 1package Text::Tradition;
2
65ed66b9 3use JSON qw / from_json /;
4a8828f0 4use Module::Load;
dd3b58b0 5use Moose;
8e1394aa 6use Text::Tradition::Collation;
56cf65bd 7use Text::Tradition::Stemma;
8e1394aa 8use Text::Tradition::Witness;
dd3b58b0 9
331c2dbf 10use vars qw( $VERSION );
f242e74d 11$VERSION = "0.5";
331c2dbf 12
dd3b58b0 13has 'collation' => (
8e1394aa 14 is => 'ro',
15 isa => 'Text::Tradition::Collation',
16 writer => '_save_collation',
17 );
dd3b58b0 18
3b853983 19has 'witness_hash' => (
20 traits => ['Hash'],
21 isa => 'HashRef[Text::Tradition::Witness]',
8e1394aa 22 handles => {
3b853983 23 witness => 'get',
24 add_witness => 'set',
25 del_witness => 'delete',
26 has_witness => 'exists',
27 witnesses => 'values',
8e1394aa 28 },
3b853983 29 default => sub { {} },
8e1394aa 30 );
c5104dc0 31
df6d9812 32has 'name' => (
33 is => 'rw',
34 isa => 'Str',
35 default => 'Tradition',
36 );
56cf65bd 37
55bc8c78 38has 'language' => (
bbd064a9 39 is => 'rw',
55bc8c78 40 isa => 'Str',
fae52efd 41 predicate => 'has_language',
55bc8c78 42 );
43
e0d617e6 44has 'stemmata' => (
45 traits => ['Array'],
46 isa => 'ArrayRef[Text::Tradition::Stemma]',
47 handles => {
7f52eac8 48 stemmata => 'elements',
e0d617e6 49 _add_stemma => 'push',
50 stemma => 'get',
51 stemma_count => 'count',
52 clear_stemmata => 'clear',
53 },
c3c79612 54 default => sub { [] },
56cf65bd 55 );
3b853983 56
10943ab0 57has 'initialized' => (
58 is => 'ro',
59 isa => 'Bool',
60 default => undef,
61 writer => '_init_done',
62 );
63
3b853983 64# Create the witness before trying to add it
910a0a6d 65around 'add_witness' => sub {
66 my $orig = shift;
67 my $self = shift;
331c2dbf 68 # TODO allow add of a Witness object?
fae52efd 69 my %args = @_ == 1 ? %{$_[0]} : @_;
70 $args{'tradition'} = $self;
71 $args{'language'} = $self->language
72 if( $self->language && !exists $args{'language'} );
73 my $new_wit = Text::Tradition::Witness->new( %args );
3b853983 74 $self->$orig( $new_wit->sigil => $new_wit );
910a0a6d 75 return $new_wit;
76};
331c2dbf 77
3b853983 78# Allow deletion of witness by object as well as by sigil
79around 'del_witness' => sub {
80 my $orig = shift;
81 my $self = shift;
82 my @key_args;
83 foreach my $arg ( @_ ) {
84 push( @key_args,
85 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
86 }
87 return $self->$orig( @key_args );
88};
89
90# Don't allow an empty hash value
91around 'witness' => sub {
92 my( $orig, $self, $arg ) = @_;
93 return unless $self->has_witness( $arg );
94 return $self->$orig( $arg );
95};
96
331c2dbf 97=head1 NAME
98
99Text::Tradition - a software model for a set of collated texts
100
101=head1 SYNOPSIS
102
103 use Text::Tradition;
104 my $t = Text::Tradition->new(
105 'name' => 'this is a text',
106 'input' => 'TEI',
107 'file' => '/path/to/tei_parallel_seg_file.xml' );
108
109 my @text_wits = $t->witnesses();
110 my $manuscript_a = $t->witness( 'A' );
82fa4d57 111
112 $t = Text::Tradition->new();
113 $t->add_witness( 'sourcetype' => 'xmldesc',
114 'file' => '/path/to/teitranscription.xml' );
115 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
116 'string' => 'The quick brown fox jumped over the lazy dogs' );
117 ## TODO
118 $t->collate_texts;
331c2dbf 119
120 my $text_path_svg = $t->collation->as_svg();
121 ## See Text::Tradition::Collation for more on text collation itself
122
123=head1 DESCRIPTION
124
125Text::Tradition is a library for representation and analysis of collated
126texts, particularly medieval ones. A 'tradition' refers to the aggregation
127of surviving versions of a text, generally preserved in multiple
128manuscripts (or 'witnesses'). A Tradition object thus has one more more
129Witnesses, as well as a Collation that represents the unity of all versions
130of the text.
131
132=head1 METHODS
133
134=head2 new
135
136Creates and returns a new text tradition object. The following options are
137accepted.
138
139General options:
140
141=over 4
142
143=item B<name> - The name of the text.
144
145=back
146
147Initialization based on a collation file:
148
149=over 4
150
151=item B<input> - The input format of the collation file. Can be one of the
152following:
153
154=over 4
155
156=item * Self - a GraphML format produced by this module
157
158=item * CollateX - a GraphML format produced by CollateX
159
160=item * CTE - a TEI XML format produced by Classical Text Editor
161
a731e73a 162=item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
163
331c2dbf 164=item * KUL - a specific CSV format for variants, not documented here
165
166=item * TEI - a TEI parallel segmentation format file
167
168=item * Tabular - a comma- or tab-separated collation. Takes an additional
169option, 'sep_char', which defaults to the tab character.
170
171=back
172
173=item B<file> - The name of the file that contains the data. One of 'file'
174or 'string' should be specified.
175
176=item B<string> - A text string that contains the data. One of 'file' or
177'string' should be specified.
178
179=item B<base> - The name of a text file that contains the base text, to be
180used with input formats that require it (currently only KUL).
181
182=back
183
184Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
185
186=over 4
187
188=item B<witnesses> - A reference to an array of Text::Tradition::Witness
189objects that carry the text to be collated.
190
191=item B<collator> - A reference to a collation program that will accept
192Witness objects.
193
194=back
195
196=head2 B<witnesses>
197
198Return the Text::Tradition::Witness objects associated with this tradition,
199as an array.
200
044d1e45 201=head2 B<witness>( $sigil )
202
203Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
204if there is no such object within the tradition.
205
331c2dbf 206=head2 B<add_witness>( %opts )
207
208Instantiate a new witness with the given options (see documentation for
209Text::Tradition::Witness) and add it to the tradition.
210
044d1e45 211=head2 B<del_witness>( $sigil )
212
213Delete the witness with the given sigil from the tradition. Returns the
214witness object for the deleted witness.
215
331c2dbf 216=begin testing
217
218use_ok( 'Text::Tradition', "can use module" );
219
220my $t = Text::Tradition->new( 'name' => 'empty' );
221is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
222is( $t->name, 'empty', "object has the right name" );
223is( scalar $t->witnesses, 0, "object has no witnesses" );
224
225my $simple = 't/data/simple.txt';
226my $s = Text::Tradition->new(
227 'name' => 'inline',
228 'input' => 'Tabular',
229 'file' => $simple,
230 );
231is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
232is( $s->name, 'inline', "object has the right name" );
233is( scalar $s->witnesses, 3, "object has three witnesses" );
234
044d1e45 235my $wit_a = $s->witness('A');
236is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
237if( $wit_a ) {
238 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
239}
240is( $s->witness('X'), undef, "There is no witness X" );
241ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
242
82fa4d57 243my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
044d1e45 244is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
245is( $wit_d->sigil, 'D', "witness has correct sigil" );
331c2dbf 246is( scalar $s->witnesses, 4, "object now has four witnesses" );
247
3b853983 248my $del = $s->del_witness( 'D' );
044d1e45 249is( $del, $wit_d, "Deleted correct witness" );
3b853983 250is( scalar $s->witnesses, 3, "object has three witnesses again" );
251
331c2dbf 252# TODO test initialization by witness list when we have it
253
254=end testing
255
256=cut
910a0a6d 257
df6d9812 258
8e1394aa 259sub BUILD {
260 my( $self, $init_args ) = @_;
fae52efd 261
262 # First, make a collation object. This will use only those arguments in
263 # init_args that apply to the collation.
264 my $collation = Text::Tradition::Collation->new( %$init_args,
265 'tradition' => $self );
266 $self->_save_collation( $collation );
c5104dc0 267
fae52efd 268 if( exists $init_args->{'input'} ) {
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 }
10943ab0 298 $self->_init_done( 1 );
fae52efd 299 return $self;
300}
301
302=head2 add_json_witnesses( $jsonstring, $options )
303
304Adds a set of witnesses from a JSON array specification. This is a wrapper
305to parse the JSON and call add_witness (with the specified $options) for
306each element therein.
307
308=cut
309
310sub add_json_witnesses {
311 my( $self, $jsonstr, $extraopts ) = @_;
65ed66b9 312 my $witarray = from_json( $jsonstr );
313 foreach my $witspec ( @{$witarray->{witnesses}} ) {
fae52efd 314 my $opts = $extraopts || {};
315 $opts->{'sourcetype'} = 'json';
316 $opts->{'object'} = $witspec;
317 $self->add_witness( $opts );
318 }
8e1394aa 319}
c5104dc0 320
56cf65bd 321=head2 add_stemma( $dotfile )
322
323Initializes a Text::Tradition::Stemma object from the given dotfile,
324and associates it with the tradition.
325
326=begin testing
327
328use Text::Tradition;
329
330my $t = Text::Tradition->new(
331 'name' => 'simple test',
332 'input' => 'Tabular',
333 'file' => 't/data/simple.txt',
334 );
335
c3c79612 336is( $t->stemma_count, 0, "No stemmas added yet" );
56cf65bd 337my $s;
9ba651b9 338ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
56cf65bd 339is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
e0d617e6 340is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
341is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
56cf65bd 342
343=end testing
344
345=cut
346
347sub add_stemma {
bffafb73 348 my $self = shift;
349 my %opts = @_;
350 my $stemma_fh;
351 if( $opts{'dotfile'} ) {
352 open $stemma_fh, '<', $opts{'dotfile'}
353 or warn "Could not open file " . $opts{'dotfile'};
354 } elsif( $opts{'dot'} ) {
355 my $str = $opts{'dot'};
356 open $stemma_fh, '<', \$str;
357 }
358 # Assume utf-8
359 binmode $stemma_fh, ':utf8';
56cf65bd 360 my $stemma = Text::Tradition::Stemma->new(
361 'collation' => $self->collation,
362 'dot' => $stemma_fh );
363 $self->_add_stemma( $stemma ) if $stemma;
364 return $stemma;
365}
366
d3e7842a 367sub lemmatize {
368 my $self = shift;
369 unless( $self->has_language ) {
370 warn "Please set a language to lemmatize a tradition";
371 return;
372 }
373 my $mod = "Text::Tradition::Language::" . $self->language;
374 load( $mod );
375 $mod->can( 'lemmatize' )->( $self );
376}
377
dd3b58b0 378no Moose;
379__PACKAGE__->meta->make_immutable;
331c2dbf 380
381
382=head1 BUGS / TODO
383
384=over
385
386=item * Allow tradition to be initialized via passing to a collator.
387
388=back
389
390=head1 LICENSE
391
392This package is free software and is provided "as is" without express
393or implied warranty. You can redistribute it and/or modify it under
394the same terms as Perl itself.
395
396=head1 AUTHOR
397
398Tara L Andrews E<lt>aurum@cpan.orgE<gt>