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