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