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