add rudimentary logic for text segmentation in lemmatizer
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Relation.pm
1 package stemmaweb::Controller::Relation;
2 use Moose;
3 use namespace::autoclean;
4 use TryCatch;
5
6 BEGIN { extends 'Catalyst::Controller' }
7
8
9 =head1 NAME
10
11 stemmaweb::Controller::Relation - Controller for the relationship mapper
12
13 =head1 DESCRIPTION
14
15 The reading relationship mapper with draggable nodes.
16
17 =head1 METHODS
18
19 =head2 index
20
21  GET relation/$textid
22  
23 Renders the application for the text identified by $textid.
24
25 =cut
26
27 sub index :Path :Args(0) {
28         my( $self, $c ) = @_;
29         $c->stash->{'template'} = 'relate.tt';
30 }
31
32 =head2 help
33
34  GET relation/help
35
36 Returns the help window HTML.
37
38 =cut
39
40 sub help :Local :Args(0) {
41         my( $self, $c ) = @_;
42         $c->stash->{'template'} = 'relatehelp.tt';
43 }
44
45 =head2 definitions
46
47  GET relation/definitions
48  
49 Returns a data structure giving the valid types and scopes for a relationship.
50
51 =cut
52
53 sub definitions :Local :Args(0) {
54         my( $self, $c ) = @_;
55         my $valid_relationships = [ qw/ spelling orthographic grammatical meaning 
56                                                                         lexical transposition / ];
57         my $valid_scopes = [ qw/ local global / ];
58         $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
59         $c->forward('View::JSON');
60 }
61
62 =head2 text
63
64  GET relation/$textid/
65  
66  Runs the relationship mapper for the specified text ID.
67  
68 =cut
69
70 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
71         my( $self, $c, $textid ) = @_;
72         # If the tradition has more than 500 ranks or so, split it up.
73         my $tradition = $c->model('Directory')->tradition( $textid );
74         my $length = $tradition->collation->end->rank;
75         if( $length > 700 ) {
76                 # Segment the tradition in order not to overload the browser.
77                 # Split it up into units of 500 ranks, but have each segment show
78                 # 550 ranks so that overlap works.
79                 my @divs;
80                 my $r = 0;
81                 while( $r + 50 < $length ) {
82                         push( @divs, $r );
83                         $r += 500;
84                 }
85                 $c->stash->{'textsegments'} = [];
86                 foreach my $start ( @divs ) {
87                         my $seg = { 'start' => $start };
88                         $seg->{'end'} = $start + 550 > $length ? $length : $start + 550;
89                         push( @{$c->stash->{'textsegments'}}, $seg );
90                 }
91         }
92         $c->stash->{'textid'} = $textid;
93         $c->stash->{'tradition'} = $tradition;
94 }
95
96 sub main :Chained('text') :PathPart('') :Args(0) {
97         my( $self, $c ) = @_;
98         my $startseg = $c->req->param('start');
99         my $tradition = delete $c->stash->{'tradition'};
100         my $collation = $tradition->collation;
101         my $svgopts;
102         if( $startseg ) {
103                 # Only render the subgraph from startseg to +500 or end,
104                 # whichever is less.
105                 $svgopts = { 'from' => $startseg };
106                 $svgopts->{'to'} = $startseg + 550
107                         if $startseg + 550 < $collation->end->rank;
108         } elsif( exists $c->stash->{'textsegments'} ) {
109                 # This is the unqualified load of a long tradition. We implicitly start 
110                 # at zero, but go only as far as 550.
111                 $svgopts = { 'to' => 550 };
112         }
113         my $svg_str = $collation->as_svg( $svgopts );
114         $svg_str =~ s/\n//gs;
115         $c->stash->{'svg_string'} = $svg_str;
116         $c->stash->{'text_title'} = $tradition->name;
117         $c->stash->{'template'} = 'relate.tt';
118 }
119
120 =head2 relationships
121
122  GET relation/$textid/relationships
123
124 Returns the list of relationships defined for this text.
125
126  POST relation/$textid/relationships { request }
127  
128 Attempts to define the requested relationship within the text. Returns 200 on
129 success or 403 on error.
130
131  DELETE relation/$textid/relationships { request }
132  
133
134 =cut
135
136 sub relationships :Chained('text') :PathPart :Args(0) {
137         my( $self, $c ) = @_;
138         my $tradition = delete $c->stash->{'tradition'};
139         my $collation = $tradition->collation;
140         my $m = $c->model('Directory');
141         if( $c->request->method eq 'GET' ) {
142                 my @pairs = $collation->relationships; # returns the edges
143                 my @all_relations;
144                 foreach my $p ( @pairs ) {
145                         my $relobj = $collation->relations->get_relationship( @$p );
146                         next if $relobj->type eq 'collated'; # Don't show these
147                         my $relhash = { source => $p->[0], target => $p->[1], 
148                                   type => $relobj->type, scope => $relobj->scope };
149                         $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
150                         push( @all_relations, $relhash );
151                 }
152                 $c->stash->{'result'} = \@all_relations;
153         } elsif( $c->request->method eq 'POST' ) {
154                 my $node = $c->request->param('source_id');
155                 my $target = $c->request->param('target_id');
156                 my $relation = $c->request->param('rel_type');
157                 my $note = $c->request->param('note');
158                 my $scope = $c->request->param('scope');
159         
160                 my $opts = { 'type' => $relation,
161                                          'scope' => $scope };
162                 $opts->{'annotation'} = $note if $note;
163                 
164                 try {
165                         my @vectors = $collation->add_relationship( $node, $target, $opts );
166                         $c->stash->{'result'} = \@vectors;
167                         $m->save( $tradition );
168                 } catch( Text::Tradition::Error $e ) {
169                         $c->response->status( '403' );
170                         $c->stash->{'result'} = { 'error' => $e->message };
171                 }
172         } elsif( $c->request->method eq 'DELETE' ) {
173                 my $node = $c->request->param('source_id');
174                 my $target = $c->request->param('target_id');
175         
176                 try {
177                         my @vectors = $collation->del_relationship( $node, $target );
178                         $m->save( $tradition );
179                         $c->stash->{'result'} = \@vectors;
180                 } catch( Text::Tradition::Error $e ) {
181                         $c->response->status( '403' );
182                         $c->stash->{'result'} = { 'error' => $e->message };
183                 }       
184         }
185         $c->forward('View::JSON');
186 }               
187                 
188
189 =head2 end
190
191 Attempt to render a view, if needed.
192
193 =cut
194
195 sub end : ActionClass('RenderView') {}
196
197 =head1 AUTHOR
198
199 Tara L Andrews
200
201 =head1 LICENSE
202
203 This library is free software. You can redistribute it and/or modify
204 it under the same terms as Perl itself.
205
206 =cut
207
208 __PACKAGE__->meta->make_immutable;
209
210 1;