get rid of intermediate local analysis DB
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Stexaminer.pm
CommitLineData
5c9ecf66 1package stemmaweb::Controller::Stexaminer;
2376359f 2use Moose;
3use namespace::autoclean;
f8d13166 4use Encode qw/ decode_utf8 /;
2376359f 5use File::Temp;
6use JSON;
a44aaf2a 7use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
f8d13166 8use Text::Tradition::Collation;
9use Text::Tradition::Stemma;
2376359f 10
11BEGIN { extends 'Catalyst::Controller' }
12
13
14=head1 NAME
15
5c9ecf66 16stemmaweb::Controller::Stexaminer - Simple controller for stemma display
2376359f 17
18=head1 DESCRIPTION
19
20The stemma analysis tool with the pretty colored table.
21
22=head1 METHODS
23
f8d13166 24=head2 index
25
2376359f 26 GET stexaminer/$textid
27
28Renders the application for the text identified by $textid.
29
2376359f 30=cut
31
32sub index :Path :Args(1) {
33 my( $self, $c, $textid ) = @_;
34 my $m = $c->model('Directory');
35 my $tradition = $m->tradition( $textid );
1c0900ef 36 if( $tradition->stemma_count ) {
37 my $stemma = $tradition->stemma(0);
f8d13166 38 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
39 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
1c0900ef 40 $c->stash->{text_title} = $tradition->name;
41 $c->stash->{template} = 'stexaminer.tt';
e217c221 42
43 # Get the analysis options
44 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
cc79edd2 45 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
46 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
e217c221 47 $c->stash->{'show_type1'} = $use_type1;
48 $c->stash->{'ignore_variant'} = $ignore_sort;
1c0900ef 49 # TODO Run the analysis as AJAX from the loaded page.
cc79edd2 50 my %analysis_options = ( exclude_type1 => !$use_type1 );
e217c221 51 if( $ignore_sort eq 'spelling' ) {
7b7abf10 52 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
e217c221 53 } elsif( $ignore_sort eq 'orthographic' ) {
7b7abf10 54 $analysis_options{'merge_types'} = 'orthographic';
e217c221 55 }
cc79edd2 56
57 # Do the deed
e217c221 58 my $t = run_analysis( $tradition, %analysis_options );
a44aaf2a 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;
ee53ab0d 66 _stringify_element( $rhash, 'independent_occurrence' );
67 _stringify_element( $rhash, 'reversions' );
e41080b6 68 unless( $rhash->{'text'} ) {
69 $rhash->{'text'} = $rhash->{'readingid'};
70 }
a44aaf2a 71 }
72 }
23306161 73 # Values for TT rendering
1c0900ef 74 $c->stash->{variants} = $t->{'variants'};
75 $c->stash->{total} = $t->{'variant_count'};
76 $c->stash->{genealogical} = $t->{'genealogical_count'};
23306161 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'} );
1c0900ef 80 } else {
81 $c->stash->{error} = 'Tradition ' . $tradition->name
82 . 'has no stemma for analysis.';
83 }
2376359f 84}
85
ee53ab0d 86sub _stringify_element {
87 my( $hash, $key ) = @_;
b203d1c4 88 return undef unless exists $hash->{$key};
89 if( ref( $hash->{$key} ) eq 'ARRAY' ) {
90 my $str = join( ', ', @{$hash->{$key}} );
91 $hash->{$key} = $str;
92 }
ee53ab0d 93}
94
f8d13166 95=head2 graphsvg
96
97 POST stexaminer/graphsvg
98 dot: <stemmagraph dot string>
99 layerwits: [ <a.c. witnesses ]
100
101Returns an SVG string of the given graph, extended to include the given
102layered witnesses.
103
104=cut
105
106sub 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 ] };
116 if( @layerwits ) {
117 $svgopts->{'layerwits'} = \@layerwits;
118 }
119 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
120 $c->forward('View::SVG');
121}
122
2376359f 123=head2 end
124
125Attempt to render a view, if needed.
126
127=cut
128
129sub end : ActionClass('RenderView') {}
130
131=head1 AUTHOR
132
133Tara L Andrews
134
135=head1 LICENSE
136
137This library is free software. You can redistribute it and/or modify
138it under the same terms as Perl itself.
139
140=cut
141
142__PACKAGE__->meta->make_immutable;
143
1441;