1 package Text::Tradition::Collation::Reading;
4 use overload '""' => \&_stringify, 'fallback' => 1;
8 Text::Tradition::Collation::Reading - represents a reading (usually a word)
13 Text::Tradition is a library for representation and analysis of collated
14 texts, particularly medieval ones. A 'reading' refers to a unit of text,
15 usually a word, that appears in one or more witnesses (manuscripts) of the
16 tradition; the text of a given witness is composed of a set of readings in
23 Creates a new reading in the given collation with the given attributes.
28 =item collation - The Text::Tradition::Collation object to which this
29 reading belongs. Required.
31 =item id - A unique identifier for this reading. Required.
33 =item text - The word or other text of the reading.
35 =item is_start - The reading is the starting point for the collation.
37 =item is_end - The reading is the ending point for the collation.
39 =item is_lacuna - The 'reading' represents a known gap in the text.
41 =item is_ph - A temporary placeholder for apparatus parsing purposes. Do
42 not use unless you know what you are doing.
44 =item rank - The sequence number of the reading. This should probably not
49 One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
65 Accessor methods for the given attributes.
71 isa => 'Text::Tradition::Collation',
86 writer => 'alter_text',
128 predicate => 'has_rank',
129 clearer => 'clear_rank',
132 ## For morphological analysis
134 has 'normal_form' => (
137 predicate => 'has_normal_form',
140 # Holds the word form. If is_disambiguated is true, the form at index zero
141 # is the correct one.
142 has 'reading_lexemes' => (
144 isa => 'ArrayRef[Text::Tradition::Collation::Reading::Lexeme]',
146 lexemes => 'elements',
147 has_lexemes => 'count',
148 clear_lexemes => 'clear',
149 add_lexeme => 'push',
151 default => sub { [] },
154 ## For prefix/suffix readings
156 has 'join_prior' => (
169 around BUILDARGS => sub {
179 # If one of our special booleans is set, we change the text and the
181 if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
182 $args->{'text'} = '#LACUNA#';
183 } elsif( exists $args->{'is_start'} ) {
184 $args->{'id'} = '#START#'; # Change the ID to ensure we have only one
185 $args->{'text'} = '#START#';
187 } elsif( exists $args->{'is_end'} ) {
188 $args->{'id'} = '#END#'; # Change the ID to ensure we have only one
189 $args->{'text'} = '#END#';
190 } elsif( exists $args->{'is_ph'} ) {
191 $args->{'text'} = $args->{'id'};
194 $class->$orig( $args );
199 A meta attribute (ha ha), which should be true if any of our 'special'
200 booleans are true. Implies that the reading does not represent a bit
201 of text found in a witness.
207 return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;
210 =head1 Convenience methods
212 =head2 related_readings
214 Calls Collation's related_readings with $self as the first argument.
218 sub related_readings {
220 return $self->collation->related_readings( $self, @_ );
225 Calls Collation's reading_witnesses with $self as the first argument.
231 return $self->collation->reading_witnesses( $self, @_ );
236 Returns a list of Reading objects that immediately precede $self in the collation.
242 my @pred = $self->collation->sequence->predecessors( $self->id );
243 return map { $self->collation->reading( $_ ) } @pred;
248 Returns a list of Reading objects that immediately follow $self in the collation.
254 my @succ = $self->collation->sequence->successors( $self->id );
255 return map { $self->collation->reading( $_ ) } @succ;
258 =head2 set_identical( $other_reading)
260 Backwards compatibility method, to add a transposition relationship
261 between $self and $other_reading. Don't use this.
266 my( $self, $other ) = @_;
267 return $self->collation->add_relationship( $self, $other,
268 { 'type' => 'transposition' } );
278 A few methods to try to tack on morphological information.
287 # my( $self, @lexemes ) = @_;
288 # # The lexemes need to be the same as $self->text.
289 # my $cmpstr = $self->has_normal_form ? lc( $self->normal_form ) : lc( $self->text );
290 # $cmpstr =~ s/[\s-]+//g;
291 # my $lexstr = lc( join( '', @lexemes ) );
292 # $lexstr =~ s/[\s-]+//g;
293 # unless( $lexstr eq $cmpstr ) {
294 # warn "Cannot split " . $self->text . " into " . join( '.', @lexemes );
297 # $self->_clear_morph;
298 # map { $self->_add_morph( { $_ => [] } ) } @lexemes;
301 # sub add_morphological_tag {
302 # my( $self, $lexeme, $opts ) = @_;
305 # # No lexeme was passed; use reading text.
307 # $lexeme = $self->text;
308 # $self->use_lexemes( $lexeme );
310 # # Get the correct container
311 # ( $struct ) = grep { exists $_->{$lexeme} } $self->lexemes;
312 # unless( $struct ) {
313 # warn "No lexeme $lexeme exists in this reading";
316 # # Now make the morph object and add it to this lexeme.
317 # my $morph_obj = Text::Tradition::Collation::Reading::Morphology->new( $opts );
318 # # TODO Check for existence
319 # push( @{$struct->{$lexeme}}, $morph_obj );
329 ## TODO will need a throw() here
332 __PACKAGE__->meta->make_immutable;
334 ###################################################
335 ### Morphology objects, to be attached to readings
336 ###################################################
338 package Text::Tradition::Collation::Reading::Morphology;
360 ## Transmute codes into comparison arrays for our various languages.
362 around BUILDARGS => sub {
366 if( @_ == 1 && ref( $_[0] ) ) {
371 if( exists( $args->{'serial'} ) ) {
372 my( $lemma, $code ) = split( /!!/, delete $args->{'serial'} );
373 $args->{'lemma'} = $lemma;
374 $args->{'code'} = $code;
376 $class->$orig( $args );
381 return join( '!!', $self->lemma, $self->code );
384 sub comparison_array {
386 if( $self->language eq 'French' ) {
388 my @bits = split( /\+/, $self->code );
389 # First push the non k/v parts.
390 while( @bits && $bits[0] !~ /=/ ) {
391 push( @array, shift @bits );
393 while( @array < 2 ) {
394 push( @array, undef );
396 # Now push the k/v parts in a known order.
397 my @fields = qw/ Pers Nb Temps Genre Spec Fonc /;
399 map { my( $k, $v ) = split( /=/, $_ ); $props{$k} = $v; } @bits;
400 foreach my $k ( @fields ) {
401 push( @array, $props{$k} );
405 } elsif( $self->language eq 'English' ) {
406 # Do something as yet undetermined
408 # Latin or Greek or Armenian, just split the chars
409 return split( '', $self->code );
414 __PACKAGE__->meta->make_immutable;