Merge UserStore and Directory as we had fun assigning users to traditions otherwise.
[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',
63 );
3b853983 64
65# Create the witness before trying to add it
910a0a6d 66around 'add_witness' => sub {
67 my $orig = shift;
68 my $self = shift;
331c2dbf 69 # TODO allow add of a Witness object?
fae52efd 70 my %args = @_ == 1 ? %{$_[0]} : @_;
71 $args{'tradition'} = $self;
72 $args{'language'} = $self->language
73 if( $self->language && !exists $args{'language'} );
74 my $new_wit = Text::Tradition::Witness->new( %args );
3b853983 75 $self->$orig( $new_wit->sigil => $new_wit );
910a0a6d 76 return $new_wit;
77};
331c2dbf 78
3b853983 79# Allow deletion of witness by object as well as by sigil
80around 'del_witness' => sub {
81 my $orig = shift;
82 my $self = shift;
83 my @key_args;
84 foreach my $arg ( @_ ) {
85 push( @key_args,
86 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
87 }
88 return $self->$orig( @key_args );
89};
90
91# Don't allow an empty hash value
92around 'witness' => sub {
93 my( $orig, $self, $arg ) = @_;
94 return unless $self->has_witness( $arg );
95 return $self->$orig( $arg );
96};
97
331c2dbf 98=head1 NAME
99
100Text::Tradition - a software model for a set of collated texts
101
102=head1 SYNOPSIS
103
104 use Text::Tradition;
105 my $t = Text::Tradition->new(
106 'name' => 'this is a text',
107 'input' => 'TEI',
108 'file' => '/path/to/tei_parallel_seg_file.xml' );
109
110 my @text_wits = $t->witnesses();
111 my $manuscript_a = $t->witness( 'A' );
82fa4d57 112
113 $t = Text::Tradition->new();
114 $t->add_witness( 'sourcetype' => 'xmldesc',
115 'file' => '/path/to/teitranscription.xml' );
116 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
117 'string' => 'The quick brown fox jumped over the lazy dogs' );
118 ## TODO
119 $t->collate_texts;
331c2dbf 120
121 my $text_path_svg = $t->collation->as_svg();
122 ## See Text::Tradition::Collation for more on text collation itself
123
124=head1 DESCRIPTION
125
126Text::Tradition is a library for representation and analysis of collated
127texts, particularly medieval ones. A 'tradition' refers to the aggregation
128of surviving versions of a text, generally preserved in multiple
129manuscripts (or 'witnesses'). A Tradition object thus has one more more
130Witnesses, as well as a Collation that represents the unity of all versions
131of the text.
132
133=head1 METHODS
134
135=head2 new
136
137Creates and returns a new text tradition object. The following options are
138accepted.
139
140General options:
141
142=over 4
143
144=item B<name> - The name of the text.
145
146=back
147
148Initialization based on a collation file:
149
150=over 4
151
152=item B<input> - The input format of the collation file. Can be one of the
153following:
154
155=over 4
156
157=item * Self - a GraphML format produced by this module
158
159=item * CollateX - a GraphML format produced by CollateX
160
161=item * CTE - a TEI XML format produced by Classical Text Editor
162
a731e73a 163=item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
164
331c2dbf 165=item * KUL - a specific CSV format for variants, not documented here
166
167=item * TEI - a TEI parallel segmentation format file
168
169=item * Tabular - a comma- or tab-separated collation. Takes an additional
170option, 'sep_char', which defaults to the tab character.
171
172=back
173
174=item B<file> - The name of the file that contains the data. One of 'file'
175or 'string' should be specified.
176
177=item B<string> - A text string that contains the data. One of 'file' or
178'string' should be specified.
179
180=item B<base> - The name of a text file that contains the base text, to be
181used with input formats that require it (currently only KUL).
182
183=back
184
185Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
186
187=over 4
188
189=item B<witnesses> - A reference to an array of Text::Tradition::Witness
190objects that carry the text to be collated.
191
192=item B<collator> - A reference to a collation program that will accept
193Witness objects.
194
195=back
196
197=head2 B<witnesses>
198
199Return the Text::Tradition::Witness objects associated with this tradition,
200as an array.
201
044d1e45 202=head2 B<witness>( $sigil )
203
204Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
205if there is no such object within the tradition.
206
331c2dbf 207=head2 B<add_witness>( %opts )
208
209Instantiate a new witness with the given options (see documentation for
210Text::Tradition::Witness) and add it to the tradition.
211
044d1e45 212=head2 B<del_witness>( $sigil )
213
214Delete the witness with the given sigil from the tradition. Returns the
215witness object for the deleted witness.
216
331c2dbf 217=begin testing
218
219use_ok( 'Text::Tradition', "can use module" );
220
221my $t = Text::Tradition->new( 'name' => 'empty' );
222is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
223is( $t->name, 'empty', "object has the right name" );
224is( scalar $t->witnesses, 0, "object has no witnesses" );
225
226my $simple = 't/data/simple.txt';
227my $s = Text::Tradition->new(
228 'name' => 'inline',
229 'input' => 'Tabular',
230 'file' => $simple,
231 );
232is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
233is( $s->name, 'inline', "object has the right name" );
234is( scalar $s->witnesses, 3, "object has three witnesses" );
235
044d1e45 236my $wit_a = $s->witness('A');
237is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
238if( $wit_a ) {
239 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
240}
241is( $s->witness('X'), undef, "There is no witness X" );
242ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
243
82fa4d57 244my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
044d1e45 245is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
246is( $wit_d->sigil, 'D', "witness has correct sigil" );
331c2dbf 247is( scalar $s->witnesses, 4, "object now has four witnesses" );
248
3b853983 249my $del = $s->del_witness( 'D' );
044d1e45 250is( $del, $wit_d, "Deleted correct witness" );
3b853983 251is( scalar $s->witnesses, 3, "object has three witnesses again" );
252
331c2dbf 253# TODO test initialization by witness list when we have it
254
255=end testing
256
257=cut
910a0a6d 258
df6d9812 259
8e1394aa 260sub BUILD {
261 my( $self, $init_args ) = @_;
fae52efd 262
263 # First, make a collation object. This will use only those arguments in
264 # init_args that apply to the collation.
265 my $collation = Text::Tradition::Collation->new( %$init_args,
266 'tradition' => $self );
267 $self->_save_collation( $collation );
c5104dc0 268
fae52efd 269 if( exists $init_args->{'input'} ) {
910a0a6d 270 # Call the appropriate parser on the given data
a731e73a 271 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
dfc37e38 272 my @format_basetext = qw/ KUL /;
273 my $use_base;
274 my $format = $init_args->{'input'};
dfc37e38 275 if( $format && !( grep { $_ eq $format } @format_standalone )
276 && !( grep { $_ eq $format } @format_basetext ) ) {
277 warn "Unrecognized input format $format; not parsing";
910a0a6d 278 return;
279 }
dfc37e38 280 if( $format && grep { $_ eq $format } @format_basetext ) {
281 $use_base = 1;
282 if( !exists $init_args->{'base'} ) {
283 warn "Cannot make a collation from $format without a base text";
284 return;
285 }
286 }
4a8828f0 287
910a0a6d 288 # Now do the parsing.
910a0a6d 289 if( $format ) {
dfc37e38 290 if( $use_base ) {
291 $format = 'BaseText'; # Use the BaseText module for parsing,
292 # but retain the original input arg.
910a0a6d 293 }
294 my $mod = "Text::Tradition::Parser::$format";
295 load( $mod );
dfc37e38 296 $mod->can('parse')->( $self, $init_args );
910a0a6d 297 }
c5104dc0 298 }
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>