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