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