Commit | Line | Data |
dd3b58b0 |
1 | package Text::Tradition::Witness; |
2 | use Moose; |
7854e12e |
3 | use Moose::Util::TypeConstraints; |
dd3b58b0 |
4 | |
784877d9 |
5 | # Sigil. Required identifier for a witness. |
dd3b58b0 |
6 | has '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 |
15 | has '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. |
23 | has 'source' => ( |
24 | is => 'ro', |
25 | isa => 'Str', |
8e1394aa |
26 | predicate => 'has_source', |
d047cd52 |
27 | ); |
784877d9 |
28 | |
1ed3973e |
29 | # Path. This is an array of Reading nodes that should mirror the |
30 | # text above. |
4a8828f0 |
31 | has 'path' => ( |
32 | is => 'rw', |
33 | isa => 'ArrayRef[Text::Tradition::Collation::Reading]', |
de51424a |
34 | predicate => 'has_path', |
4a8828f0 |
35 | ); |
36 | |
b15511bf |
37 | has 'uncorrected_path' => ( |
6a222840 |
38 | is => 'rw', |
b15511bf |
39 | isa => 'ArrayRef[Text::Tradition::Collation::Reading]', |
40 | predicate => 'has_ante_corr', |
7854e12e |
41 | ); |
f6066bac |
42 | |
43 | # Manuscript name or similar |
44 | has 'identifier' => ( |
45 | is => 'ro', |
46 | isa => 'Str', |
47 | ); |
48 | |
49 | # Any other info we have |
50 | has 'other_info' => ( |
51 | is => 'ro', |
52 | isa => 'Str', |
53 | ); |
e2902068 |
54 | |
55 | |
784877d9 |
56 | sub BUILD { |
57 | my $self = shift; |
d047cd52 |
58 | if( $self->has_source ) { |
784877d9 |
59 | # Read the file and initialize the text. |
d047cd52 |
60 | open( WITNESS, $self->source ) or die "Could not open " |
784877d9 |
61 | . $self->file . "for reading"; |
62 | # TODO support TEI as well as plaintext, sometime |
63 | my @words; |
64 | while(<WITNESS>) { |
65 | chomp; |
66 | push( @words, split( /\s+/, $_ ) ); |
67 | } |
68 | close WITNESS; |
d047cd52 |
69 | $self->text( \@words ); |
784877d9 |
70 | } |
71 | } |
72 | |
de51424a |
73 | # If the text is not present, and the path is, and this is a 'get' |
74 | # request, generate text from path. |
75 | around text => sub { |
76 | my $orig = shift; |
77 | my $self = shift; |
78 | |
79 | if( $self->has_path && !$self->has_text && !@_ ) { |
80 | my @words = map { $_->label } @{$self->path}; |
81 | $self->$orig( \@words ); |
82 | } |
83 | |
84 | $self->$orig( @_ ); |
85 | }; |
86 | |
dd3b58b0 |
87 | no Moose; |
88 | __PACKAGE__->meta->make_immutable; |