Commit | Line | Data |
dd3b58b0 |
1 | package Text::Tradition::Collation; |
d047cd52 |
2 | |
910a0a6d |
3 | use Encode qw( decode_utf8 ); |
4 | use File::Temp; |
c9bf3dbf |
5 | use Graph; |
d047cd52 |
6 | use Graph::Easy; |
8e1394aa |
7 | use IPC::Run qw( run binary ); |
910a0a6d |
8 | use Text::CSV_XS; |
3265b0ce |
9 | use Text::Tradition::Collation::Path; |
b15511bf |
10 | use Text::Tradition::Collation::Reading; |
11 | use Text::Tradition::Collation::Relationship; |
df6d9812 |
12 | use XML::LibXML; |
dd3b58b0 |
13 | use Moose; |
14 | |
15 | has 'graph' => ( |
d047cd52 |
16 | is => 'ro', |
17 | isa => 'Graph::Easy', |
18 | handles => { |
910a0a6d |
19 | add_reading => 'add_node', |
eca16057 |
20 | add_lacuna => 'add_node', |
910a0a6d |
21 | del_reading => 'del_node', |
910a0a6d |
22 | add_path => 'add_edge', |
23 | del_path => 'del_edge', |
24 | reading => 'node', |
25 | path => 'edge', |
26 | readings => 'nodes', |
910a0a6d |
27 | paths => 'edges', |
28 | relationships => 'edges', |
d047cd52 |
29 | }, |
30 | default => sub { Graph::Easy->new( undirected => 0 ) }, |
31 | ); |
910a0a6d |
32 | |
dd3b58b0 |
33 | |
f6066bac |
34 | has 'tradition' => ( # TODO should this not be ro? |
8e1394aa |
35 | is => 'rw', |
d047cd52 |
36 | isa => 'Text::Tradition', |
37 | ); |
dd3b58b0 |
38 | |
8e1394aa |
39 | has 'svg' => ( |
40 | is => 'ro', |
41 | isa => 'Str', |
42 | writer => '_save_svg', |
43 | predicate => 'has_svg', |
44 | ); |
45 | |
8e1394aa |
46 | has 'graphml' => ( |
47 | is => 'ro', |
df6d9812 |
48 | isa => 'Str', |
8e1394aa |
49 | writer => '_save_graphml', |
50 | predicate => 'has_graphml', |
51 | ); |
52 | |
910a0a6d |
53 | has 'csv' => ( |
54 | is => 'ro', |
55 | isa => 'Str', |
56 | writer => '_save_csv', |
57 | predicate => 'has_csv', |
58 | ); |
59 | |
3a1f2523 |
60 | # Keeps track of the lemmas within the collation. At most one lemma |
61 | # per position in the graph. |
62 | has 'lemmata' => ( |
63 | is => 'ro', |
64 | isa => 'HashRef[Maybe[Str]]', |
65 | default => sub { {} }, |
66 | ); |
67 | |
4a8828f0 |
68 | has 'wit_list_separator' => ( |
7854e12e |
69 | is => 'rw', |
70 | isa => 'Str', |
71 | default => ', ', |
72 | ); |
73 | |
74 | has 'baselabel' => ( |
75 | is => 'rw', |
76 | isa => 'Str', |
77 | default => 'base text', |
78 | ); |
4a8828f0 |
79 | |
1f563ac3 |
80 | has 'collapsed' => ( |
15d2d3df |
81 | is => 'rw', |
82 | isa => 'Bool', |
83 | ); |
84 | |
85 | has 'linear' => ( |
86 | is => 'rw', |
87 | isa => 'Bool', |
88 | default => 1, |
89 | ); |
1f563ac3 |
90 | |
ef9d481f |
91 | has 'ac_label' => ( |
92 | is => 'rw', |
93 | isa => 'Str', |
94 | default => ' (a.c.)', |
95 | ); |
96 | |
1f563ac3 |
97 | |
dd3b58b0 |
98 | # The collation can be created two ways: |
99 | # 1. Collate a set of witnesses (with CollateX I guess) and process |
100 | # the results as in 2. |
101 | # 2. Read a pre-prepared collation in one of a variety of formats, |
102 | # and make the graph from that. |
103 | |
104 | # The graph itself will (for now) be immutable, and the positions |
105 | # within the graph will also be immutable. We need to calculate those |
106 | # positions upon graph construction. The equivalences between graph |
107 | # nodes will be mutable, entirely determined by the user (or possibly |
108 | # by some semantic pre-processing provided by the user.) So the |
109 | # constructor should just make an empty equivalences object. The |
110 | # constructor will also need to make the witness objects, if we didn't |
111 | # come through option 1. |
112 | |
d047cd52 |
113 | sub BUILD { |
114 | my( $self, $args ) = @_; |
8e1394aa |
115 | $self->graph->use_class('node', 'Text::Tradition::Collation::Reading'); |
3265b0ce |
116 | $self->graph->use_class('edge', 'Text::Tradition::Collation::Path'); |
d047cd52 |
117 | |
4a8828f0 |
118 | # Pass through any graph-specific options. |
119 | my $shape = exists( $args->{'shape'} ) ? $args->{'shape'} : 'ellipse'; |
120 | $self->graph->set_attribute( 'node', 'shape', $shape ); |
94c00c71 |
121 | |
122 | # Start and end points for all texts |
123 | $self->start( 'INIT' ); |
124 | $self->end( 'INIT' ); |
d047cd52 |
125 | } |
784877d9 |
126 | |
eca16057 |
127 | around add_lacuna => sub { |
128 | my $orig = shift; |
129 | my $self = shift; |
130 | my $id = shift @_; |
131 | my $l = $self->$orig( '#LACUNA_' . $id . '#' ); |
132 | $l->is_lacuna( 1 ); |
133 | return $l; |
134 | }; |
135 | |
7854e12e |
136 | # Wrapper around add_path |
137 | |
138 | around add_path => sub { |
139 | my $orig = shift; |
140 | my $self = shift; |
141 | |
142 | # Make sure there are three arguments |
143 | unless( @_ == 3 ) { |
910a0a6d |
144 | warn "Call add_path with args source, target, witness"; |
145 | return; |
7854e12e |
146 | } |
147 | # Make sure the proposed path does not yet exist |
b15511bf |
148 | # NOTE 'reading' will currently return readings and segments |
7854e12e |
149 | my( $source, $target, $wit ) = @_; |
150 | $source = $self->reading( $source ) |
910a0a6d |
151 | unless ref( $source ) eq 'Text::Tradition::Collation::Reading'; |
7854e12e |
152 | $target = $self->reading( $target ) |
910a0a6d |
153 | unless ref( $target ) eq 'Text::Tradition::Collation::Reading'; |
7854e12e |
154 | foreach my $path ( $source->edges_to( $target ) ) { |
910a0a6d |
155 | if( $path->label eq $wit && $path->class eq 'edge.path' ) { |
156 | return; |
157 | } |
7854e12e |
158 | } |
159 | # Do the deed |
160 | $self->$orig( @_ ); |
161 | }; |
162 | |
3265b0ce |
163 | # Wrapper around paths |
164 | around paths => sub { |
165 | my $orig = shift; |
166 | my $self = shift; |
167 | |
b15511bf |
168 | my @result = grep { $_->sub_class eq 'path' } $self->$orig( @_ ); |
df6d9812 |
169 | return @result; |
170 | }; |
171 | |
172 | around relationships => sub { |
173 | my $orig = shift; |
174 | my $self = shift; |
b15511bf |
175 | my @result = grep { $_->sub_class eq 'relationship' } $self->$orig( @_ ); |
176 | return @result; |
177 | }; |
178 | |
7854e12e |
179 | # Wrapper around merge_nodes |
784877d9 |
180 | sub merge_readings { |
181 | my $self = shift; |
182 | my $first_node = shift; |
183 | my $second_node = shift; |
184 | $first_node->merge_from( $second_node ); |
185 | unshift( @_, $first_node, $second_node ); |
186 | return $self->graph->merge_nodes( @_ ); |
187 | } |
188 | |
15d2d3df |
189 | # Extra graph-alike utility |
190 | sub has_path { |
191 | my( $self, $source, $target, $label ) = @_; |
192 | my @paths = $source->edges_to( $target ); |
193 | my @relevant = grep { $_->label eq $label } @paths; |
b15511bf |
194 | return scalar @relevant; |
195 | } |
196 | |
3265b0ce |
197 | ## Dealing with relationships between readings. This is a different |
4cdd82f1 |
198 | ## sort of graph edge. Return a success/failure value and a list of |
199 | ## node pairs that have been linked. |
3265b0ce |
200 | |
201 | sub add_relationship { |
b15511bf |
202 | my( $self, $source, $target, $options ) = @_; |
ef9d481f |
203 | |
204 | # Make sure there is not another relationship between these two |
94c00c71 |
205 | # readings already |
ef9d481f |
206 | $source = $self->reading( $source ) |
910a0a6d |
207 | unless ref( $source ) && $source->isa( 'Graph::Easy::Node' ); |
ef9d481f |
208 | $target = $self->reading( $target ) |
910a0a6d |
209 | unless ref( $target ) && $target->isa( 'Graph::Easy::Node' ); |
4cdd82f1 |
210 | foreach my $rel ( $source->edges_to( $target ), $target->edges_to( $source ) ) { |
910a0a6d |
211 | if( $rel->class eq 'edge.relationship' ) { |
212 | return ( undef, "Relationship already exists between these readings" ); |
213 | } |
4cdd82f1 |
214 | } |
910a0a6d |
215 | if( $options->{'equal_rank'} && !relationship_valid( $source, $target ) ) { |
216 | return ( undef, 'Relationship creates witness loop' ); |
ef9d481f |
217 | } |
218 | |
910a0a6d |
219 | # TODO Think about positional hilarity if relationships are added after positions |
220 | # are assigned. |
221 | |
4cdd82f1 |
222 | my @joined = ( [ $source->name, $target->name ] ); # Keep track of the nodes we join. |
223 | |
224 | $options->{'this_relation'} = [ $source, $target ]; |
f6066bac |
225 | my $rel; |
226 | eval { $rel = Text::Tradition::Collation::Relationship->new( %$options ) }; |
227 | if( $@ ) { |
228 | return ( undef, $@ ); |
229 | } |
3265b0ce |
230 | $self->graph->add_edge( $source, $target, $rel ); |
910a0a6d |
231 | |
232 | # TODO Handle global relationship setting |
233 | |
4cdd82f1 |
234 | return( 1, @joined ); |
3265b0ce |
235 | } |
236 | |
910a0a6d |
237 | sub relationship_valid { |
238 | my( $source, $target ) = @_; |
239 | # Check that linking the source and target in a relationship won't lead |
240 | # to a path loop for any witness. |
241 | my @proposed_related = ( $source, $target ); |
242 | push( @proposed_related, $source->related_readings ); |
243 | push( @proposed_related, $target->related_readings ); |
244 | my %pr_ids; |
245 | map { $pr_ids{ $_->name } = 1 } @proposed_related; |
246 | # The lists of 'in' and 'out' should not have any element that appears |
247 | # in 'proposed_related'. |
248 | foreach my $pr ( @proposed_related ) { |
249 | foreach my $e ( $pr->incoming ) { |
250 | if( exists $pr_ids{ $e->from->name } ) { |
251 | return 0; |
252 | } |
253 | } |
254 | foreach my $e ( $pr->outgoing ) { |
255 | if( exists $pr_ids{ $e->to->name } ) { |
256 | return 0; |
257 | } |
258 | } |
259 | } |
260 | return 1; |
261 | } |
262 | |
8e1394aa |
263 | =head2 Output method(s) |
264 | |
265 | =over |
266 | |
267 | =item B<as_svg> |
268 | |
269 | print $graph->as_svg( $recalculate ); |
270 | |
271 | Returns an SVG string that represents the graph. Uses GraphViz to do |
4a8828f0 |
272 | this, because Graph::Easy doesn\'t cope well with long graphs. Unless |
8e1394aa |
273 | $recalculate is passed (and is a true value), the method will return a |
274 | cached copy of the SVG after the first call to the method. |
275 | |
276 | =cut |
277 | |
278 | sub as_svg { |
279 | my( $self, $recalc ) = @_; |
280 | return $self->svg if $self->has_svg; |
281 | |
3265b0ce |
282 | $self->collapse_graph_paths(); |
8e1394aa |
283 | |
284 | my @cmd = qw/dot -Tsvg/; |
285 | my( $svg, $err ); |
910a0a6d |
286 | my $dotfile = File::Temp->new(); |
d9e873d0 |
287 | ## TODO REMOVE |
eca16057 |
288 | # $dotfile->unlink_on_destroy(0); |
910a0a6d |
289 | binmode $dotfile, ':utf8'; |
290 | print $dotfile $self->as_dot(); |
291 | push( @cmd, $dotfile->filename ); |
292 | run( \@cmd, ">", binary(), \$svg ); |
293 | $svg = decode_utf8( $svg ); |
df6d9812 |
294 | $self->_save_svg( $svg ); |
3265b0ce |
295 | $self->expand_graph_paths(); |
8e1394aa |
296 | return $svg; |
297 | } |
298 | |
df6d9812 |
299 | =item B<as_dot> |
300 | |
301 | print $graph->as_dot( $view, $recalculate ); |
302 | |
303 | Returns a string that is the collation graph expressed in dot |
304 | (i.e. GraphViz) format. The 'view' argument determines what kind of |
305 | graph is produced. |
306 | * 'path': a graph of witness paths through the collation (DEFAULT) |
307 | * 'relationship': a graph of how collation readings relate to |
308 | each other |
309 | |
310 | =cut |
311 | |
312 | sub as_dot { |
313 | my( $self, $view ) = @_; |
314 | $view = 'path' unless $view; |
315 | # TODO consider making some of these things configurable |
316 | my $dot = sprintf( "digraph %s {\n", $self->tradition->name ); |
317 | $dot .= "\tedge [ arrowhead=open ];\n"; |
318 | $dot .= "\tgraph [ rankdir=LR ];\n"; |
319 | $dot .= sprintf( "\tnode [ fontsize=%d, fillcolor=%s, style=%s, shape=%s ];\n", |
910a0a6d |
320 | 11, "white", "filled", $self->graph->get_attribute( 'node', 'shape' ) ); |
df6d9812 |
321 | |
322 | foreach my $reading ( $self->readings ) { |
910a0a6d |
323 | # Need not output nodes without separate labels |
324 | next if $reading->name eq $reading->label; |
910a0a6d |
325 | $dot .= sprintf( "\t\"%s\" [ label=\"%s\" ];\n", $reading->name, $reading->label ); |
df6d9812 |
326 | } |
327 | |
328 | my @edges = $view eq 'relationship' ? $self->relationships : $self->paths; |
329 | foreach my $edge ( @edges ) { |
910a0a6d |
330 | my %variables = ( 'color' => '#000000', |
331 | 'fontcolor' => '#000000', |
332 | 'label' => $edge->label, |
333 | ); |
334 | my $varopts = join( ', ', map { $_.'="'.$variables{$_}.'"' } sort keys %variables ); |
335 | $dot .= sprintf( "\t\"%s\" -> \"%s\" [ %s ];\n", |
336 | $edge->from->name, $edge->to->name, $varopts ); |
df6d9812 |
337 | } |
df6d9812 |
338 | $dot .= "}\n"; |
339 | return $dot; |
340 | } |
341 | |
8e1394aa |
342 | =item B<as_graphml> |
343 | |
344 | print $graph->as_graphml( $recalculate ) |
345 | |
346 | Returns a GraphML representation of the collation graph, with |
347 | transposition information and position information. Unless |
348 | $recalculate is passed (and is a true value), the method will return a |
349 | cached copy of the SVG after the first call to the method. |
350 | |
351 | =cut |
352 | |
353 | sub as_graphml { |
354 | my( $self, $recalc ) = @_; |
355 | return $self->graphml if $self->has_graphml; |
356 | |
357 | # Some namespaces |
358 | my $graphml_ns = 'http://graphml.graphdrawing.org/xmlns'; |
359 | my $xsi_ns = 'http://www.w3.org/2001/XMLSchema-instance'; |
360 | my $graphml_schema = 'http://graphml.graphdrawing.org/xmlns ' . |
910a0a6d |
361 | 'http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'; |
8e1394aa |
362 | |
363 | # Create the document and root node |
364 | my $graphml = XML::LibXML->createDocument( "1.0", "UTF-8" ); |
365 | my $root = $graphml->createElementNS( $graphml_ns, 'graphml' ); |
366 | $graphml->setDocumentElement( $root ); |
367 | $root->setNamespace( $xsi_ns, 'xsi', 0 ); |
368 | $root->setAttributeNS( $xsi_ns, 'schemaLocation', $graphml_schema ); |
369 | |
f6066bac |
370 | # TODO Add some global graph data |
371 | |
8e1394aa |
372 | # Add the data keys for nodes |
ef9d481f |
373 | my %node_data_keys; |
374 | my $ndi = 0; |
d9e873d0 |
375 | foreach my $datum ( qw/ name reading identical rank class / ) { |
910a0a6d |
376 | $node_data_keys{$datum} = 'dn'.$ndi++; |
377 | my $key = $root->addNewChild( $graphml_ns, 'key' ); |
378 | $key->setAttribute( 'attr.name', $datum ); |
379 | $key->setAttribute( 'attr.type', 'string' ); |
380 | $key->setAttribute( 'for', 'node' ); |
381 | $key->setAttribute( 'id', $node_data_keys{$datum} ); |
8e1394aa |
382 | } |
383 | |
df6d9812 |
384 | # Add the data keys for edges, i.e. witnesses |
ef9d481f |
385 | my $edi = 0; |
386 | my %edge_data_keys; |
c9bf3dbf |
387 | my @string_keys = qw/ class witness relationship /; |
388 | my @bool_keys = qw/ extra equal_rank non_correctable non_independent /; |
389 | foreach my $edge_key( @string_keys ) { |
910a0a6d |
390 | $edge_data_keys{$edge_key} = 'de'.$edi++; |
391 | my $key = $root->addNewChild( $graphml_ns, 'key' ); |
392 | $key->setAttribute( 'attr.name', $edge_key ); |
c9bf3dbf |
393 | $key->setAttribute( 'attr.type', 'string' ); |
394 | $key->setAttribute( 'for', 'edge' ); |
395 | $key->setAttribute( 'id', $edge_data_keys{$edge_key} ); |
396 | } |
397 | foreach my $edge_key( @bool_keys ) { |
398 | $edge_data_keys{$edge_key} = 'de'.$edi++; |
399 | my $key = $root->addNewChild( $graphml_ns, 'key' ); |
400 | $key->setAttribute( 'attr.name', $edge_key ); |
401 | $key->setAttribute( 'attr.type', 'boolean' ); |
910a0a6d |
402 | $key->setAttribute( 'for', 'edge' ); |
403 | $key->setAttribute( 'id', $edge_data_keys{$edge_key} ); |
8e1394aa |
404 | } |
ef9d481f |
405 | |
8e1394aa |
406 | # Add the graph, its nodes, and its edges |
407 | my $graph = $root->addNewChild( $graphml_ns, 'graph' ); |
408 | $graph->setAttribute( 'edgedefault', 'directed' ); |
94c00c71 |
409 | $graph->setAttribute( 'id', $self->tradition->name ); |
8e1394aa |
410 | $graph->setAttribute( 'parse.edgeids', 'canonical' ); |
df6d9812 |
411 | $graph->setAttribute( 'parse.edges', scalar($self->paths) ); |
8e1394aa |
412 | $graph->setAttribute( 'parse.nodeids', 'canonical' ); |
df6d9812 |
413 | $graph->setAttribute( 'parse.nodes', scalar($self->readings) ); |
8e1394aa |
414 | $graph->setAttribute( 'parse.order', 'nodesfirst' ); |
415 | |
416 | my $node_ctr = 0; |
417 | my %node_hash; |
b15511bf |
418 | # Add our readings to the graph |
b5054ca9 |
419 | foreach my $n ( sort { $a->name cmp $b->name } $self->readings ) { |
910a0a6d |
420 | my $node_el = $graph->addNewChild( $graphml_ns, 'node' ); |
421 | my $node_xmlid = 'n' . $node_ctr++; |
422 | $node_hash{ $n->name } = $node_xmlid; |
423 | $node_el->setAttribute( 'id', $node_xmlid ); |
424 | _add_graphml_data( $node_el, $node_data_keys{'name'}, $n->name ); |
425 | _add_graphml_data( $node_el, $node_data_keys{'reading'}, $n->label ); |
d9e873d0 |
426 | _add_graphml_data( $node_el, $node_data_keys{'rank'}, $n->rank ) |
427 | if $n->has_rank; |
910a0a6d |
428 | _add_graphml_data( $node_el, $node_data_keys{'class'}, $n->sub_class ); |
429 | _add_graphml_data( $node_el, $node_data_keys{'identical'}, $n->primary->name ) |
94c00c71 |
430 | if $n->has_primary && $n->primary ne $n; |
b15511bf |
431 | } |
432 | |
94c00c71 |
433 | # Add the path and relationship edges |
df6d9812 |
434 | my $edge_ctr = 0; |
ef9d481f |
435 | foreach my $e ( sort { $a->from->name cmp $b->from->name } $self->graph->edges() ) { |
910a0a6d |
436 | my( $name, $from, $to ) = ( 'e'.$edge_ctr++, |
437 | $node_hash{ $e->from->name() }, |
438 | $node_hash{ $e->to->name() } ); |
439 | my $edge_el = $graph->addNewChild( $graphml_ns, 'edge' ); |
440 | $edge_el->setAttribute( 'source', $from ); |
441 | $edge_el->setAttribute( 'target', $to ); |
442 | $edge_el->setAttribute( 'id', $name ); |
443 | # Add the edge class |
444 | _add_graphml_data( $edge_el, $edge_data_keys{'class'}, $e->sub_class ); |
94c00c71 |
445 | |
446 | # For some classes we have extra information to save. |
910a0a6d |
447 | if( $e->sub_class eq 'path' ) { |
448 | # It's a witness path, so add the witness |
449 | my $base = $e->label; |
450 | my $key = $edge_data_keys{'witness_main'}; |
94c00c71 |
451 | # Is this an ante-corr witness? |
452 | my $aclabel = $self->ac_label; |
453 | if( $e->label =~ /^(.*)\Q$aclabel\E$/ ) { |
454 | # Keep the base witness |
910a0a6d |
455 | $base = $1; |
94c00c71 |
456 | # ...and record that this is an 'extra' reading path |
457 | _add_graphml_data( $edge_el, $edge_data_keys{'extra'}, 'true' ); |
910a0a6d |
458 | } |
94c00c71 |
459 | _add_graphml_data( $edge_el, $edge_data_keys{'witness'}, $base ); |
910a0a6d |
460 | } elsif( $e->sub_class eq 'relationship' ) { |
c9bf3dbf |
461 | # It's a relationship, so save the relationship data |
910a0a6d |
462 | _add_graphml_data( $edge_el, $edge_data_keys{'relationship'}, $e->label ); |
c9bf3dbf |
463 | _add_graphml_data( $edge_el, $edge_data_keys{'equal_rank'}, $e->equal_rank ); |
464 | _add_graphml_data( $edge_el, $edge_data_keys{'non_correctable'}, $e->non_correctable ); |
465 | _add_graphml_data( $edge_el, $edge_data_keys{'non_independent'}, $e->non_independent ); |
94c00c71 |
466 | } |
8e1394aa |
467 | } |
468 | |
94c00c71 |
469 | # Save and return the thing |
470 | my $result = decode_utf8( $graphml->toString(1) ); |
471 | $self->_save_graphml( $result ); |
472 | return $result; |
df6d9812 |
473 | } |
474 | |
b15511bf |
475 | sub _add_graphml_data { |
476 | my( $el, $key, $value ) = @_; |
b15511bf |
477 | return unless defined $value; |
c9bf3dbf |
478 | my $data_el = $el->addNewChild( $el->namespaceURI, 'data' ); |
b15511bf |
479 | $data_el->setAttribute( 'key', $key ); |
480 | $data_el->appendText( $value ); |
8e1394aa |
481 | } |
482 | |
910a0a6d |
483 | =item B<as_csv> |
484 | |
485 | print $graph->as_csv( $recalculate ) |
486 | |
487 | Returns a CSV alignment table representation of the collation graph, one |
488 | row per witness (or witness uncorrected.) Unless $recalculate is passed |
489 | (and is a true value), the method will return a cached copy of the CSV |
490 | after the first call to the method. |
491 | |
492 | =cut |
493 | |
494 | sub as_csv { |
495 | my( $self, $recalc ) = @_; |
496 | return $self->csv if $self->has_csv; |
497 | my $table = $self->make_alignment_table; |
498 | my $csv = Text::CSV_XS->new( { binary => 1, quote_null => 0 } ); |
499 | my @result; |
500 | foreach my $row ( @$table ) { |
501 | $csv->combine( @$row ); |
502 | push( @result, decode_utf8( $csv->string ) ); |
503 | } |
504 | $self->_save_csv( join( "\n", @result ) ); |
505 | return $self->csv; |
506 | } |
507 | |
0e476982 |
508 | # Make an alignment table - $noderefs controls whether the objects |
509 | # in the table are the nodes or simply their readings. |
9f3ba6f7 |
510 | |
910a0a6d |
511 | sub make_alignment_table { |
0e476982 |
512 | my( $self, $noderefs ) = @_; |
910a0a6d |
513 | unless( $self->linear ) { |
514 | warn "Need a linear graph in order to make an alignment table"; |
515 | return; |
516 | } |
517 | my $table; |
518 | my @all_pos = sort { $a <=> $b } $self->possible_positions; |
519 | foreach my $wit ( $self->tradition->witnesses ) { |
eca16057 |
520 | # print STDERR "Making witness row(s) for " . $wit->sigil . "\n"; |
0e476982 |
521 | my @row = _make_witness_row( $wit->path, \@all_pos, $noderefs ); |
910a0a6d |
522 | unshift( @row, $wit->sigil ); |
523 | push( @$table, \@row ); |
524 | if( $wit->has_ante_corr ) { |
0e476982 |
525 | my @ac_row = _make_witness_row( $wit->uncorrected_path, \@all_pos, $noderefs ); |
910a0a6d |
526 | unshift( @ac_row, $wit->sigil . $self->ac_label ); |
527 | push( @$table, \@ac_row ); |
528 | } |
529 | } |
0e476982 |
530 | |
910a0a6d |
531 | # Return a table where the witnesses read in columns rather than rows. |
532 | my $turned = _turn_table( $table ); |
533 | return $turned; |
534 | } |
535 | |
536 | sub _make_witness_row { |
0e476982 |
537 | my( $path, $positions, $noderefs ) = @_; |
910a0a6d |
538 | my %char_hash; |
539 | map { $char_hash{$_} = undef } @$positions; |
540 | foreach my $rdg ( @$path ) { |
eca16057 |
541 | my $rtext = $rdg->text; |
542 | $rtext = '#LACUNA#' if $rdg->is_lacuna; |
0e476982 |
543 | $char_hash{$rdg->rank} = $noderefs ? $rdg : $rtext; |
910a0a6d |
544 | } |
545 | my @row = map { $char_hash{$_} } @$positions; |
eca16057 |
546 | # Fill in lacuna markers for undef spots in the row |
547 | my $last_el = shift @row; |
548 | my @filled_row = ( $last_el ); |
549 | foreach my $el ( @row ) { |
0e476982 |
550 | # If we are using node reference, make the lacuna node appear many times |
551 | # in the table. If not, use the lacuna tag. |
552 | if( $last_el && _el_is_lacuna( $last_el ) && !defined $el ) { |
553 | $el = $noderefs ? $last_el : '#LACUNA#'; |
eca16057 |
554 | } |
555 | push( @filled_row, $el ); |
556 | $last_el = $el; |
557 | } |
558 | return @filled_row; |
910a0a6d |
559 | } |
560 | |
0e476982 |
561 | # Tiny utility function to say if a table element is a lacuna |
562 | sub _el_is_lacuna { |
563 | my $el = shift; |
564 | return 1 if $el eq '#LACUNA#'; |
565 | return 1 if ref( $el ) eq 'Text::Tradition::Collation::Reading' |
566 | && $el->is_lacuna; |
567 | return 0; |
568 | } |
569 | |
910a0a6d |
570 | # Helper to turn the witnesses along columns rather than rows. Assumes |
571 | # equal-sized rows. |
572 | sub _turn_table { |
573 | my( $table ) = @_; |
574 | my $result = []; |
575 | return $result unless scalar @$table; |
576 | my $nrows = scalar @{$table->[0]}; |
577 | foreach my $idx ( 0 .. $nrows - 1 ) { |
578 | foreach my $wit ( 0 .. $#{$table} ) { |
579 | $result->[$idx]->[$wit] = $table->[$wit]->[$idx]; |
580 | } |
581 | } |
582 | return $result; |
583 | } |
584 | |
585 | |
3265b0ce |
586 | sub collapse_graph_paths { |
1f563ac3 |
587 | my $self = shift; |
3265b0ce |
588 | # Our collation graph has an path per witness. This is great for |
1f563ac3 |
589 | # calculation purposes, but terrible for display. Thus we want to |
3265b0ce |
590 | # display only one path between any two nodes. |
1f563ac3 |
591 | |
592 | return if $self->collapsed; |
593 | |
3265b0ce |
594 | print STDERR "Collapsing witness paths in graph...\n"; |
1f563ac3 |
595 | |
596 | # Don't list out every witness if we have more than half to list. |
910a0a6d |
597 | my $majority = int( scalar( $self->tradition->witnesses ) / 2 ) + 1; |
a0093bf2 |
598 | # But don't compress if there are only a few witnesses. |
599 | $majority = 4 if $majority < 4; |
b15511bf |
600 | foreach my $node ( $self->readings ) { |
910a0a6d |
601 | my $newlabels = {}; |
602 | # We will visit each node, so we only look ahead. |
603 | foreach my $edge ( $node->outgoing() ) { |
604 | next unless $edge->class eq 'edge.path'; |
605 | add_hash_entry( $newlabels, $edge->to->name, $edge->name ); |
606 | $self->del_path( $edge ); |
607 | } |
608 | |
609 | foreach my $newdest ( keys %$newlabels ) { |
610 | my $label; |
611 | my @compressed_wits = @{$newlabels->{$newdest}}; |
612 | if( @compressed_wits < $majority ) { |
613 | $label = join( ', ', sort( @{$newlabels->{$newdest}} ) ); |
614 | } else { |
615 | ## TODO FIX THIS HACK |
616 | my @aclabels; |
617 | foreach my $wit ( @compressed_wits ) { |
618 | push( @aclabels, $wit ) if( $wit =~ /^(.*?)(\s*\(?a\.\s*c\.\)?)$/ ); |
619 | } |
620 | $label = join( ', ', 'majority', sort( @aclabels ) ); |
621 | } |
622 | |
623 | my $newpath = $self->add_path( $node, $self->reading( $newdest ), $label ); |
624 | $newpath->hidden_witnesses( \@compressed_wits ); |
625 | } |
1f563ac3 |
626 | } |
627 | |
628 | $self->collapsed( 1 ); |
629 | } |
630 | |
3265b0ce |
631 | sub expand_graph_paths { |
1f563ac3 |
632 | my $self = shift; |
3265b0ce |
633 | # Our collation graph has only one path between any two nodes. |
1f563ac3 |
634 | # This is great for display, but not so great for analysis. |
3265b0ce |
635 | # Expand this so that each witness has its own path between any |
1f563ac3 |
636 | # two reading nodes. |
637 | return unless $self->collapsed; |
638 | |
3265b0ce |
639 | print STDERR "Expanding witness paths in graph...\n"; |
3265b0ce |
640 | foreach my $path( $self->paths ) { |
910a0a6d |
641 | my $from = $path->from; |
642 | my $to = $path->to; |
643 | warn sprintf( "No hidden witnesses on %s -> %s ?", $from->name, $to->name ) |
644 | unless $path->has_hidden_witnesses; |
645 | my @wits = @{$path->hidden_witnesses}; |
646 | $self->del_path( $path ); |
647 | foreach ( @wits ) { |
648 | $self->add_path( $from, $to, $_ ); |
649 | } |
1f563ac3 |
650 | } |
651 | $self->collapsed( 0 ); |
652 | } |
653 | |
8e1394aa |
654 | =back |
655 | |
de51424a |
656 | =head2 Navigation methods |
657 | |
658 | =over |
659 | |
8e1394aa |
660 | =item B<start> |
661 | |
662 | my $beginning = $collation->start(); |
663 | |
664 | Returns the beginning of the collation, a meta-reading with label '#START#'. |
665 | |
666 | =cut |
667 | |
668 | sub start { |
4a8828f0 |
669 | # Return the beginning reading of the graph. |
94c00c71 |
670 | my( $self, $new_start ) = @_; |
671 | my $start = $self->reading( '#START#' ); |
672 | if( ref( $new_start ) eq 'Text::Tradition::Collation::Reading' ) { |
673 | # Replace the existing start node. |
910a0a6d |
674 | $self->del_reading( '#START#' ); |
675 | $self->graph->rename_node( $new_start, '#START#' ); |
94c00c71 |
676 | $start = $new_start; |
677 | } elsif ( $new_start && $new_start eq 'INIT' ) { |
678 | # Make a new start node. |
679 | $start = $self->add_reading( '#START#' ); |
910a0a6d |
680 | } |
94c00c71 |
681 | # Make sure the start node is a meta node |
682 | $start->is_meta( 1 ); |
910a0a6d |
683 | # Make sure the start node has a start position. |
94c00c71 |
684 | unless( $start->has_rank ) { |
685 | $start->rank( '0' ); |
8e1394aa |
686 | } |
94c00c71 |
687 | return $start; |
8e1394aa |
688 | } |
689 | |
910a0a6d |
690 | =item B<end> |
691 | |
692 | my $end = $collation->end(); |
693 | |
694 | Returns the end of the collation, a meta-reading with label '#END#'. |
695 | |
696 | =cut |
697 | |
698 | sub end { |
699 | my $self = shift; |
700 | my( $new_end ) = @_; |
94c00c71 |
701 | my $end = $self->reading( '#END#' ); |
702 | if( ref( $new_end ) eq 'Text::Tradition::Collation::Reading' ) { |
910a0a6d |
703 | $self->del_reading( '#END#' ); |
704 | $self->graph->rename_node( $new_end, '#END#' ); |
94c00c71 |
705 | $end = $new_end |
706 | } elsif ( $new_end && $new_end eq 'INIT' ) { |
707 | # Make a new start node. |
708 | $end = $self->add_reading( '#END#' ); |
910a0a6d |
709 | } |
94c00c71 |
710 | # Make sure the start node is a meta node |
711 | $end->is_meta( 1 ); |
712 | return $end; |
910a0a6d |
713 | } |
714 | |
e2902068 |
715 | =item B<reading_sequence> |
716 | |
717 | my @readings = $graph->reading_sequence( $first, $last, $path[, $alt_path] ); |
718 | |
719 | Returns the ordered list of readings, starting with $first and ending |
720 | with $last, along the given witness path. If no path is specified, |
721 | assume that the path is that of the base text (if any.) |
722 | |
723 | =cut |
724 | |
910a0a6d |
725 | # TODO Think about returning some lazy-eval iterator. |
726 | |
e2902068 |
727 | sub reading_sequence { |
728 | my( $self, $start, $end, $witness, $backup ) = @_; |
729 | |
930ff666 |
730 | $witness = $self->baselabel unless $witness; |
e2902068 |
731 | my @readings = ( $start ); |
732 | my %seen; |
733 | my $n = $start; |
930ff666 |
734 | while( $n && $n ne $end ) { |
910a0a6d |
735 | if( exists( $seen{$n->name()} ) ) { |
736 | warn "Detected loop at " . $n->name(); |
737 | last; |
738 | } |
739 | $seen{$n->name()} = 1; |
740 | |
741 | my $next = $self->next_reading( $n, $witness, $backup ); |
742 | warn "Did not find any path for $witness from reading " . $n->name |
743 | unless $next; |
744 | push( @readings, $next ); |
745 | $n = $next; |
e2902068 |
746 | } |
747 | # Check that the last reading is our end reading. |
748 | my $last = $readings[$#readings]; |
749 | warn "Last reading found from " . $start->label() . |
910a0a6d |
750 | " for witness $witness is not the end!" |
751 | unless $last eq $end; |
e2902068 |
752 | |
753 | return @readings; |
754 | } |
755 | |
4a8828f0 |
756 | =item B<next_reading> |
8e1394aa |
757 | |
4a8828f0 |
758 | my $next_reading = $graph->next_reading( $reading, $witpath ); |
8e1394aa |
759 | |
4a8828f0 |
760 | Returns the reading that follows the given reading along the given witness |
930ff666 |
761 | path. |
8e1394aa |
762 | |
763 | =cut |
764 | |
4a8828f0 |
765 | sub next_reading { |
e2902068 |
766 | # Return the successor via the corresponding path. |
8e1394aa |
767 | my $self = shift; |
4a8828f0 |
768 | return $self->_find_linked_reading( 'next', @_ ); |
8e1394aa |
769 | } |
770 | |
4a8828f0 |
771 | =item B<prior_reading> |
8e1394aa |
772 | |
4a8828f0 |
773 | my $prior_reading = $graph->prior_reading( $reading, $witpath ); |
8e1394aa |
774 | |
4a8828f0 |
775 | Returns the reading that precedes the given reading along the given witness |
930ff666 |
776 | path. |
8e1394aa |
777 | |
778 | =cut |
779 | |
4a8828f0 |
780 | sub prior_reading { |
e2902068 |
781 | # Return the predecessor via the corresponding path. |
8e1394aa |
782 | my $self = shift; |
4a8828f0 |
783 | return $self->_find_linked_reading( 'prior', @_ ); |
8e1394aa |
784 | } |
785 | |
4a8828f0 |
786 | sub _find_linked_reading { |
e2902068 |
787 | my( $self, $direction, $node, $path, $alt_path ) = @_; |
788 | my @linked_paths = $direction eq 'next' |
910a0a6d |
789 | ? $node->outgoing() : $node->incoming(); |
e2902068 |
790 | return undef unless scalar( @linked_paths ); |
8e1394aa |
791 | |
e2902068 |
792 | # We have to find the linked path that contains all of the |
793 | # witnesses supplied in $path. |
794 | my( @path_wits, @alt_path_wits ); |
795 | @path_wits = $self->witnesses_of_label( $path ) if $path; |
796 | @alt_path_wits = $self->witnesses_of_label( $alt_path ) if $alt_path; |
797 | my $base_le; |
798 | my $alt_le; |
799 | foreach my $le ( @linked_paths ) { |
910a0a6d |
800 | if( $le->name eq $self->baselabel ) { |
801 | $base_le = $le; |
802 | } else { |
803 | my @le_wits = $self->witnesses_of_label( $le->name ); |
804 | if( _is_within( \@path_wits, \@le_wits ) ) { |
805 | # This is the right path. |
806 | return $direction eq 'next' ? $le->to() : $le->from(); |
807 | } elsif( _is_within( \@alt_path_wits, \@le_wits ) ) { |
808 | $alt_le = $le; |
809 | } |
810 | } |
8e1394aa |
811 | } |
e2902068 |
812 | # Got this far? Return the alternate path if it exists. |
813 | return $direction eq 'next' ? $alt_le->to() : $alt_le->from() |
910a0a6d |
814 | if $alt_le; |
e2902068 |
815 | |
816 | # Got this far? Return the base path if it exists. |
817 | return $direction eq 'next' ? $base_le->to() : $base_le->from() |
910a0a6d |
818 | if $base_le; |
e2902068 |
819 | |
820 | # Got this far? We have no appropriate path. |
8e1394aa |
821 | warn "Could not find $direction node from " . $node->label |
910a0a6d |
822 | . " along path $path"; |
8e1394aa |
823 | return undef; |
824 | } |
825 | |
4a8828f0 |
826 | # Some set logic. |
827 | sub _is_within { |
828 | my( $set1, $set2 ) = @_; |
7854e12e |
829 | my $ret = @$set1; # will be 0, i.e. false, if set1 is empty |
4a8828f0 |
830 | foreach my $el ( @$set1 ) { |
910a0a6d |
831 | $ret = 0 unless grep { /^\Q$el\E$/ } @$set2; |
4a8828f0 |
832 | } |
833 | return $ret; |
834 | } |
835 | |
de51424a |
836 | |
837 | ## INITIALIZATION METHODS - for use by parsers |
4a8828f0 |
838 | # Walk the paths for each witness in the graph, and return the nodes |
e2902068 |
839 | # that the graph has in common. If $using_base is true, some |
840 | # different logic is needed. |
7e450e44 |
841 | # NOTE This does not create paths; it merely finds common readings. |
4a8828f0 |
842 | |
843 | sub walk_witness_paths { |
7e450e44 |
844 | my( $self ) = @_; |
4a8828f0 |
845 | # For each witness, walk the path through the graph. |
846 | # Then we need to find the common nodes. |
847 | # TODO This method is going to fall down if we have a very gappy |
848 | # text in the collation. |
849 | my $paths = {}; |
3a1f2523 |
850 | my @common_readings; |
910a0a6d |
851 | foreach my $wit ( $self->tradition->witnesses ) { |
852 | my $curr_reading = $self->start; |
7e450e44 |
853 | my @wit_path = $self->reading_sequence( $self->start, $self->end, |
910a0a6d |
854 | $wit->sigil ); |
855 | $wit->path( \@wit_path ); |
856 | |
857 | # Detect the common readings. |
858 | @common_readings = _find_common( \@common_readings, \@wit_path ); |
4a8828f0 |
859 | } |
860 | |
861 | # Mark all the nodes as either common or not. |
3a1f2523 |
862 | foreach my $cn ( @common_readings ) { |
910a0a6d |
863 | print STDERR "Setting " . $cn->name . " / " . $cn->label |
864 | . " as common node\n"; |
865 | $cn->make_common; |
4a8828f0 |
866 | } |
867 | foreach my $n ( $self->readings() ) { |
910a0a6d |
868 | $n->make_variant unless $n->is_common; |
4a8828f0 |
869 | } |
3a1f2523 |
870 | # Return an array of the common nodes in order. |
871 | return @common_readings; |
4a8828f0 |
872 | } |
873 | |
930ff666 |
874 | sub _find_common { |
875 | my( $common_readings, $new_path ) = @_; |
876 | my @cr; |
877 | if( @$common_readings ) { |
910a0a6d |
878 | foreach my $n ( @$new_path ) { |
879 | push( @cr, $n ) if grep { $_ eq $n } @$common_readings; |
880 | } |
930ff666 |
881 | } else { |
910a0a6d |
882 | push( @cr, @$new_path ); |
930ff666 |
883 | } |
884 | return @cr; |
885 | } |
886 | |
887 | sub _remove_common { |
888 | my( $common_readings, $divergence ) = @_; |
889 | my @cr; |
890 | my %diverged; |
891 | map { $diverged{$_->name} = 1 } @$divergence; |
892 | foreach( @$common_readings ) { |
910a0a6d |
893 | push( @cr, $_ ) unless $diverged{$_->name}; |
930ff666 |
894 | } |
895 | return @cr; |
896 | } |
897 | |
898 | |
7e450e44 |
899 | # For use when a collation is constructed from a base text and an apparatus. |
900 | # We have the sequences of readings and just need to add path edges. |
e2902068 |
901 | |
6a222840 |
902 | sub make_witness_paths { |
903 | my( $self ) = @_; |
910a0a6d |
904 | foreach my $wit ( $self->tradition->witnesses ) { |
905 | print STDERR "Making path for " . $wit->sigil . "\n"; |
906 | $self->make_witness_path( $wit ); |
7854e12e |
907 | } |
7854e12e |
908 | } |
909 | |
6a222840 |
910 | sub make_witness_path { |
7854e12e |
911 | my( $self, $wit ) = @_; |
912 | my @chain = @{$wit->path}; |
15d2d3df |
913 | my $sig = $wit->sigil; |
7854e12e |
914 | foreach my $idx ( 0 .. $#chain-1 ) { |
910a0a6d |
915 | $self->add_path( $chain[$idx], $chain[$idx+1], $sig ); |
7854e12e |
916 | } |
d9e873d0 |
917 | if( $wit->has_ante_corr ) { |
918 | @chain = @{$wit->uncorrected_path}; |
919 | foreach my $idx( 0 .. $#chain-1 ) { |
920 | my $source = $chain[$idx]; |
921 | my $target = $chain[$idx+1]; |
922 | $self->add_path( $source, $target, $sig.$self->ac_label ) |
923 | unless $self->has_path( $source, $target, $sig ); |
924 | } |
15d2d3df |
925 | } |
e2902068 |
926 | } |
927 | |
910a0a6d |
928 | sub calculate_ranks { |
929 | my $self = shift; |
930 | # Walk a version of the graph where every node linked by a relationship |
931 | # edge is fundamentally the same node, and do a topological ranking on |
932 | # the nodes in this graph. |
c9bf3dbf |
933 | my $topo_graph = Graph->new(); |
910a0a6d |
934 | my %rel_containers; |
935 | my $rel_ctr = 0; |
936 | # Add the nodes |
937 | foreach my $r ( $self->readings ) { |
938 | next if exists $rel_containers{$r->name}; |
939 | my @rels = $r->related_readings( 'colocated' ); |
940 | if( @rels ) { |
941 | # Make a relationship container. |
942 | push( @rels, $r ); |
c9bf3dbf |
943 | my $rn = 'rel_container_' . $rel_ctr++; |
944 | $topo_graph->add_vertex( $rn ); |
910a0a6d |
945 | foreach( @rels ) { |
946 | $rel_containers{$_->name} = $rn; |
947 | } |
948 | } else { |
949 | # Add a new node to mirror the old node. |
c9bf3dbf |
950 | $rel_containers{$r->name} = $r->name; |
951 | $topo_graph->add_vertex( $r->name ); |
910a0a6d |
952 | } |
4a8828f0 |
953 | } |
3a1f2523 |
954 | |
910a0a6d |
955 | # Add the edges. Need only one edge between any pair of nodes. |
956 | foreach my $r ( $self->readings ) { |
957 | foreach my $n ( $r->neighbor_readings( 'forward' ) ) { |
c9bf3dbf |
958 | my( $tfrom, $tto ) = ( $rel_containers{$r->name}, |
959 | $rel_containers{$n->name} ); |
960 | $topo_graph->add_edge( $tfrom, $tto ) |
961 | unless $topo_graph->has_edge( $tfrom, $tto ); |
910a0a6d |
962 | } |
963 | } |
964 | |
965 | # Now do the rankings, starting with the start node. |
966 | my $topo_start = $rel_containers{$self->start->name}; |
c9bf3dbf |
967 | my $node_ranks = { $topo_start => 0 }; |
910a0a6d |
968 | my @curr_origin = ( $topo_start ); |
969 | # A little iterative function. |
970 | while( @curr_origin ) { |
c9bf3dbf |
971 | @curr_origin = _assign_rank( $topo_graph, $node_ranks, @curr_origin ); |
910a0a6d |
972 | } |
973 | # Transfer our rankings from the topological graph to the real one. |
974 | foreach my $r ( $self->readings ) { |
c9bf3dbf |
975 | $r->rank( $node_ranks->{$rel_containers{$r->name}} ); |
de51424a |
976 | } |
8e1394aa |
977 | } |
3a1f2523 |
978 | |
910a0a6d |
979 | sub _assign_rank { |
c9bf3dbf |
980 | my( $graph, $node_ranks, @current_nodes ) = @_; |
910a0a6d |
981 | # Look at each of the children of @current_nodes. If all the child's |
982 | # parents have a rank, assign it the highest rank + 1 and add it to |
c9bf3dbf |
983 | # @next_nodes. Otherwise skip it; we will return when the highest-ranked |
984 | # parent gets a rank. |
910a0a6d |
985 | my @next_nodes; |
986 | foreach my $c ( @current_nodes ) { |
c9bf3dbf |
987 | warn "Current reading $c has no rank!" |
988 | unless exists $node_ranks->{$c}; |
989 | # print STDERR "Looking at child of node $c, rank " |
990 | # . $node_ranks->{$c} . "\n"; |
991 | foreach my $child ( $graph->successors( $c ) ) { |
992 | next if exists $node_ranks->{$child}; |
910a0a6d |
993 | my $highest_rank = -1; |
994 | my $skip = 0; |
c9bf3dbf |
995 | foreach my $parent ( $graph->predecessors( $child ) ) { |
996 | if( exists $node_ranks->{$parent} ) { |
997 | $highest_rank = $node_ranks->{$parent} |
998 | if $highest_rank <= $node_ranks->{$parent}; |
910a0a6d |
999 | } else { |
1000 | $skip = 1; |
1001 | last; |
1002 | } |
1003 | } |
1004 | next if $skip; |
c9bf3dbf |
1005 | my $c_rank = $highest_rank + 1; |
1006 | # print STDERR "Assigning rank $c_rank to node $child \n"; |
1007 | $node_ranks->{$child} = $c_rank; |
910a0a6d |
1008 | push( @next_nodes, $child ); |
1009 | } |
1010 | } |
1011 | return @next_nodes; |
4cdd82f1 |
1012 | } |
910a0a6d |
1013 | |
0e476982 |
1014 | # Another method to make up for rough collation methods. If the same reading |
1015 | # appears multiple times at the same rank, collapse the nodes. |
1016 | sub flatten_ranks { |
1017 | my $self = shift; |
1018 | my %unique_rank_rdg; |
1019 | foreach my $rdg ( $self->readings ) { |
1020 | next unless $rdg->has_rank; |
1021 | my $key = $rdg->rank . "||" . $rdg->text; |
1022 | if( exists $unique_rank_rdg{$key} ) { |
1023 | # Combine! |
1024 | print STDERR "Combining readings at same rank: $key\n"; |
1025 | $self->merge_readings( $unique_rank_rdg{$key}, $rdg ); |
1026 | } else { |
1027 | $unique_rank_rdg{$key} = $rdg; |
1028 | } |
1029 | } |
1030 | } |
1031 | |
1032 | |
4cdd82f1 |
1033 | sub possible_positions { |
3a1f2523 |
1034 | my $self = shift; |
910a0a6d |
1035 | my %all_pos; |
1036 | map { $all_pos{ $_->rank } = 1 } $self->readings; |
1037 | return keys %all_pos; |
3a1f2523 |
1038 | } |
1039 | |
4cdd82f1 |
1040 | # TODO think about indexing this. |
3a1f2523 |
1041 | sub readings_at_position { |
4cdd82f1 |
1042 | my( $self, $position, $strict ) = @_; |
4cdd82f1 |
1043 | my @answer; |
1044 | foreach my $r ( $self->readings ) { |
910a0a6d |
1045 | push( @answer, $r ) if $r->is_at_position( $position, $strict ); |
4cdd82f1 |
1046 | } |
3a1f2523 |
1047 | return @answer; |
1048 | } |
1049 | |
1050 | ## Lemmatizer functions |
1051 | |
1052 | sub init_lemmata { |
1053 | my $self = shift; |
4cdd82f1 |
1054 | |
1055 | foreach my $position ( $self->possible_positions ) { |
910a0a6d |
1056 | $self->lemmata->{$position} = undef; |
3a1f2523 |
1057 | } |
1058 | |
1059 | foreach my $cr ( $self->common_readings ) { |
910a0a6d |
1060 | $self->lemmata->{$cr->position->maxref} = $cr->name; |
3a1f2523 |
1061 | } |
1062 | } |
7e450e44 |
1063 | |
1064 | sub common_readings { |
1065 | my $self = shift; |
1066 | my @common = grep { $_->is_common } $self->readings(); |
d9e873d0 |
1067 | return sort { $a->rank <=> $b->rank } @common; |
7e450e44 |
1068 | } |
3a1f2523 |
1069 | |
1070 | =item B<lemma_readings> |
1071 | |
1072 | my @state = $graph->lemma_readings( @readings_delemmatized ); |
1073 | |
1074 | Takes a list of readings that have just been delemmatized, and returns |
1075 | a set of tuples of the form ['reading', 'state'] that indicates what |
1076 | changes need to be made to the graph. |
1077 | |
1078 | =over |
1079 | |
1080 | =item * |
1081 | |
1082 | A state of 1 means 'lemmatize this reading' |
1083 | |
1084 | =item * |
1085 | |
1086 | A state of 0 means 'delemmatize this reading' |
1087 | |
1088 | =item * |
1089 | |
1090 | A state of undef means 'an ellipsis belongs in the text here because |
1091 | no decision has been made / an earlier decision was backed out' |
1092 | |
1093 | =back |
1094 | |
1095 | =cut |
1096 | |
1097 | sub lemma_readings { |
1098 | my( $self, @toggled_off_nodes ) = @_; |
1099 | |
1100 | # First get the positions of those nodes which have been |
1101 | # toggled off. |
1102 | my $positions_off = {}; |
4cdd82f1 |
1103 | map { $positions_off->{ $_->position->reference } = $_->name } |
1104 | @toggled_off_nodes; |
de51424a |
1105 | |
3a1f2523 |
1106 | # Now for each position, we have to see if a node is on, and we |
4cdd82f1 |
1107 | # have to see if a node has been turned off. The lemmata hash |
1108 | # should contain fixed positions, range positions whose node was |
1109 | # just turned off, and range positions whose node is on. |
3a1f2523 |
1110 | my @answer; |
4cdd82f1 |
1111 | my %fixed_positions; |
1112 | # TODO One of these is probably redundant. |
1113 | map { $fixed_positions{$_} = 0 } keys %{$self->lemmata}; |
1114 | map { $fixed_positions{$_} = 0 } keys %{$positions_off}; |
1115 | map { $fixed_positions{$_} = 1 } $self->possible_positions; |
1116 | foreach my $pos ( sort { Text::Tradition::Collation::Position::str_cmp( $a, $b ) } keys %fixed_positions ) { |
910a0a6d |
1117 | # Find the state of this position. If there is an active node, |
1118 | # its name will be the state; otherwise the state will be 0 |
1119 | # (nothing at this position) or undef (ellipsis at this position) |
1120 | my $active = undef; |
1121 | $active = $self->lemmata->{$pos} if exists $self->lemmata->{$pos}; |
1122 | |
1123 | # Is there a formerly active node that was toggled off? |
1124 | if( exists( $positions_off->{$pos} ) ) { |
1125 | my $off_node = $positions_off->{$pos}; |
1126 | if( $active && $active ne $off_node) { |
1127 | push( @answer, [ $off_node, 0 ], [ $active, 1 ] ); |
1128 | } else { |
1129 | unless( $fixed_positions{$pos} ) { |
1130 | $active = 0; |
1131 | delete $self->lemmata->{$pos}; |
1132 | } |
1133 | push( @answer, [ $off_node, $active ] ); |
1134 | } |
1135 | |
1136 | # No formerly active node, so we just see if there is a currently |
1137 | # active one. |
1138 | } elsif( $active ) { |
1139 | # Push the active node, whatever it is. |
1140 | push( @answer, [ $active, 1 ] ); |
1141 | } else { |
1142 | # Push the state that is there. Arbitrarily use the first node |
1143 | # at that position. |
1144 | my @pos_nodes = $self->readings_at_position( $pos ); |
1145 | push( @answer, [ $pos_nodes[0]->name, $self->lemmata->{$pos} ] ); |
1146 | delete $self->lemmata->{$pos} unless $fixed_positions{$pos}; |
1147 | } |
3a1f2523 |
1148 | } |
4cdd82f1 |
1149 | |
3a1f2523 |
1150 | return @answer; |
1151 | } |
1152 | |
de51424a |
1153 | =item B<toggle_reading> |
1154 | |
1155 | my @readings_delemmatized = $graph->toggle_reading( $reading_name ); |
1156 | |
1157 | Takes a reading node name, and either lemmatizes or de-lemmatizes |
1158 | it. Returns a list of all readings that are de-lemmatized as a result |
1159 | of the toggle. |
1160 | |
1161 | =cut |
1162 | |
1163 | sub toggle_reading { |
1164 | my( $self, $rname ) = @_; |
1165 | |
1166 | return unless $rname; |
1167 | my $reading = $self->reading( $rname ); |
1168 | if( !$reading || $reading->is_common() ) { |
910a0a6d |
1169 | # Do nothing, it's a common node. |
1170 | return; |
de51424a |
1171 | } |
1172 | |
1173 | my $pos = $reading->position; |
4cdd82f1 |
1174 | my $fixed = $reading->position->fixed; |
1175 | my $old_state = $self->lemmata->{$pos->reference}; |
1176 | |
de51424a |
1177 | my @readings_off; |
1178 | if( $old_state && $old_state eq $rname ) { |
910a0a6d |
1179 | # Turn off the node. We turn on no others by default. |
1180 | push( @readings_off, $reading ); |
de51424a |
1181 | } else { |
910a0a6d |
1182 | # Turn on the node. |
1183 | $self->lemmata->{$pos->reference} = $rname; |
1184 | # Any other 'on' readings in the same position should be off |
1185 | # if we have a fixed position. |
1186 | push( @readings_off, $self->same_position_as( $reading, 1 ) ) |
1187 | if $pos->fixed; |
1188 | # Any node that is an identical transposed one should be off. |
1189 | push( @readings_off, $reading->identical_readings ); |
de51424a |
1190 | } |
1191 | @readings_off = unique_list( @readings_off ); |
910a0a6d |
1192 | |
de51424a |
1193 | # Turn off the readings that need to be turned off. |
1194 | my @readings_delemmatized; |
1195 | foreach my $n ( @readings_off ) { |
910a0a6d |
1196 | my $npos = $n->position; |
1197 | my $state = undef; |
1198 | $state = $self->lemmata->{$npos->reference} |
1199 | if defined $self->lemmata->{$npos->reference}; |
1200 | if( $state && $state eq $n->name ) { |
1201 | # this reading is still on, so turn it off |
1202 | push( @readings_delemmatized, $n ); |
1203 | my $new_state = undef; |
1204 | if( $npos->fixed && $n eq $reading ) { |
1205 | # This is the reading that was clicked, so if there are no |
1206 | # other readings there and this is a fixed position, turn off |
1207 | # the position. In all other cases, restore the ellipsis. |
1208 | my @other_n = $self->same_position_as( $n ); # TODO do we need strict? |
1209 | $new_state = 0 unless @other_n; |
1210 | } |
1211 | $self->lemmata->{$npos->reference} = $new_state; |
1212 | } elsif( $old_state && $old_state eq $n->name ) { |
1213 | # another reading has already been turned on here |
1214 | push( @readings_delemmatized, $n ); |
1215 | } # else some other reading was on anyway, so pass. |
de51424a |
1216 | } |
1217 | return @readings_delemmatized; |
1218 | } |
1219 | |
1220 | sub same_position_as { |
4cdd82f1 |
1221 | my( $self, $reading, $strict ) = @_; |
de51424a |
1222 | my $pos = $reading->position; |
4cdd82f1 |
1223 | my %onpath = ( $reading->name => 1 ); |
1224 | # TODO This might not always be sufficient. We really want to |
1225 | # exclude all readings on this one's path between its two |
1226 | # common points. |
1227 | map { $onpath{$_->name} = 1 } $reading->neighbor_readings; |
1228 | my @same = grep { !$onpath{$_->name} } |
1229 | $self->readings_at_position( $reading->position, $strict ); |
de51424a |
1230 | return @same; |
1231 | } |
3a1f2523 |
1232 | |
4a8828f0 |
1233 | # Return the string that joins together a list of witnesses for |
1234 | # display on a single path. |
1235 | sub path_label { |
1236 | my $self = shift; |
1237 | return join( $self->wit_list_separator, @_ ); |
1238 | } |
1239 | |
1240 | sub witnesses_of_label { |
de51424a |
1241 | my( $self, $label ) = @_; |
4a8828f0 |
1242 | my $regex = $self->wit_list_separator; |
de51424a |
1243 | my @answer = split( /\Q$regex\E/, $label ); |
1244 | return @answer; |
4a8828f0 |
1245 | } |
8e1394aa |
1246 | |
de51424a |
1247 | sub unique_list { |
1248 | my( @list ) = @_; |
1249 | my %h; |
1250 | map { $h{$_->name} = $_ } @list; |
1251 | return values( %h ); |
1252 | } |
1253 | |
1f563ac3 |
1254 | sub add_hash_entry { |
1255 | my( $hash, $key, $entry ) = @_; |
1256 | if( exists $hash->{$key} ) { |
910a0a6d |
1257 | push( @{$hash->{$key}}, $entry ); |
1f563ac3 |
1258 | } else { |
910a0a6d |
1259 | $hash->{$key} = [ $entry ]; |
1f563ac3 |
1260 | } |
1261 | } |
1262 | |
dd3b58b0 |
1263 | no Moose; |
1264 | __PACKAGE__->meta->make_immutable; |