break out punctuation from the rest of the reading text
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
index 9c30ea2..7c0daa9 100644 (file)
@@ -2,7 +2,6 @@ package Text::Tradition::Collation::Reading;
 
 use Moose;
 use overload '""' => \&_stringify, 'fallback' => 1;
-use Text::Tradition::Collation;
 
 =head1 NAME
 
@@ -37,6 +36,8 @@ Options include:
 
 =item is_lacuna - The 'reading' represents a known gap in the text.
 
+=item is_ph - A temporary placeholder for apparatus parsing purposes.  Do not use unless you know what you are doing.
+
 =item rank - The sequence number of the reading. This should probably not be set manually.
 
 =back
@@ -78,6 +79,23 @@ has 'text' => (
        is => 'ro',
        isa => 'Str',
        required => 1,
+       writer => 'alter_text',
+       );
+       
+has 'punctuation' => (
+       traits => ['Array'],
+       isa => 'ArrayRef[HashRef[Str]]',
+       default => sub { [] },
+       handles => {
+                       punctuation => 'elements',
+                       add_punctuation => 'push',
+                       },
+       );
+
+has 'separate_punctuation' => (
+       is => 'ro',
+       isa => 'Bool',
+       default => 1,
        );
 
 has 'is_start' => (
@@ -97,6 +115,12 @@ has 'is_lacuna' => (
     isa => 'Bool',
        default => undef,
     );
+    
+has 'is_ph' => (
+       is => 'ro',
+       isa => 'Bool',
+       default => undef,
+       );
 
 has 'rank' => (
     is => 'rw',
@@ -127,11 +151,40 @@ around BUILDARGS => sub {
        } elsif( exists $args->{'is_end'} ) {
                $args->{'id'} = '#END#';        # Change the ID to ensure we have only one
                $args->{'text'} = '#END#';
+       } elsif( exists $args->{'is_ph'} ) {
+               $args->{'text'} = $args->{'id'};
        }
        
        $class->$orig( $args );
 };
 
+# Post-process the given text, stripping punctuation if we are asked.
+sub BUILD {
+       my $self = shift;
+       if( $self->separate_punctuation && !$self->is_meta ) {
+               my $pos = 0;
+               my $wspunct = '';  # word sans punctuation
+               foreach my $char ( split( //, $self->text ) ) {
+                       if( $char =~ /^[[:punct:]]$/ ) {
+                               $self->add_punctuation( { 'char' => $char, 'pos' => $pos } );
+                       } else {
+                               $wspunct .= $char;
+                       }
+                       $pos++;
+               }
+               $self->alter_text( $wspunct );
+       }
+}
+
+sub punctuated_form {
+       my $self = shift;
+       my $word = $self->text;
+       foreach my $p ( sort { $a->{pos} <=> $b->{pos} } $self->punctuation ) {
+               substr( $word, $p->{pos}, 0, $p->{char} );
+       }
+       return $word;
+}
+
 =head2 is_meta
 
 A meta attribute (ha ha), which should be true if any of our 'special'
@@ -142,7 +195,7 @@ of text found in a witness.
 
 sub is_meta {
        my $self = shift;
-       return $self->is_start || $self->is_end || $self->is_lacuna;    
+       return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;    
 }
 
 # Some syntactic sugar
@@ -151,6 +204,18 @@ sub related_readings {
        return $self->collation->related_readings( $self, @_ );
 }
 
+sub predecessors {
+       my $self = shift;
+       my @pred = $self->collation->sequence->predecessors( $self->id );
+       return map { $self->collation->reading( $_ ) } @pred;
+}
+
+sub successors {
+       my $self = shift;
+       my @succ = $self->collation->sequence->successors( $self->id );
+       return map { $self->collation->reading( $_ ) } @succ;
+}
+
 sub set_identical {
        my( $self, $other ) = @_;
        return $self->collation->add_relationship( $self, $other,