47bd0f708af8d25f8da759b7a52fc9d0c4b7733a
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
1 package Text::Tradition::Collation::Reading;
2
3 use Moose;
4 use overload '""' => \&_stringify, 'fallback' => 1;
5
6 =head1 NAME
7
8 Text::Tradition::Collation::Reading - represents a reading (usually a word)
9 in a collation.
10
11 =head1 DESCRIPTION
12
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
17 a particular sequence
18
19 =head1 METHODS
20
21 =head2 new
22
23 Creates a new reading in the given collation with the given attributes.
24 Options include:
25
26 =over 4
27
28 =item collation - The Text::Tradition::Collation object to which this
29 reading belongs.  Required.
30
31 =item id - A unique identifier for this reading. Required.
32
33 =item text - The word or other text of the reading.
34
35 =item is_start - The reading is the starting point for the collation.
36
37 =item is_end - The reading is the ending point for the collation.
38
39 =item is_lacuna - The 'reading' represents a known gap in the text.
40
41 =item is_ph - A temporary placeholder for apparatus parsing purposes.  Do
42 not use unless you know what you are doing.
43
44 =item rank - The sequence number of the reading. This should probably not
45 be set manually.
46
47 =back
48
49 One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
50
51 =head2 collation
52
53 =head2 id
54
55 =head2 text
56
57 =head2 is_start
58
59 =head2 is_end
60
61 =head2 is_lacuna
62
63 =head2 rank
64
65 Accessor methods for the given attributes.
66
67 =cut
68
69 has 'collation' => (
70         is => 'ro',
71         isa => 'Text::Tradition::Collation',
72         # required => 1,
73         weak_ref => 1,
74         );
75
76 has 'id' => (
77         is => 'ro',
78         isa => 'Str',
79         required => 1,
80         );
81
82 has 'text' => (
83         is => 'ro',
84         isa => 'Str',
85         required => 1,
86         writer => 'alter_text',
87         );
88         
89 has 'language' => (
90         is => 'ro',
91         isa => 'Str',
92         default => 'Default',
93         );
94         
95 has 'is_start' => (
96         is => 'ro',
97         isa => 'Bool',
98         default => undef,
99         );
100
101 has 'is_end' => (
102         is => 'ro',
103         isa => 'Bool',
104         default => undef,
105         );
106     
107 has 'is_lacuna' => (
108     is => 'ro',
109     isa => 'Bool',
110         default => undef,
111     );
112     
113 has 'is_ph' => (
114         is => 'ro',
115         isa => 'Bool',
116         default => undef,
117         );
118         
119 has 'is_common' => (
120         is => 'rw',
121         isa => 'Bool',
122         default => undef,
123         );
124
125 has 'rank' => (
126     is => 'rw',
127     isa => 'Int',
128     predicate => 'has_rank',
129     clearer => 'clear_rank',
130     );
131     
132 ## For morphological analysis
133
134 has 'normal_form' => (
135         is => 'rw',
136         isa => 'Str',
137         predicate => 'has_normal_form',
138         );
139
140 # Holds the word form. If is_disambiguated is true, the form at index zero
141 # is the correct one.
142 has 'lexemes' => (
143         traits => ['Array'],
144         isa => 'ArrayRef[Text::Tradition::Collation::Lexeme]',
145         handles => {
146                 lexemes => 'elements',
147                 has_lexemes => 'count',
148                 _clear_lexemes => 'clear',
149                 _add_lexeme => 'push',
150                 },
151         );
152         
153 ## For prefix/suffix readings
154
155 has 'join_prior' => (
156         is => 'ro',
157         isa => 'Bool',
158         default => undef,
159         );
160         
161 has 'join_next' => (
162         is => 'ro',
163         isa => 'Bool',
164         default => undef,
165         );
166
167
168 around BUILDARGS => sub {
169         my $orig = shift;
170         my $class = shift;
171         my $args;
172         if( @_ == 1 ) {
173                 $args = shift;
174         } else {
175                 $args = { @_ };
176         }
177                         
178         # If one of our special booleans is set, we change the text and the
179         # ID to match.
180         if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
181                 $args->{'text'} = '#LACUNA#';
182         } elsif( exists $args->{'is_start'} ) {
183                 $args->{'id'} = '#START#';  # Change the ID to ensure we have only one
184                 $args->{'text'} = '#START#';
185                 $args->{'rank'} = 0;
186         } elsif( exists $args->{'is_end'} ) {
187                 $args->{'id'} = '#END#';        # Change the ID to ensure we have only one
188                 $args->{'text'} = '#END#';
189         } elsif( exists $args->{'is_ph'} ) {
190                 $args->{'text'} = $args->{'id'};
191         }
192         
193         $class->$orig( $args );
194 };
195
196 =head2 is_meta
197
198 A meta attribute (ha ha), which should be true if any of our 'special'
199 booleans are true.  Implies that the reading does not represent a bit 
200 of text found in a witness.
201
202 =cut
203
204 sub is_meta {
205         my $self = shift;
206         return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;    
207 }
208
209 =head1 Convenience methods
210
211 =head2 related_readings
212
213 Calls Collation's related_readings with $self as the first argument.
214
215 =cut
216
217 sub related_readings {
218         my $self = shift;
219         return $self->collation->related_readings( $self, @_ );
220 }
221
222 =head2 witnesses 
223
224 Calls Collation's reading_witnesses with $self as the first argument.
225
226 =cut
227
228 sub witnesses {
229         my $self = shift;
230         return $self->collation->reading_witnesses( $self, @_ );
231 }
232
233 =head2 predecessors
234
235 Returns a list of Reading objects that immediately precede $self in the collation.
236
237 =cut
238
239 sub predecessors {
240         my $self = shift;
241         my @pred = $self->collation->sequence->predecessors( $self->id );
242         return map { $self->collation->reading( $_ ) } @pred;
243 }
244
245 =head2 successors
246
247 Returns a list of Reading objects that immediately follow $self in the collation.
248
249 =cut
250
251 sub successors {
252         my $self = shift;
253         my @succ = $self->collation->sequence->successors( $self->id );
254         return map { $self->collation->reading( $_ ) } @succ;
255 }
256
257 =head2 set_identical( $other_reading)
258
259 Backwards compatibility method, to add a transposition relationship
260 between $self and $other_reading.  Don't use this.
261
262 =cut
263
264 sub set_identical {
265         my( $self, $other ) = @_;
266         return $self->collation->add_relationship( $self, $other, 
267                 { 'type' => 'transposition' } );
268 }
269
270 sub _stringify {
271         my $self = shift;
272         return $self->id;
273 }
274
275 =head1 MORPHOLOGY
276
277 A few methods to try to tack on morphological information.
278
279 =head2 use_lexemes
280
281 TBD
282
283 =cut
284
285 # sub use_lexemes {
286 #       my( $self, @lexemes ) = @_;
287 #       # The lexemes need to be the same as $self->text.
288 #       my $cmpstr = $self->has_normal_form ? lc( $self->normal_form ) : lc( $self->text );
289 #       $cmpstr =~ s/[\s-]+//g;
290 #       my $lexstr = lc( join( '', @lexemes ) );
291 #       $lexstr =~ s/[\s-]+//g;
292 #       unless( $lexstr eq $cmpstr ) {
293 #               warn "Cannot split " . $self->text . " into " . join( '.', @lexemes );
294 #               return;
295 #       }
296 #       $self->_clear_morph;
297 #       map { $self->_add_morph( { $_ => [] } ) } @lexemes;
298 # }
299
300 # sub add_morphological_tag {
301 #       my( $self, $lexeme, $opts ) = @_;
302 #       my $struct;
303 #       unless( $opts ) {
304 #               # No lexeme was passed; use reading text.
305 #               $opts = $lexeme;
306 #               $lexeme = $self->text;
307 #               $self->use_lexemes( $lexeme );
308 #       }
309 #       # Get the correct container
310 #       ( $struct ) = grep { exists $_->{$lexeme} } $self->lexemes;
311 #       unless( $struct ) {
312 #               warn "No lexeme $lexeme exists in this reading";
313 #               return;
314 #       }
315 #       # Now make the morph object and add it to this lexeme.
316 #       my $morph_obj = Text::Tradition::Collation::Reading::Morphology->new( $opts );
317 #       # TODO Check for existence
318 #       push( @{$struct->{$lexeme}}, $morph_obj );
319 # }
320
321 ## Utility methods
322
323 sub TO_JSON {
324         my $self = shift;
325         return $self->text;
326 }
327
328 ## TODO will need a throw() here
329
330 no Moose;
331 __PACKAGE__->meta->make_immutable;
332
333 ###################################################
334 ### Morphology objects, to be attached to readings
335 ###################################################
336
337 package Text::Tradition::Collation::Reading::Morphology;
338
339 use Moose;
340
341 has 'lemma' => (
342         is => 'ro',
343         isa => 'Str',
344         required => 1,
345         );
346         
347 has 'code' => (
348         is => 'ro',
349         isa => 'Str',
350         required => 1,
351         );
352         
353 has 'language' => (
354         is => 'ro',
355         isa => 'Str',
356         required => 1,
357         );
358         
359 ## Transmute codes into comparison arrays for our various languages.
360
361 around BUILDARGS => sub {
362         my $orig = shift;
363         my $class = shift;
364         my $args;
365         if( @_ == 1 && ref( $_[0] ) ) {
366                 $args = shift;
367         } else {
368                 $args = { @_ };
369         }
370         if( exists( $args->{'serial'} ) ) {
371                 my( $lemma, $code ) = split( /!!/, delete $args->{'serial'} );
372                 $args->{'lemma'} = $lemma;
373                 $args->{'code'} = $code;
374         }
375         $class->$orig( $args );
376 };
377
378 sub serialization {
379         my $self = shift;
380         return join( '!!', $self->lemma, $self->code );
381 };
382
383 sub comparison_array {
384         my $self = shift;
385         if( $self->language eq 'French' ) {
386                 my @array;
387                 my @bits = split( /\+/, $self->code );
388                 # First push the non k/v parts.
389                 while( @bits && $bits[0] !~ /=/ ) {
390                         push( @array, shift @bits );
391                 }
392                 while( @array < 2 ) {
393                         push( @array, undef );
394                 }
395                 # Now push the k/v parts in a known order.
396                 my @fields = qw/ Pers Nb Temps Genre Spec Fonc /;
397                 my %props;
398                 map { my( $k, $v ) = split( /=/, $_ ); $props{$k} = $v; } @bits;
399                 foreach my $k ( @fields ) {
400                         push( @array, $props{$k} );
401                 }
402                 # Give the answer.
403                 return @array;
404         } elsif( $self->language eq 'English' ) {
405                 # Do something as yet undetermined
406         } else {
407                 # Latin or Greek or Armenian, just split the chars
408                 return split( '', $self->code );
409         }
410 };
411
412 no Moose;
413 __PACKAGE__->meta->make_immutable;
414
415 1;
416