break out punctuation from the rest of the reading text
[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 'punctuation' => (
86         traits => ['Array'],
87         isa => 'ArrayRef[HashRef[Str]]',
88         default => sub { [] },
89         handles => {
90                         punctuation => 'elements',
91                         add_punctuation => 'push',
92                         },
93         );
94
95 has 'separate_punctuation' => (
96         is => 'ro',
97         isa => 'Bool',
98         default => 1,
99         );
100
101 has 'is_start' => (
102         is => 'ro',
103         isa => 'Bool',
104         default => undef,
105         );
106
107 has 'is_end' => (
108         is => 'ro',
109         isa => 'Bool',
110         default => undef,
111         );
112     
113 has 'is_lacuna' => (
114     is => 'ro',
115     isa => 'Bool',
116         default => undef,
117     );
118     
119 has 'is_ph' => (
120         is => 'ro',
121         isa => 'Bool',
122         default => undef,
123         );
124
125 has 'rank' => (
126     is => 'rw',
127     isa => 'Int',
128     predicate => 'has_rank',
129     );
130
131
132 around BUILDARGS => sub {
133         my $orig = shift;
134         my $class = shift;
135         my $args;
136         if( @_ == 1 ) {
137                 $args = shift;
138         } else {
139                 $args = { @_ };
140         }
141         
142         # If one of our special booleans is set, we change the text and the
143         # ID to match.
144         
145         if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
146                 $args->{'text'} = sprintf( "#LACUNA_%s#", $args->{'id'} );
147         } elsif( exists $args->{'is_start'} ) {
148                 $args->{'id'} = '#START#';  # Change the ID to ensure we have only one
149                 $args->{'text'} = '#START#';
150                 $args->{'rank'} = 0;
151         } elsif( exists $args->{'is_end'} ) {
152                 $args->{'id'} = '#END#';        # Change the ID to ensure we have only one
153                 $args->{'text'} = '#END#';
154         } elsif( exists $args->{'is_ph'} ) {
155                 $args->{'text'} = $args->{'id'};
156         }
157         
158         $class->$orig( $args );
159 };
160
161 # Post-process the given text, stripping punctuation if we are asked.
162 sub BUILD {
163         my $self = shift;
164         if( $self->separate_punctuation && !$self->is_meta ) {
165                 my $pos = 0;
166                 my $wspunct = '';  # word sans punctuation
167                 foreach my $char ( split( //, $self->text ) ) {
168                         if( $char =~ /^[[:punct:]]$/ ) {
169                                 $self->add_punctuation( { 'char' => $char, 'pos' => $pos } );
170                         } else {
171                                 $wspunct .= $char;
172                         }
173                         $pos++;
174                 }
175                 $self->alter_text( $wspunct );
176         }
177 }
178
179 sub punctuated_form {
180         my $self = shift;
181         my $word = $self->text;
182         foreach my $p ( sort { $a->{pos} <=> $b->{pos} } $self->punctuation ) {
183                 substr( $word, $p->{pos}, 0, $p->{char} );
184         }
185         return $word;
186 }
187
188 =head2 is_meta
189
190 A meta attribute (ha ha), which should be true if any of our 'special'
191 booleans are true.  Implies that the reading does not represent a bit 
192 of text found in a witness.
193
194 =cut
195
196 sub is_meta {
197         my $self = shift;
198         return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;    
199 }
200
201 # Some syntactic sugar
202 sub related_readings {
203         my $self = shift;
204         return $self->collation->related_readings( $self, @_ );
205 }
206
207 sub predecessors {
208         my $self = shift;
209         my @pred = $self->collation->sequence->predecessors( $self->id );
210         return map { $self->collation->reading( $_ ) } @pred;
211 }
212
213 sub successors {
214         my $self = shift;
215         my @succ = $self->collation->sequence->successors( $self->id );
216         return map { $self->collation->reading( $_ ) } @succ;
217 }
218
219 sub set_identical {
220         my( $self, $other ) = @_;
221         return $self->collation->add_relationship( $self, $other, 
222                 { 'type' => 'transposition' } );
223 }
224
225 sub _stringify {
226         my $self = shift;
227         return $self->id;
228 }
229
230 no Moose;
231 __PACKAGE__->meta->make_immutable;
232
233 1;
234