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