tests passing with new library, yay
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
CommitLineData
dd3b58b0 1package Text::Tradition;
2
4a8828f0 3use Module::Load;
dd3b58b0 4use Moose;
8e1394aa 5use Text::Tradition::Collation;
6use Text::Tradition::Witness;
dd3b58b0 7
8has 'collation' => (
8e1394aa 9 is => 'ro',
10 isa => 'Text::Tradition::Collation',
11 writer => '_save_collation',
12 );
dd3b58b0 13
14has 'witnesses' => (
8e1394aa 15 traits => ['Array'],
16 is => 'rw',
17 isa => 'ArrayRef[Text::Tradition::Witness]',
18 handles => {
4a8828f0 19 all => 'elements',
20 add => 'push',
8e1394aa 21 },
4a8828f0 22 default => sub { [] },
8e1394aa 23 );
c5104dc0 24
8e1394aa 25sub BUILD {
26 my( $self, $init_args ) = @_;
c5104dc0 27
8e1394aa 28 if( exists $init_args->{'witnesses'} ) {
c5104dc0 29 # We got passed an uncollated list of witnesses. Make a
30 # witness object for each witness, and then send them to the
31 # collator.
32 my $autosigil = 0;
8e1394aa 33 foreach my $wit ( %{$init_args->{'witnesses'}} ) {
c5104dc0 34 # Each item in the list is either a string or an arrayref.
35 # If it's a string, it is a filename; if it's an arrayref,
36 # it is a tuple of 'sigil, file'. Handle either case.
37 my $args;
38 if( ref( $wit ) eq 'ARRAY' ) {
39 $args = { 'sigil' => $wit->[0],
40 'file' => $wit->[1] };
41 } else {
42 $args = { 'sigil' => chr( $autosigil+65 ),
43 'file' => $wit };
44 $autosigil++;
45 }
8e1394aa 46 $self->witnesses->push( Text::Tradition::Witness->new( $args ) );
47 # TODO Now how to collate these?
c5104dc0 48 }
49 } else {
4a8828f0 50 # Else we need to parse some collation data. Make a Collation object
51 my $collation = Text::Tradition::Collation->new( %$init_args,
52 'tradition' => $self );
53 $self->_save_collation( $collation );
54
55 # Call the appropriate parser on the given data
56 my @formats = grep { /^(GraphML|CSV|CTE|TEI)$/ } keys( %$init_args );
57 my $format = shift( @formats );
58 unless( $format ) {
59 warn "No data given to create a collation; will initialize an empty one";
60 }
61 if( $format && $format =~ /^(CSV|CTE)$/ &&
62 !exists $init_args->{'base'} ) {
63 warn "Cannot make a collation from $format without a base text";
64 return;
65 }
66
67 # Starting point for all texts
68 my $last_node = $collation->add_reading( '#START#' );
69
70 # Now do the parsing.
71 my @sigla;
72 if( $format ) {
73 my @parseargs;
74 if( $format =~ /^(CSV|CTE)$/ ) {
75 @parseargs = ( 'base' => $init_args->{'base'},
76 'data' => $init_args->{$format},
77 'format' => $format );
78 $format = 'BaseText';
79 } else {
80 @parseargs = ( $init_args->{ $format } );
81 }
82 my $mod = "Text::Tradition::Parser::$format";
83 load( $mod );
84 $mod->can('parse')->( $self, @parseargs );
85 }
c5104dc0 86 }
8e1394aa 87}
c5104dc0 88
de51424a 89sub witness {
90 my( $self, $sigil ) = @_;
91 my $requested_wit;
92 foreach my $wit ( @{$self->witnesses} ) {
93 $requested_wit = $wit if $wit->sigil eq $sigil;
94 }
95 warn "No such witness $sigil" unless $requested_wit;
96 return $requested_wit;
97}
98
99
4a8828f0 100sub add_witness {
101 my $self = shift;
102 my $new_wit = Text::Tradition::Witness->new( @_ );
103 push( @{$self->witnesses}, $new_wit );
104}
105
dd3b58b0 106# The user will usually be instantiating a Tradition object, and
107# examining its collation. The information about the tradition can
108# come via several routes:
109# - graphML from CollateX or elsewhere, standalone
110# - TEI parallel segmentation
111# - Leuven-style spreadsheet of variants, converted to CSV, plus base text
112# - apparatus pulled from CTE, plus base text
113# From this we should be able to get basic witness information.
114#
115# Alternatively the user can just give us the uncollated texts. Then
116# instead of passing a collation, s/he is passing a set of witnesses
117# from which we will generate a collation. Those witnesses can be in
118# plaintext or in TEI with certain constraints adopted.
119
120# So the constructor for a tradition needs to take one of these infosets,
121# and construct the collation and the witness objects.
122
123no Moose;
124__PACKAGE__->meta->make_immutable;