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