fix bug in finding scoped relationships in some cases
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
CommitLineData
784877d9 1package Text::Tradition::Collation::Reading;
2
8e1394aa 3use Moose;
e4b0f464 4use overload '""' => \&_stringify, 'fallback' => 1;
784877d9 5
3a2ebbf4 6=head1 NAME
784877d9 7
3a2ebbf4 8Text::Tradition::Collation::Reading - represents a reading (usually a word) in a collation.
eca16057 9
3a2ebbf4 10=head1 DESCRIPTION
784877d9 11
3a2ebbf4 12Text::Tradition is a library for representation and analysis of collated
13texts, particularly medieval ones. A 'reading' refers to a unit of text,
14usually a word, that appears in one or more witnesses (manuscripts) of the
15tradition; the text of a given witness is composed of a set of readings in
16a particular sequence
784877d9 17
3a2ebbf4 18=head1 METHODS
1ca1163d 19
3a2ebbf4 20=head2 new
8e1394aa 21
3a2ebbf4 22Creates a new reading in the given collation with the given attributes.
23Options include:
94c00c71 24
3a2ebbf4 25=over 4
784877d9 26
3a2ebbf4 27=item collation - The Text::Tradition::Collation object to which this reading belongs. Required.
e2902068 28
3a2ebbf4 29=item id - A unique identifier for this reading. Required.
910a0a6d 30
3a2ebbf4 31=item text - The word or other text of the reading.
784877d9 32
3a2ebbf4 33=item is_start - The reading is the starting point for the collation.
3265b0ce 34
3a2ebbf4 35=item is_end - The reading is the ending point for the collation.
784877d9 36
3a2ebbf4 37=item is_lacuna - The 'reading' represents a known gap in the text.
de51424a 38
12720144 39=item is_ph - A temporary placeholder for apparatus parsing purposes. Do not use unless you know what you are doing.
40
3a2ebbf4 41=item rank - The sequence number of the reading. This should probably not be set manually.
d047cd52 42
3a2ebbf4 43=back
8e1394aa 44
3a2ebbf4 45One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
8e1394aa 46
3a2ebbf4 47=head2 collation
94c00c71 48
3a2ebbf4 49=head2 id
94c00c71 50
3a2ebbf4 51=head2 text
4cdd82f1 52
3a2ebbf4 53=head2 is_start
4cdd82f1 54
3a2ebbf4 55=head2 is_end
4a8828f0 56
3a2ebbf4 57=head2 is_lacuna
4a8828f0 58
3a2ebbf4 59=head2 rank
4a8828f0 60
3a2ebbf4 61Accessor methods for the given attributes.
d047cd52 62
3a2ebbf4 63=cut
d047cd52 64
3a2ebbf4 65has 'collation' => (
66 is => 'ro',
67 isa => 'Text::Tradition::Collation',
68 # required => 1,
69 weak_ref => 1,
70 );
d047cd52 71
3a2ebbf4 72has 'id' => (
73 is => 'ro',
74 isa => 'Str',
75 required => 1,
76 );
d047cd52 77
3a2ebbf4 78has 'text' => (
79 is => 'ro',
80 isa => 'Str',
81 required => 1,
49d4f2ac 82 writer => 'alter_text',
3a2ebbf4 83 );
0e47f4f6 84
3a2ebbf4 85has 'is_start' => (
86 is => 'ro',
87 isa => 'Bool',
88 default => undef,
89 );
90
91has 'is_end' => (
92 is => 'ro',
93 isa => 'Bool',
94 default => undef,
95 );
96
97has 'is_lacuna' => (
98 is => 'ro',
99 isa => 'Bool',
100 default => undef,
101 );
12720144 102
103has 'is_ph' => (
104 is => 'ro',
105 isa => 'Bool',
106 default => undef,
107 );
d4b75f44 108
109has 'is_common' => (
110 is => 'rw',
111 isa => 'Bool',
112 default => undef,
113 );
3a2ebbf4 114
115has 'rank' => (
116 is => 'rw',
117 isa => 'Int',
118 predicate => 'has_rank',
ca6e6095 119 clearer => 'clear_rank',
3a2ebbf4 120 );
fd602649 121
122## For morphological analysis
123
124has 'normal_form' => (
125 is => 'rw',
126 isa => 'Str',
127 predicate => 'has_normal_form',
128 );
129
130has 'lemma' => (
131 is => 'rw',
132 isa => 'Str',
133 predicate => 'has_lemma',
134 );
135
136has 'morphology' => (
137 is => 'rw',
138 isa => 'Str',
139 predicate => 'has_morphology',
140 );
141
142has 'morph_possibilities' => (
143 is => 'ro',
144 isa => 'HashRef[Str]',
145 default => sub { {} },
146 );
3a2ebbf4 147
629e27b0 148## For prefix/suffix readings
149
150has 'join_prior' => (
151 is => 'ro',
152 isa => 'Bool',
153 default => undef,
154 );
155
156has 'join_next' => (
157 is => 'ro',
158 isa => 'Bool',
159 default => undef,
160 );
161
3a2ebbf4 162
163around BUILDARGS => sub {
164 my $orig = shift;
165 my $class = shift;
166 my $args;
167 if( @_ == 1 ) {
168 $args = shift;
169 } else {
170 $args = { @_ };
171 }
b0b4421a 172
3a2ebbf4 173 # If one of our special booleans is set, we change the text and the
174 # ID to match.
1d310495 175 if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
56eefa04 176 $args->{'text'} = '#LACUNA#';
3a2ebbf4 177 } elsif( exists $args->{'is_start'} ) {
178 $args->{'id'} = '#START#'; # Change the ID to ensure we have only one
179 $args->{'text'} = '#START#';
180 $args->{'rank'} = 0;
181 } elsif( exists $args->{'is_end'} ) {
182 $args->{'id'} = '#END#'; # Change the ID to ensure we have only one
183 $args->{'text'} = '#END#';
12720144 184 } elsif( exists $args->{'is_ph'} ) {
185 $args->{'text'} = $args->{'id'};
3a2ebbf4 186 }
187
188 $class->$orig( $args );
189};
190
191=head2 is_meta
192
193A meta attribute (ha ha), which should be true if any of our 'special'
194booleans are true. Implies that the reading does not represent a bit
195of text found in a witness.
196
197=cut
198
199sub is_meta {
200 my $self = shift;
12720144 201 return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;
3a2ebbf4 202}
203
027d819c 204=head1 Convenience methods
205
206=head2 related_readings
207
208Calls Collation's related_readings with $self as the first argument.
209
210=cut
211
3a2ebbf4 212sub related_readings {
213 my $self = shift;
214 return $self->collation->related_readings( $self, @_ );
215}
216
7f52eac8 217=head2 witnesses
218
219Calls Collation's reading_witnesses with $self as the first argument.
220
221=cut
222
223sub witnesses {
224 my $self = shift;
225 return $self->collation->reading_witnesses( $self, @_ );
226}
227
027d819c 228=head2 predecessors
229
230Returns a list of Reading objects that immediately precede $self in the collation.
231
232=cut
233
22222af9 234sub predecessors {
235 my $self = shift;
236 my @pred = $self->collation->sequence->predecessors( $self->id );
237 return map { $self->collation->reading( $_ ) } @pred;
238}
239
027d819c 240=head2 successors
241
242Returns a list of Reading objects that immediately follow $self in the collation.
243
244=cut
245
22222af9 246sub successors {
247 my $self = shift;
248 my @succ = $self->collation->sequence->successors( $self->id );
249 return map { $self->collation->reading( $_ ) } @succ;
250}
251
027d819c 252=head2 set_identical( $other_reading)
253
254Backwards compatibility method, to add a transposition relationship
255between $self and $other_reading. Don't use this.
256
257=cut
258
1d310495 259sub set_identical {
260 my( $self, $other ) = @_;
261 return $self->collation->add_relationship( $self, $other,
262 { 'type' => 'transposition' } );
263}
264
3a2ebbf4 265sub _stringify {
266 my $self = shift;
267 return $self->id;
268}
d047cd52 269
2acf0892 270sub TO_JSON {
271 my $self = shift;
272 return $self->text;
273}
274
021bdbac 275no Moose;
276__PACKAGE__->meta->make_immutable;
d047cd52 277
021bdbac 2781;
d047cd52 279