Commit | Line | Data |
b8a92065 |
1 | package stemmaweb::Controller::Stexaminer; |
2 | use Moose; |
3 | use namespace::autoclean; |
be8bf746 |
4 | use Encode qw/ decode_utf8 /; |
b8a92065 |
5 | use File::Temp; |
6 | use JSON; |
0737e7dd |
7 | use Text::Tradition::Analysis qw/ run_analysis wit_stringify /; |
be8bf746 |
8 | use Text::Tradition::Collation; |
9 | use Text::Tradition::Stemma; |
b8a92065 |
10 | |
11 | BEGIN { extends 'Catalyst::Controller' } |
12 | |
13 | |
14 | =head1 NAME |
15 | |
16 | stemmaweb::Controller::Stexaminer - Simple controller for stemma display |
17 | |
18 | =head1 DESCRIPTION |
19 | |
20 | The stemma analysis tool with the pretty colored table. |
21 | |
22 | =head1 METHODS |
23 | |
be8bf746 |
24 | =head2 index |
25 | |
b8a92065 |
26 | GET stexaminer/$textid |
27 | |
28 | Renders the application for the text identified by $textid. |
29 | |
b8a92065 |
30 | =cut |
31 | |
32 | sub index :Path :Args(1) { |
33 | my( $self, $c, $textid ) = @_; |
34 | my $m = $c->model('Directory'); |
f7371955 |
35 | my $am = $c->model('Analysis'); |
b8a92065 |
36 | my $tradition = $m->tradition( $textid ); |
9c2e7b80 |
37 | if( $tradition->stemma_count ) { |
38 | my $stemma = $tradition->stemma(0); |
be8bf746 |
39 | $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } ); |
40 | $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' }); |
9c2e7b80 |
41 | $c->stash->{text_title} = $tradition->name; |
42 | $c->stash->{template} = 'stexaminer.tt'; |
0c236c96 |
43 | |
44 | # Get the analysis options |
45 | my( $use_type1, $ignore_sort ) = ( 0, 'none' ); |
46 | if( $c->req->method eq 'POST' ) { |
e816672a |
47 | $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0; |
0c236c96 |
48 | $ignore_sort = $c->req->param( 'ignore_variant' ); |
49 | } |
50 | $c->stash->{'show_type1'} = $use_type1; |
51 | $c->stash->{'ignore_variant'} = $ignore_sort; |
9c2e7b80 |
52 | # TODO Run the analysis as AJAX from the loaded page. |
f7371955 |
53 | my %analysis_options = ( calcdir => $am ); |
0c236c96 |
54 | $analysis_options{'exclude_type1'} = !$use_type1; |
55 | if( $ignore_sort eq 'spelling' ) { |
e816672a |
56 | $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ]; |
0c236c96 |
57 | } elsif( $ignore_sort eq 'orthographic' ) { |
e816672a |
58 | $analysis_options{'merge_types'} = 'orthographic'; |
0c236c96 |
59 | } |
60 | |
61 | my $t = run_analysis( $tradition, %analysis_options ); |
0737e7dd |
62 | # Stringify the reading groups |
63 | foreach my $loc ( @{$t->{'variants'}} ) { |
64 | my $mst = wit_stringify( $loc->{'missing'} ); |
65 | $loc->{'missing'} = $mst; |
66 | foreach my $rhash ( @{$loc->{'readings'}} ) { |
67 | my $gst = wit_stringify( $rhash->{'group'} ); |
68 | $rhash->{'group'} = $gst; |
f7371955 |
69 | _stringify_element( $rhash, 'independent_occurrence' ); |
70 | _stringify_element( $rhash, 'reversions' ); |
5089c3b7 |
71 | unless( $rhash->{'text'} ) { |
72 | $rhash->{'text'} = $rhash->{'readingid'}; |
73 | } |
0737e7dd |
74 | } |
75 | } |
d5514865 |
76 | # Values for TT rendering |
9c2e7b80 |
77 | $c->stash->{variants} = $t->{'variants'}; |
78 | $c->stash->{total} = $t->{'variant_count'}; |
79 | $c->stash->{genealogical} = $t->{'genealogical_count'}; |
d5514865 |
80 | $c->stash->{conflict} = $t->{'conflict_count'}; |
81 | # Also make a JSON stash of the data for the statistics tables |
82 | $c->stash->{reading_statistics} = to_json( $t->{'variants'} ); |
9c2e7b80 |
83 | } else { |
84 | $c->stash->{error} = 'Tradition ' . $tradition->name |
85 | . 'has no stemma for analysis.'; |
86 | } |
b8a92065 |
87 | } |
88 | |
f7371955 |
89 | sub _stringify_element { |
90 | my( $hash, $key ) = @_; |
8603127a |
91 | return undef unless exists $hash->{$key}; |
92 | if( ref( $hash->{$key} ) eq 'ARRAY' ) { |
93 | my $str = join( ', ', @{$hash->{$key}} ); |
94 | $hash->{$key} = $str; |
95 | } |
f7371955 |
96 | } |
97 | |
be8bf746 |
98 | =head2 graphsvg |
99 | |
100 | POST stexaminer/graphsvg |
101 | dot: <stemmagraph dot string> |
102 | layerwits: [ <a.c. witnesses ] |
103 | |
104 | Returns an SVG string of the given graph, extended to include the given |
105 | layered witnesses. |
106 | |
107 | =cut |
108 | |
109 | sub graphsvg :Local { |
110 | my( $self, $c ) = @_; |
111 | my $dot = $c->request->param('dot'); |
112 | my @layerwits = $c->request->param('layerwits[]'); |
113 | open my $stemma_fh, '<', \$dot; |
114 | binmode( $stemma_fh, ':encoding(UTF-8)' ); |
115 | my $emptycoll = Text::Tradition::Collation->new(); |
116 | my $tempstemma = Text::Tradition::Stemma->new( |
117 | collation => $emptycoll, 'dot' => $stemma_fh ); |
118 | my $svgopts = { size => [ 600, 350 ] }; |
119 | if( @layerwits ) { |
120 | $svgopts->{'layerwits'} = \@layerwits; |
121 | } |
122 | $c->stash->{'result'} = $tempstemma->as_svg( $svgopts ); |
123 | $c->forward('View::SVG'); |
124 | } |
125 | |
b8a92065 |
126 | =head2 end |
127 | |
128 | Attempt to render a view, if needed. |
129 | |
130 | =cut |
131 | |
132 | sub end : ActionClass('RenderView') {} |
133 | |
134 | =head1 AUTHOR |
135 | |
136 | Tara L Andrews |
137 | |
138 | =head1 LICENSE |
139 | |
140 | This library is free software. You can redistribute it and/or modify |
141 | it under the same terms as Perl itself. |
142 | |
143 | =cut |
144 | |
145 | __PACKAGE__->meta->make_immutable; |
146 | |
147 | 1; |