make normal_form default to ->text
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
CommitLineData
784877d9 1package Text::Tradition::Collation::Reading;
2
8e1394aa 3use Moose;
10e4b1ac 4use Moose::Util::TypeConstraints;
7604424b 5use JSON qw/ from_json /;
6ad2ce78 6use Module::Load;
70745e70 7use Text::Tradition::Error;
10e4b1ac 8use XML::Easy::Syntax qw( $xml10_name_rx $xml10_namestartchar_rx );
7cd9f181 9use YAML::XS;
e4b0f464 10use overload '""' => \&_stringify, 'fallback' => 1;
784877d9 11
10e4b1ac 12subtype 'ReadingID',
13 as 'Str',
14 where { $_ =~ /\A$xml10_name_rx\z/ },
15 message { 'Reading ID must be a valid XML attribute string' };
16
17no Moose::Util::TypeConstraints;
18
3a2ebbf4 19=head1 NAME
784877d9 20
4aea6e9b 21Text::Tradition::Collation::Reading - represents a reading (usually a word)
22in a collation.
23
3a2ebbf4 24=head1 DESCRIPTION
784877d9 25
3a2ebbf4 26Text::Tradition is a library for representation and analysis of collated
27texts, particularly medieval ones. A 'reading' refers to a unit of text,
28usually a word, that appears in one or more witnesses (manuscripts) of the
29tradition; the text of a given witness is composed of a set of readings in
30a particular sequence
784877d9 31
3a2ebbf4 32=head1 METHODS
1ca1163d 33
3a2ebbf4 34=head2 new
8e1394aa 35
4aea6e9b 36Creates a new reading in the given collation with the given attributes.
3a2ebbf4 37Options include:
94c00c71 38
3a2ebbf4 39=over 4
784877d9 40
4aea6e9b 41=item collation - The Text::Tradition::Collation object to which this
42reading belongs. Required.
e2902068 43
3a2ebbf4 44=item id - A unique identifier for this reading. Required.
910a0a6d 45
3a2ebbf4 46=item text - The word or other text of the reading.
784877d9 47
3a2ebbf4 48=item is_start - The reading is the starting point for the collation.
3265b0ce 49
3a2ebbf4 50=item is_end - The reading is the ending point for the collation.
784877d9 51
3a2ebbf4 52=item is_lacuna - The 'reading' represents a known gap in the text.
de51424a 53
4aea6e9b 54=item is_ph - A temporary placeholder for apparatus parsing purposes. Do
55not use unless you know what you are doing.
12720144 56
4aea6e9b 57=item rank - The sequence number of the reading. This should probably not
58be set manually.
d047cd52 59
3a2ebbf4 60=back
8e1394aa 61
3a2ebbf4 62One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
8e1394aa 63
3a2ebbf4 64=head2 collation
94c00c71 65
3a2ebbf4 66=head2 id
94c00c71 67
3a2ebbf4 68=head2 text
4cdd82f1 69
3a2ebbf4 70=head2 is_start
4cdd82f1 71
3a2ebbf4 72=head2 is_end
4a8828f0 73
3a2ebbf4 74=head2 is_lacuna
4a8828f0 75
3a2ebbf4 76=head2 rank
4a8828f0 77
3a2ebbf4 78Accessor methods for the given attributes.
d047cd52 79
3a2ebbf4 80=cut
d047cd52 81
3a2ebbf4 82has 'collation' => (
83 is => 'ro',
84 isa => 'Text::Tradition::Collation',
85 # required => 1,
86 weak_ref => 1,
87 );
d047cd52 88
3a2ebbf4 89has 'id' => (
90 is => 'ro',
10e4b1ac 91 isa => 'ReadingID',
3a2ebbf4 92 required => 1,
93 );
d047cd52 94
3a2ebbf4 95has 'text' => (
96 is => 'ro',
97 isa => 'Str',
98 required => 1,
49d4f2ac 99 writer => 'alter_text',
3a2ebbf4 100 );
0e47f4f6 101
fae52efd 102has 'language' => (
103 is => 'ro',
104 isa => 'Str',
6ad2ce78 105 predicate => 'has_language',
fae52efd 106 );
107
3a2ebbf4 108has 'is_start' => (
109 is => 'ro',
110 isa => 'Bool',
111 default => undef,
112 );
113
114has 'is_end' => (
115 is => 'ro',
116 isa => 'Bool',
117 default => undef,
118 );
119
120has 'is_lacuna' => (
121 is => 'ro',
122 isa => 'Bool',
123 default => undef,
124 );
12720144 125
126has 'is_ph' => (
127 is => 'ro',
128 isa => 'Bool',
129 default => undef,
130 );
d4b75f44 131
132has 'is_common' => (
133 is => 'rw',
134 isa => 'Bool',
135 default => undef,
136 );
3a2ebbf4 137
138has 'rank' => (
139 is => 'rw',
140 isa => 'Int',
141 predicate => 'has_rank',
ca6e6095 142 clearer => 'clear_rank',
3a2ebbf4 143 );
fd602649 144
145## For morphological analysis
146
a8928d1d 147has 'grammar_invalid' => (
148 is => 'rw',
149 isa => 'Bool',
150 default => undef,
151 );
152
153has 'is_nonsense' => (
154 is => 'rw',
155 isa => 'Bool',
0e6e9e7a 156 default => undef,
a8928d1d 157 );
158
fd602649 159has 'normal_form' => (
160 is => 'rw',
161 isa => 'Str',
367e901b 162 predicate => '_has_normal_form',
163 clearer => '_clear_normal_form',
fd602649 164 );
165
7cd9f181 166# Holds the lexemes for the reading.
d3e7842a 167has 'reading_lexemes' => (
4d9593df 168 traits => ['Array'],
d3e7842a 169 isa => 'ArrayRef[Text::Tradition::Collation::Reading::Lexeme]',
4d9593df 170 handles => {
da83693e 171 lexeme => 'get',
4d9593df 172 lexemes => 'elements',
cca4f996 173 has_lexemes => 'count',
d3e7842a 174 clear_lexemes => 'clear',
175 add_lexeme => 'push',
4d9593df 176 },
d3e7842a 177 default => sub { [] },
fd602649 178 );
179
629e27b0 180## For prefix/suffix readings
181
182has 'join_prior' => (
183 is => 'ro',
184 isa => 'Bool',
185 default => undef,
186 );
187
188has 'join_next' => (
189 is => 'ro',
190 isa => 'Bool',
191 default => undef,
192 );
193
3a2ebbf4 194
195around BUILDARGS => sub {
196 my $orig = shift;
197 my $class = shift;
198 my $args;
199 if( @_ == 1 ) {
200 $args = shift;
201 } else {
202 $args = { @_ };
203 }
b0b4421a 204
3a2ebbf4 205 # If one of our special booleans is set, we change the text and the
206 # ID to match.
1d310495 207 if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
56eefa04 208 $args->{'text'} = '#LACUNA#';
3a2ebbf4 209 } elsif( exists $args->{'is_start'} ) {
10e4b1ac 210 $args->{'id'} = '__START__'; # Change the ID to ensure we have only one
3a2ebbf4 211 $args->{'text'} = '#START#';
212 $args->{'rank'} = 0;
213 } elsif( exists $args->{'is_end'} ) {
10e4b1ac 214 $args->{'id'} = '__END__'; # Change the ID to ensure we have only one
3a2ebbf4 215 $args->{'text'} = '#END#';
12720144 216 } elsif( exists $args->{'is_ph'} ) {
217 $args->{'text'} = $args->{'id'};
3a2ebbf4 218 }
219
10e4b1ac 220 # Backwards compatibility for non-XMLname IDs
221 my $rid = $args->{'id'};
222 $rid =~ s/\#/__/g;
223 $rid =~ s/[\/,]/./g;
224 if( $rid !~ /^$xml10_namestartchar_rx/ ) {
225 $rid = 'r'.$rid;
226 }
227 $args->{'id'} = $rid;
228
3a2ebbf4 229 $class->$orig( $args );
230};
231
70745e70 232# Look for a lexeme-string argument in the build args.
233sub BUILD {
234 my( $self, $args ) = @_;
235 if( exists $args->{'lexemes'} ) {
236 $self->_deserialize_lexemes( $args->{'lexemes'} );
237 }
238}
239
367e901b 240# Make normal_form default to text, transparently.
241around 'normal_form' => sub {
242 my $orig = shift;
243 my $self = shift;
244 my( $arg ) = @_;
245 if( $arg && $arg eq $self->text ) {
246 $self->_clear_normal_form;
247 return $arg;
248 } elsif( !$arg && !$self->_has_normal_form ) {
249 return $self->text;
250 } else {
251 $self->$orig( @_ );
252 }
253};
254
3a2ebbf4 255=head2 is_meta
256
257A meta attribute (ha ha), which should be true if any of our 'special'
258booleans are true. Implies that the reading does not represent a bit
259of text found in a witness.
260
261=cut
262
263sub is_meta {
264 my $self = shift;
12720144 265 return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;
3a2ebbf4 266}
267
56772e8c 268=head2 is_nonrel
269
270Similar to is_meta, but returns false for the start and end readings.
271
272=cut
273
274sub is_nonrel {
275 my $self = shift;
276 return $self->is_lacuna || $self->is_ph;
277}
278
027d819c 279=head1 Convenience methods
280
281=head2 related_readings
282
283Calls Collation's related_readings with $self as the first argument.
284
285=cut
286
3a2ebbf4 287sub related_readings {
288 my $self = shift;
289 return $self->collation->related_readings( $self, @_ );
290}
291
7f52eac8 292=head2 witnesses
293
294Calls Collation's reading_witnesses with $self as the first argument.
295
296=cut
297
298sub witnesses {
299 my $self = shift;
300 return $self->collation->reading_witnesses( $self, @_ );
301}
302
027d819c 303=head2 predecessors
304
305Returns a list of Reading objects that immediately precede $self in the collation.
306
307=cut
308
22222af9 309sub predecessors {
310 my $self = shift;
311 my @pred = $self->collation->sequence->predecessors( $self->id );
312 return map { $self->collation->reading( $_ ) } @pred;
313}
314
027d819c 315=head2 successors
316
317Returns a list of Reading objects that immediately follow $self in the collation.
318
319=cut
320
22222af9 321sub successors {
322 my $self = shift;
323 my @succ = $self->collation->sequence->successors( $self->id );
324 return map { $self->collation->reading( $_ ) } @succ;
325}
326
027d819c 327=head2 set_identical( $other_reading)
328
329Backwards compatibility method, to add a transposition relationship
330between $self and $other_reading. Don't use this.
331
332=cut
333
1d310495 334sub set_identical {
335 my( $self, $other ) = @_;
336 return $self->collation->add_relationship( $self, $other,
337 { 'type' => 'transposition' } );
338}
339
3a2ebbf4 340sub _stringify {
341 my $self = shift;
342 return $self->id;
343}
d047cd52 344
4d9593df 345=head1 MORPHOLOGY
346
7cd9f181 347Methods for the morphological information (if any) attached to readings.
348A reading may be made up of multiple lexemes; the concatenated lexeme
349strings ought to match the reading's normalized form.
350
351See L<Text::Tradition::Collation::Reading::Lexeme> for more information
352on Lexeme objects and their attributes.
353
354=head2 has_lexemes
355
356Returns a true value if the reading has any attached lexemes.
4d9593df 357
6ad2ce78 358=head2 lexemes
06e7cbc7 359
7cd9f181 360Returns the Lexeme objects (if any) attached to the reading.
6ad2ce78 361
362=head2 clear_lexemes
363
7cd9f181 364Wipes any associated Lexeme objects out of the reading.
365
366=head2 add_lexeme( $lexobj )
6ad2ce78 367
7cd9f181 368Adds the Lexeme in $lexobj to the list of lexemes.
369
370=head2 lemmatize
371
372If the language of the reading is set, this method will use the appropriate
373Language model to determine the lexemes that belong to this reading. See
374L<Text::Tradition::lemmatize> if you wish to lemmatize an entire tradition.
06e7cbc7 375
4d9593df 376=cut
377
6ad2ce78 378sub lemmatize {
379 my $self = shift;
380 unless( $self->has_language ) {
381 warn "Please set a language to lemmatize a tradition";
382 return;
383 }
384 my $mod = "Text::Tradition::Language::" . $self->language;
385 load( $mod );
386 $mod->can( 'reading_lookup' )->( $self );
387
388}
4d9593df 389
7604424b 390# For graph serialization. Return a JSON representation of the associated
7cd9f181 391# reading lexemes.
392sub _serialize_lexemes {
393 my $self = shift;
7604424b 394 my $json = JSON->new->allow_blessed(1)->convert_blessed(1);
395 return $json->encode( [ $self->lexemes ] );
7cd9f181 396}
70745e70 397
7604424b 398# Given a JSON representation of the lexemes, instantiate them and add
399# them to the reading.
70745e70 400sub _deserialize_lexemes {
7604424b 401 my( $self, $json ) = @_;
402 my $data = from_json( $json );
403 return unless @$data;
70745e70 404
7604424b 405 # Need to have the lexeme module in order to have lexemes.
406 eval { use Text::Tradition::Collation::Reading::Lexeme; };
70745e70 407 throw( $@ ) if $@;
408
409 # Good to go - add the lexemes.
410 my @lexemes;
7604424b 411 foreach my $lexhash ( @$data ) {
412 push( @lexemes, Text::Tradition::Collation::Reading::Lexeme->new(
413 'JSON' => $lexhash ) );
70745e70 414 }
415 $self->clear_lexemes;
416 $self->add_lexeme( @lexemes );
417}
7cd9f181 418
4d9593df 419## Utility methods
420
2acf0892 421sub TO_JSON {
422 my $self = shift;
423 return $self->text;
424}
425
70745e70 426sub throw {
427 Text::Tradition::Error->throw(
428 'ident' => 'Reading error',
429 'message' => $_[0],
430 );
431}
4d9593df 432
433no Moose;
434__PACKAGE__->meta->make_immutable;
435
021bdbac 4361;