make witness plaintext parsing work
[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) in a collation.
9     
10 =head1 DESCRIPTION
11
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
16 a particular sequence
17
18 =head1 METHODS
19
20 =head2 new
21
22 Creates a new reading in the given collation with the given attributes. 
23 Options include:
24
25 =over 4
26
27 =item collation - The Text::Tradition::Collation object to which this reading belongs.  Required.
28
29 =item id - A unique identifier for this reading. Required.
30
31 =item text - The word or other text of the reading.
32
33 =item is_start - The reading is the starting point for the collation.
34
35 =item is_end - The reading is the ending point for the collation.
36
37 =item is_lacuna - The 'reading' represents a known gap in the text.
38
39 =item is_ph - A temporary placeholder for apparatus parsing purposes.  Do not use unless you know what you are doing.
40
41 =item rank - The sequence number of the reading. This should probably not be set manually.
42
43 =back
44
45 One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
46
47 =head2 collation
48
49 =head2 id
50
51 =head2 text
52
53 =head2 is_start
54
55 =head2 is_end
56
57 =head2 is_lacuna
58
59 =head2 rank
60
61 Accessor methods for the given attributes.
62
63 =cut
64
65 has 'collation' => (
66         is => 'ro',
67         isa => 'Text::Tradition::Collation',
68         # required => 1,
69         weak_ref => 1,
70         );
71
72 has 'id' => (
73         is => 'ro',
74         isa => 'Str',
75         required => 1,
76         );
77
78 has 'text' => (
79         is => 'ro',
80         isa => 'Str',
81         required => 1,
82         writer => 'alter_text',
83         );
84         
85 has 'language' => (
86         is => 'ro',
87         isa => 'Str',
88         default => 'Default',
89         );
90         
91 has 'is_start' => (
92         is => 'ro',
93         isa => 'Bool',
94         default => undef,
95         );
96
97 has 'is_end' => (
98         is => 'ro',
99         isa => 'Bool',
100         default => undef,
101         );
102     
103 has 'is_lacuna' => (
104     is => 'ro',
105     isa => 'Bool',
106         default => undef,
107     );
108     
109 has 'is_ph' => (
110         is => 'ro',
111         isa => 'Bool',
112         default => undef,
113         );
114         
115 has 'is_common' => (
116         is => 'rw',
117         isa => 'Bool',
118         default => undef,
119         );
120
121 has 'rank' => (
122     is => 'rw',
123     isa => 'Int',
124     predicate => 'has_rank',
125     clearer => 'clear_rank',
126     );
127     
128 ## For morphological analysis
129
130 has 'normal_form' => (
131         is => 'rw',
132         isa => 'Str',
133         predicate => 'has_normal_form',
134         );
135
136 has 'lemma' => (
137         is => 'rw',
138         isa => 'Str',
139         predicate => 'has_lemma',
140         );
141
142 has 'morphology' => (
143         traits => ['Array'],
144         isa => 'ArrayRef[HashRef[ArrayRef[Text::Tradition::Collation::Reading::Morphology]]]',
145         handles => {
146                 lexemes => 'elements',
147                 has_morphology => 'count',
148                 _clear_morph => 'clear',
149                 _add_morph => '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 is_disambiguated
280
281 Returns true if there is only one tag per lexeme in this reading.
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 sub disambiguate {
322         my( $self, $lexeme, $index ) = @_;
323         my $struct;
324         unless( $index ) {
325                 # No lexeme was passed; use reading text.
326                 $index = $lexeme;
327                 $lexeme = $self->text;
328         }
329         # Get the correct container
330         ( $struct ) = grep { exists $_->{$lexeme} } $self->lexemes;
331         unless( $struct ) {
332                 warn "No lexeme $lexeme exists in this reading";
333                 return;
334         }
335         # Keep the object at the selected index
336         my $selected = $struct->{$lexeme}->[$index];
337         $struct->{$lexeme} = [ $selected ];
338 }
339
340 sub is_disambiguated {
341         my $self = shift;
342         return undef unless $self->has_morphology;
343         foreach my $lexeme ( $self->lexemes ) {
344                 my( $key ) = keys %$lexeme; # will be only one
345                 return undef unless @{$lexeme->{$key}} == 1;
346         }
347         return 1;
348 }
349
350 ## Utility methods
351
352 sub TO_JSON {
353         my $self = shift;
354         return $self->text;
355 }
356
357 ## TODO will need a throw() here
358
359 no Moose;
360 __PACKAGE__->meta->make_immutable;
361
362 ###################################################
363 ### Morphology objects, to be attached to readings
364 ###################################################
365
366 package Text::Tradition::Collation::Reading::Morphology;
367
368 use Moose;
369
370 has 'lemma' => (
371         is => 'ro',
372         isa => 'Str',
373         required => 1,
374         );
375         
376 has 'code' => (
377         is => 'ro',
378         isa => 'Str',
379         required => 1,
380         );
381         
382 has 'language' => (
383         is => 'ro',
384         isa => 'Str',
385         required => 1,
386         );
387         
388 ## Transmute codes into comparison arrays for our various languages.
389
390 around BUILDARGS => sub {
391         my $orig = shift;
392         my $class = shift;
393         my $args;
394         if( @_ == 1 && ref( $_[0] ) ) {
395                 $args = shift;
396         } else {
397                 $args = { @_ };
398         }
399         if( exists( $args->{'serial'} ) ) {
400                 my( $lemma, $code ) = split( /!!/, delete $args->{'serial'} );
401                 $args->{'lemma'} = $lemma;
402                 $args->{'code'} = $code;
403         }
404         $class->$orig( $args );
405 };
406
407 sub serialization {
408         my $self = shift;
409         return join( '!!', $self->lemma, $self->code );
410 };
411
412 sub comparison_array {
413         my $self = shift;
414         if( $self->language eq 'French' ) {
415                 my @array;
416                 my @bits = split( /\+/, $self->code );
417                 # First push the non k/v parts.
418                 while( @bits && $bits[0] !~ /=/ ) {
419                         push( @array, shift @bits );
420                 }
421                 while( @array < 2 ) {
422                         push( @array, undef );
423                 }
424                 # Now push the k/v parts in a known order.
425                 my @fields = qw/ Pers Nb Temps Genre Spec Fonc /;
426                 my %props;
427                 map { my( $k, $v ) = split( /=/, $_ ); $props{$k} = $v; } @bits;
428                 foreach my $k ( @fields ) {
429                         push( @array, $props{$k} );
430                 }
431                 # Give the answer.
432                 return @array;
433         } elsif( $self->language eq 'English' ) {
434                 # Do something as yet undetermined
435         } else {
436                 # Latin or Greek or Armenian, just split the chars
437                 return split( '', $self->code );
438         }
439 };
440
441 no Moose;
442 __PACKAGE__->meta->make_immutable;
443
444 1;
445