working with new base text merge routine, up to line 25
[scpubgit/stemmatology.git] / lib / Text / Tradition / Witness.pm
CommitLineData
dd3b58b0 1package Text::Tradition::Witness;
2use Moose;
7854e12e 3use Moose::Util::TypeConstraints;
dd3b58b0 4
784877d9 5# Sigil. Required identifier for a witness.
dd3b58b0 6has 'sigil' => (
d047cd52 7 is => 'ro',
8 isa => 'Str',
9 required => 1,
10 );
dd3b58b0 11
d047cd52 12# Text. This is an array of strings (i.e. word tokens).
13# TODO Think about how to handle this for the case of pre-prepared
14# collations, where the tokens are in the graph already.
dd3b58b0 15has 'text' => (
d047cd52 16 is => 'rw',
17 isa => 'ArrayRef[Str]',
de51424a 18 predicate => 'has_text',
d047cd52 19 );
dd3b58b0 20
d047cd52 21# Source. This is where we read in the witness, if not from a
22# pre-prepared collation. It is probably a filename.
23has 'source' => (
24 is => 'ro',
25 isa => 'Str',
8e1394aa 26 predicate => 'has_source',
d047cd52 27 );
784877d9 28
4a8828f0 29has 'path' => (
30 is => 'rw',
31 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
de51424a 32 predicate => 'has_path',
4a8828f0 33 );
34
7854e12e 35subtype 'Correction',
36 as 'ArrayRef',
37 where { @{$_} == 3 &&
6a222840 38 find_type_constraint('Int')->check( $_->[0] ) &&
39 find_type_constraint('Int')->check( $_->[1] ) &&
40 find_type_constraint('ArrayRef[Text::Tradition::Collation::Reading]')->check( $_->[2] );
7854e12e 41 },
42 message { 'Correction must be a tuple of [offset, length, list]' };
43
6a222840 44has 'ante_corr' => (
45 is => 'rw',
7854e12e 46 isa => 'ArrayRef[Correction]',
6a222840 47 predicate => 'has_ante_corr',
7854e12e 48 );
e2902068 49
50
784877d9 51sub BUILD {
52 my $self = shift;
d047cd52 53 if( $self->has_source ) {
784877d9 54 # Read the file and initialize the text.
d047cd52 55 open( WITNESS, $self->source ) or die "Could not open "
784877d9 56 . $self->file . "for reading";
57 # TODO support TEI as well as plaintext, sometime
58 my @words;
59 while(<WITNESS>) {
60 chomp;
61 push( @words, split( /\s+/, $_ ) );
62 }
63 close WITNESS;
d047cd52 64 $self->text( \@words );
784877d9 65 }
66}
67
de51424a 68# If the text is not present, and the path is, and this is a 'get'
69# request, generate text from path.
70around text => sub {
71 my $orig = shift;
72 my $self = shift;
73
74 if( $self->has_path && !$self->has_text && !@_ ) {
75 my @words = map { $_->label } @{$self->path};
76 $self->$orig( \@words );
77 }
78
79 $self->$orig( @_ );
80};
81
6a222840 82sub uncorrected_path {
7854e12e 83 my $self = shift;
930ff666 84
85 my @new_path;
86 push( @new_path, @{$self->path} );
87 my $drift = 0;
6a222840 88 foreach my $change ( @{$self->ante_corr} ) {
89 my( $offset, $length, $items ) = @$change;
930ff666 90 my $realoffset = $offset + $drift;
91 splice( @new_path, $realoffset, $length, @$items );
92 $drift += @$items - $length;
93 }
94 return \@new_path;
7854e12e 95}
930ff666 96
7854e12e 97
dd3b58b0 98no Moose;
99__PACKAGE__->meta->make_immutable;