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