1 package Text::Tradition::Collation::Reading;
4 use overload '""' => \&_stringify, 'fallback' => 1;
8 Text::Tradition::Collation::Reading - represents a reading (usually a word) in a collation.
12 Text::Tradition is a library for representation and analysis of collated
13 texts, particularly medieval ones. A 'reading' refers to a unit of text,
14 usually a word, that appears in one or more witnesses (manuscripts) of the
15 tradition; the text of a given witness is composed of a set of readings in
22 Creates a new reading in the given collation with the given attributes.
27 =item collation - The Text::Tradition::Collation object to which this reading belongs. Required.
29 =item id - A unique identifier for this reading. Required.
31 =item text - The word or other text of the reading.
33 =item is_start - The reading is the starting point for the collation.
35 =item is_end - The reading is the ending point for the collation.
37 =item is_lacuna - The 'reading' represents a known gap in the text.
39 =item is_ph - A temporary placeholder for apparatus parsing purposes. Do not use unless you know what you are doing.
41 =item rank - The sequence number of the reading. This should probably not be set manually.
45 One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
61 Accessor methods for the given attributes.
67 isa => 'Text::Tradition::Collation',
82 writer => 'alter_text',
85 has 'punctuation' => (
87 isa => 'ArrayRef[HashRef[Str]]',
88 default => sub { [] },
90 punctuation => 'elements',
91 add_punctuation => 'push',
95 has 'separate_punctuation' => (
128 predicate => 'has_rank',
132 around BUILDARGS => sub {
142 # Did we get a JSON token to parse into a reading? If so, massage it.
143 if( exists $args->{'json'} ) {
144 my $j = delete $args->{'json'};
146 # If we have separated punctuation and don't want it, restore it.
147 if( exists $j->{'punctuation'}
148 && exists $args->{'separate_punctuation'}
149 && !$args->{'separate_punctuation'} ) {
150 $args->{'text'} = _restore_punct( $j->{'t'}, $j->{'punctuation'} );
152 # In all other cases, keep text and punct as they are.
154 $args->{'text'} = $j->{'t'};
155 # we don't use comparison or canonical forms here
156 $args->{'punctuation'} = $j->{'punctuation'}
157 if exists $j->{'punctuation'};
161 # If one of our special booleans is set, we change the text and the
163 if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
164 $args->{'text'} = '#LACUNA#';
165 } elsif( exists $args->{'is_start'} ) {
166 $args->{'id'} = '#START#'; # Change the ID to ensure we have only one
167 $args->{'text'} = '#START#';
169 } elsif( exists $args->{'is_end'} ) {
170 $args->{'id'} = '#END#'; # Change the ID to ensure we have only one
171 $args->{'text'} = '#END#';
172 } elsif( exists $args->{'is_ph'} ) {
173 $args->{'text'} = $args->{'id'};
176 $class->$orig( $args );
179 # Post-process the given text, stripping punctuation if we are asked.
182 if( $self->separate_punctuation && !$self->is_meta
183 && !$self->punctuation ) {
185 my $wspunct = ''; # word sans punctuation
186 foreach my $char ( split( //, $self->text ) ) {
187 if( $char =~ /^[[:punct:]]$/ ) {
188 $self->add_punctuation( { 'char' => $char, 'pos' => $pos } );
194 $self->alter_text( $wspunct );
198 sub punctuated_form {
200 return _restore_punct( $self->text, $self->punctuation );
204 my( $word, @punct ) = @_;
205 foreach my $p ( sort { $a->{pos} <=> $b->{pos} } @punct ) {
206 substr( $word, $p->{pos}, 0, $p->{char} );
213 A meta attribute (ha ha), which should be true if any of our 'special'
214 booleans are true. Implies that the reading does not represent a bit
215 of text found in a witness.
221 return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;
224 # Some syntactic sugar
225 sub related_readings {
227 return $self->collation->related_readings( $self, @_ );
232 my @pred = $self->collation->sequence->predecessors( $self->id );
233 return map { $self->collation->reading( $_ ) } @pred;
238 my @succ = $self->collation->sequence->successors( $self->id );
239 return map { $self->collation->reading( $_ ) } @succ;
243 my( $self, $other ) = @_;
244 return $self->collation->add_relationship( $self, $other,
245 { 'type' => 'transposition' } );
254 __PACKAGE__->meta->make_immutable;