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