1 package stemmaweb::Controller::Stexaminer;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
7 use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
8 use Text::Tradition::Collation;
9 use Text::Tradition::Stemma;
11 BEGIN { extends 'Catalyst::Controller' }
16 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
20 The stemma analysis tool with the pretty colored table.
26 GET stexaminer/$textid
28 Renders the application for the text identified by $textid.
32 sub index :Path :Args(1) {
33 my( $self, $c, $textid ) = @_;
34 my $m = $c->model('Directory');
35 my $tradition = $m->tradition( $textid );
36 if( $tradition->stemma_count ) {
37 my $stemma = $tradition->stemma(0);
38 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
39 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
40 $c->stash->{text_title} = $tradition->name;
41 $c->stash->{template} = 'stexaminer.tt';
43 # Get the analysis options
44 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
45 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
46 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
47 $c->stash->{'show_type1'} = $use_type1;
48 $c->stash->{'ignore_variant'} = $ignore_sort;
49 # TODO Run the analysis as AJAX from the loaded page.
50 my %analysis_options = ( exclude_type1 => !$use_type1 );
51 if( $ignore_sort eq 'spelling' ) {
52 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
53 } elsif( $ignore_sort eq 'orthographic' ) {
54 $analysis_options{'merge_types'} = 'orthographic';
58 my $t = run_analysis( $tradition, %analysis_options );
59 # Stringify the reading groups
60 foreach my $loc ( @{$t->{'variants'}} ) {
61 my $mst = wit_stringify( $loc->{'missing'} );
62 $loc->{'missing'} = $mst;
63 foreach my $rhash ( @{$loc->{'readings'}} ) {
64 my $gst = wit_stringify( $rhash->{'group'} );
65 $rhash->{'group'} = $gst;
66 _stringify_element( $rhash, 'independent_occurrence' );
67 _stringify_element( $rhash, 'reversions' );
68 unless( $rhash->{'text'} ) {
69 $rhash->{'text'} = $rhash->{'readingid'};
73 # Values for TT rendering
74 $c->stash->{variants} = $t->{'variants'};
75 $c->stash->{total} = $t->{'variant_count'};
76 $c->stash->{genealogical} = $t->{'genealogical_count'};
77 $c->stash->{conflict} = $t->{'conflict_count'};
78 # Also make a JSON stash of the data for the statistics tables
79 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
81 $c->stash->{error} = 'Tradition ' . $tradition->name
82 . 'has no stemma for analysis.';
86 sub _stringify_element {
87 my( $hash, $key ) = @_;
88 return undef unless exists $hash->{$key};
89 if( ref( $hash->{$key} ) eq 'ARRAY' ) {
90 my $str = join( ', ', @{$hash->{$key}} );
97 POST stexaminer/graphsvg
98 dot: <stemmagraph dot string>
99 layerwits: [ <a.c. witnesses ]
101 Returns an SVG string of the given graph, extended to include the given
106 sub graphsvg :Local {
107 my( $self, $c ) = @_;
108 my $dot = $c->request->param('dot');
109 my @layerwits = $c->request->param('layerwits[]');
110 open my $stemma_fh, '<', \$dot;
111 binmode( $stemma_fh, ':encoding(UTF-8)' );
112 my $emptycoll = Text::Tradition::Collation->new();
113 my $tempstemma = Text::Tradition::Stemma->new(
114 collation => $emptycoll, 'dot' => $stemma_fh );
115 my $svgopts = { size => [ 600, 350 ] };
117 $svgopts->{'layerwits'} = \@layerwits;
119 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
120 $c->forward('View::SVG');
125 Attempt to render a view, if needed.
129 sub end : ActionClass('RenderView') {}
137 This library is free software. You can redistribute it and/or modify
138 it under the same terms as Perl itself.
142 __PACKAGE__->meta->make_immutable;