Commit | Line | Data |
dd3b58b0 |
1 | package Text::Tradition; |
2 | |
4a8828f0 |
3 | use Module::Load; |
dd3b58b0 |
4 | use Moose; |
8e1394aa |
5 | use Text::Tradition::Collation; |
56cf65bd |
6 | use Text::Tradition::Stemma; |
8e1394aa |
7 | use Text::Tradition::Witness; |
dd3b58b0 |
8 | |
331c2dbf |
9 | use vars qw( $VERSION ); |
85030ed0 |
10 | $VERSION = "0.3"; |
331c2dbf |
11 | |
dd3b58b0 |
12 | has 'collation' => ( |
8e1394aa |
13 | is => 'ro', |
14 | isa => 'Text::Tradition::Collation', |
15 | writer => '_save_collation', |
16 | ); |
dd3b58b0 |
17 | |
3b853983 |
18 | has 'witness_hash' => ( |
19 | traits => ['Hash'], |
20 | isa => 'HashRef[Text::Tradition::Witness]', |
8e1394aa |
21 | handles => { |
3b853983 |
22 | witness => 'get', |
23 | add_witness => 'set', |
24 | del_witness => 'delete', |
25 | has_witness => 'exists', |
26 | witnesses => 'values', |
8e1394aa |
27 | }, |
3b853983 |
28 | default => sub { {} }, |
8e1394aa |
29 | ); |
c5104dc0 |
30 | |
df6d9812 |
31 | has 'name' => ( |
32 | is => 'rw', |
33 | isa => 'Str', |
34 | default => 'Tradition', |
35 | ); |
56cf65bd |
36 | |
55bc8c78 |
37 | has 'language' => ( |
38 | is => 'ro', |
39 | isa => 'Str', |
40 | ); |
41 | |
e0d617e6 |
42 | has 'stemmata' => ( |
43 | traits => ['Array'], |
44 | isa => 'ArrayRef[Text::Tradition::Stemma]', |
45 | handles => { |
7f52eac8 |
46 | stemmata => 'elements', |
e0d617e6 |
47 | _add_stemma => 'push', |
48 | stemma => 'get', |
49 | stemma_count => 'count', |
50 | clear_stemmata => 'clear', |
51 | }, |
c3c79612 |
52 | default => sub { [] }, |
56cf65bd |
53 | ); |
3b853983 |
54 | |
55 | # Create the witness before trying to add it |
910a0a6d |
56 | around 'add_witness' => sub { |
57 | my $orig = shift; |
58 | my $self = shift; |
331c2dbf |
59 | # TODO allow add of a Witness object? |
910a0a6d |
60 | my $new_wit = Text::Tradition::Witness->new( @_ ); |
3b853983 |
61 | $self->$orig( $new_wit->sigil => $new_wit ); |
910a0a6d |
62 | return $new_wit; |
63 | }; |
331c2dbf |
64 | |
3b853983 |
65 | # Allow deletion of witness by object as well as by sigil |
66 | around 'del_witness' => sub { |
67 | my $orig = shift; |
68 | my $self = shift; |
69 | my @key_args; |
70 | foreach my $arg ( @_ ) { |
71 | push( @key_args, |
72 | ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg ); |
73 | } |
74 | return $self->$orig( @key_args ); |
75 | }; |
76 | |
77 | # Don't allow an empty hash value |
78 | around 'witness' => sub { |
79 | my( $orig, $self, $arg ) = @_; |
80 | return unless $self->has_witness( $arg ); |
81 | return $self->$orig( $arg ); |
82 | }; |
83 | |
331c2dbf |
84 | =head1 NAME |
85 | |
86 | Text::Tradition - a software model for a set of collated texts |
87 | |
88 | =head1 SYNOPSIS |
89 | |
90 | use Text::Tradition; |
91 | my $t = Text::Tradition->new( |
92 | 'name' => 'this is a text', |
93 | 'input' => 'TEI', |
94 | 'file' => '/path/to/tei_parallel_seg_file.xml' ); |
95 | |
96 | my @text_wits = $t->witnesses(); |
97 | my $manuscript_a = $t->witness( 'A' ); |
98 | my $new_ms = $t->add_witness( 'sigil' => 'B' ); |
99 | |
100 | my $text_path_svg = $t->collation->as_svg(); |
101 | ## See Text::Tradition::Collation for more on text collation itself |
102 | |
103 | =head1 DESCRIPTION |
104 | |
105 | Text::Tradition is a library for representation and analysis of collated |
106 | texts, particularly medieval ones. A 'tradition' refers to the aggregation |
107 | of surviving versions of a text, generally preserved in multiple |
108 | manuscripts (or 'witnesses'). A Tradition object thus has one more more |
109 | Witnesses, as well as a Collation that represents the unity of all versions |
110 | of the text. |
111 | |
112 | =head1 METHODS |
113 | |
114 | =head2 new |
115 | |
116 | Creates and returns a new text tradition object. The following options are |
117 | accepted. |
118 | |
119 | General options: |
120 | |
121 | =over 4 |
122 | |
123 | =item B<name> - The name of the text. |
124 | |
125 | =back |
126 | |
127 | Initialization based on a collation file: |
128 | |
129 | =over 4 |
130 | |
131 | =item B<input> - The input format of the collation file. Can be one of the |
132 | following: |
133 | |
134 | =over 4 |
135 | |
136 | =item * Self - a GraphML format produced by this module |
137 | |
138 | =item * CollateX - a GraphML format produced by CollateX |
139 | |
140 | =item * CTE - a TEI XML format produced by Classical Text Editor |
141 | |
a731e73a |
142 | =item * JSON - an alignment table in JSON format, as produced by CollateX and other tools |
143 | |
331c2dbf |
144 | =item * KUL - a specific CSV format for variants, not documented here |
145 | |
146 | =item * TEI - a TEI parallel segmentation format file |
147 | |
148 | =item * Tabular - a comma- or tab-separated collation. Takes an additional |
149 | option, 'sep_char', which defaults to the tab character. |
150 | |
151 | =back |
152 | |
153 | =item B<file> - The name of the file that contains the data. One of 'file' |
154 | or 'string' should be specified. |
155 | |
156 | =item B<string> - A text string that contains the data. One of 'file' or |
157 | 'string' should be specified. |
158 | |
159 | =item B<base> - The name of a text file that contains the base text, to be |
160 | used with input formats that require it (currently only KUL). |
161 | |
162 | =back |
163 | |
164 | Initialization based on a list of witnesses [NOT YET IMPLEMENTED]: |
165 | |
166 | =over 4 |
167 | |
168 | =item B<witnesses> - A reference to an array of Text::Tradition::Witness |
169 | objects that carry the text to be collated. |
170 | |
171 | =item B<collator> - A reference to a collation program that will accept |
172 | Witness objects. |
173 | |
174 | =back |
175 | |
176 | =head2 B<witnesses> |
177 | |
178 | Return the Text::Tradition::Witness objects associated with this tradition, |
179 | as an array. |
180 | |
044d1e45 |
181 | =head2 B<witness>( $sigil ) |
182 | |
183 | Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef |
184 | if there is no such object within the tradition. |
185 | |
331c2dbf |
186 | =head2 B<add_witness>( %opts ) |
187 | |
188 | Instantiate a new witness with the given options (see documentation for |
189 | Text::Tradition::Witness) and add it to the tradition. |
190 | |
044d1e45 |
191 | =head2 B<del_witness>( $sigil ) |
192 | |
193 | Delete the witness with the given sigil from the tradition. Returns the |
194 | witness object for the deleted witness. |
195 | |
331c2dbf |
196 | =begin testing |
197 | |
198 | use_ok( 'Text::Tradition', "can use module" ); |
199 | |
200 | my $t = Text::Tradition->new( 'name' => 'empty' ); |
201 | is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" ); |
202 | is( $t->name, 'empty', "object has the right name" ); |
203 | is( scalar $t->witnesses, 0, "object has no witnesses" ); |
204 | |
205 | my $simple = 't/data/simple.txt'; |
206 | my $s = Text::Tradition->new( |
207 | 'name' => 'inline', |
208 | 'input' => 'Tabular', |
209 | 'file' => $simple, |
210 | ); |
211 | is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" ); |
212 | is( $s->name, 'inline', "object has the right name" ); |
213 | is( scalar $s->witnesses, 3, "object has three witnesses" ); |
214 | |
044d1e45 |
215 | my $wit_a = $s->witness('A'); |
216 | is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" ); |
217 | if( $wit_a ) { |
218 | is( $wit_a->sigil, 'A', "Witness A has the right sigil" ); |
219 | } |
220 | is( $s->witness('X'), undef, "There is no witness X" ); |
221 | ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" ); |
222 | |
223 | my $wit_d = $s->add_witness( 'sigil' => 'D' ); |
224 | is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" ); |
225 | is( $wit_d->sigil, 'D', "witness has correct sigil" ); |
331c2dbf |
226 | is( scalar $s->witnesses, 4, "object now has four witnesses" ); |
227 | |
3b853983 |
228 | my $del = $s->del_witness( 'D' ); |
044d1e45 |
229 | is( $del, $wit_d, "Deleted correct witness" ); |
3b853983 |
230 | is( scalar $s->witnesses, 3, "object has three witnesses again" ); |
231 | |
331c2dbf |
232 | # TODO test initialization by witness list when we have it |
233 | |
234 | =end testing |
235 | |
236 | =cut |
910a0a6d |
237 | |
df6d9812 |
238 | |
8e1394aa |
239 | sub BUILD { |
240 | my( $self, $init_args ) = @_; |
c5104dc0 |
241 | |
8e1394aa |
242 | if( exists $init_args->{'witnesses'} ) { |
910a0a6d |
243 | # We got passed an uncollated list of witnesses. Make a |
244 | # witness object for each witness, and then send them to the |
245 | # collator. |
246 | my $autosigil = 0; |
247 | foreach my $wit ( %{$init_args->{'witnesses'}} ) { |
248 | # Each item in the list is either a string or an arrayref. |
249 | # If it's a string, it is a filename; if it's an arrayref, |
250 | # it is a tuple of 'sigil, file'. Handle either case. |
251 | my $args; |
252 | if( ref( $wit ) eq 'ARRAY' ) { |
253 | $args = { 'sigil' => $wit->[0], |
254 | 'file' => $wit->[1] }; |
255 | } else { |
256 | $args = { 'sigil' => chr( $autosigil+65 ), |
257 | 'file' => $wit }; |
258 | $autosigil++; |
259 | } |
260 | $self->witnesses->add_witness( $args ); |
261 | # TODO Now how to collate these? |
262 | } |
c5104dc0 |
263 | } else { |
910a0a6d |
264 | # Else we need to parse some collation data. Make a Collation object |
265 | my $collation = Text::Tradition::Collation->new( %$init_args, |
266 | 'tradition' => $self ); |
267 | $self->_save_collation( $collation ); |
4a8828f0 |
268 | |
910a0a6d |
269 | # Call the appropriate parser on the given data |
a731e73a |
270 | my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /; |
dfc37e38 |
271 | my @format_basetext = qw/ KUL /; |
272 | my $use_base; |
273 | my $format = $init_args->{'input'}; |
dfc37e38 |
274 | if( $format && !( grep { $_ eq $format } @format_standalone ) |
275 | && !( grep { $_ eq $format } @format_basetext ) ) { |
276 | warn "Unrecognized input format $format; not parsing"; |
910a0a6d |
277 | return; |
278 | } |
dfc37e38 |
279 | if( $format && grep { $_ eq $format } @format_basetext ) { |
280 | $use_base = 1; |
281 | if( !exists $init_args->{'base'} ) { |
282 | warn "Cannot make a collation from $format without a base text"; |
283 | return; |
284 | } |
285 | } |
4a8828f0 |
286 | |
910a0a6d |
287 | # Now do the parsing. |
910a0a6d |
288 | if( $format ) { |
dfc37e38 |
289 | if( $use_base ) { |
290 | $format = 'BaseText'; # Use the BaseText module for parsing, |
291 | # but retain the original input arg. |
910a0a6d |
292 | } |
293 | my $mod = "Text::Tradition::Parser::$format"; |
294 | load( $mod ); |
dfc37e38 |
295 | $mod->can('parse')->( $self, $init_args ); |
910a0a6d |
296 | } |
c5104dc0 |
297 | } |
8e1394aa |
298 | } |
c5104dc0 |
299 | |
56cf65bd |
300 | =head2 add_stemma( $dotfile ) |
301 | |
302 | Initializes a Text::Tradition::Stemma object from the given dotfile, |
303 | and associates it with the tradition. |
304 | |
305 | =begin testing |
306 | |
307 | use Text::Tradition; |
308 | |
309 | my $t = Text::Tradition->new( |
310 | 'name' => 'simple test', |
311 | 'input' => 'Tabular', |
312 | 'file' => 't/data/simple.txt', |
313 | ); |
314 | |
c3c79612 |
315 | is( $t->stemma_count, 0, "No stemmas added yet" ); |
56cf65bd |
316 | my $s; |
9ba651b9 |
317 | ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" ); |
56cf65bd |
318 | is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" ); |
e0d617e6 |
319 | is( $t->stemma_count, 1, "Tradition claims to have a stemma" ); |
320 | is( $t->stemma(0), $s, "Tradition hands back the right stemma" ); |
56cf65bd |
321 | |
322 | =end testing |
323 | |
324 | =cut |
325 | |
326 | sub add_stemma { |
bffafb73 |
327 | my $self = shift; |
328 | my %opts = @_; |
329 | my $stemma_fh; |
330 | if( $opts{'dotfile'} ) { |
331 | open $stemma_fh, '<', $opts{'dotfile'} |
332 | or warn "Could not open file " . $opts{'dotfile'}; |
333 | } elsif( $opts{'dot'} ) { |
334 | my $str = $opts{'dot'}; |
335 | open $stemma_fh, '<', \$str; |
336 | } |
337 | # Assume utf-8 |
338 | binmode $stemma_fh, ':utf8'; |
56cf65bd |
339 | my $stemma = Text::Tradition::Stemma->new( |
340 | 'collation' => $self->collation, |
341 | 'dot' => $stemma_fh ); |
342 | $self->_add_stemma( $stemma ) if $stemma; |
343 | return $stemma; |
344 | } |
345 | |
dd3b58b0 |
346 | no Moose; |
347 | __PACKAGE__->meta->make_immutable; |
331c2dbf |
348 | |
349 | |
350 | =head1 BUGS / TODO |
351 | |
352 | =over |
353 | |
354 | =item * Allow tradition to be initialized via passing to a collator. |
355 | |
356 | =back |
357 | |
358 | =head1 LICENSE |
359 | |
360 | This package is free software and is provided "as is" without express |
361 | or implied warranty. You can redistribute it and/or modify it under |
362 | the same terms as Perl itself. |
363 | |
364 | =head1 AUTHOR |
365 | |
366 | Tara L Andrews E<lt>aurum@cpan.orgE<gt> |