fix circular-path bugs in the graph
[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
15d2d3df 35has 'uncorrected_path' => (
6a222840 36 is => 'rw',
15d2d3df 37 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
6a222840 38 predicate => 'has_ante_corr',
7854e12e 39 );
e2902068 40
41
784877d9 42sub BUILD {
43 my $self = shift;
d047cd52 44 if( $self->has_source ) {
784877d9 45 # Read the file and initialize the text.
d047cd52 46 open( WITNESS, $self->source ) or die "Could not open "
784877d9 47 . $self->file . "for reading";
48 # TODO support TEI as well as plaintext, sometime
49 my @words;
50 while(<WITNESS>) {
51 chomp;
52 push( @words, split( /\s+/, $_ ) );
53 }
54 close WITNESS;
d047cd52 55 $self->text( \@words );
784877d9 56 }
57}
58
de51424a 59# If the text is not present, and the path is, and this is a 'get'
60# request, generate text from path.
61around text => sub {
62 my $orig = shift;
63 my $self = shift;
64
65 if( $self->has_path && !$self->has_text && !@_ ) {
66 my @words = map { $_->label } @{$self->path};
67 $self->$orig( \@words );
68 }
69
70 $self->$orig( @_ );
71};
72
7854e12e 73
dd3b58b0 74no Moose;
75__PACKAGE__->meta->make_immutable;