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