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