add rudimentary logic for text segmentation in lemmatizer
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Relation.pm
CommitLineData
5c9ecf66 1package stemmaweb::Controller::Relation;
2376359f 2use Moose;
3use namespace::autoclean;
581aee24 4use TryCatch;
2376359f 5
6BEGIN { extends 'Catalyst::Controller' }
7
8
9=head1 NAME
10
5c9ecf66 11stemmaweb::Controller::Relation - Controller for the relationship mapper
2376359f 12
13=head1 DESCRIPTION
14
581aee24 15The reading relationship mapper with draggable nodes.
2376359f 16
17=head1 METHODS
18
581aee24 19=head2 index
20
2376359f 21 GET relation/$textid
22
23Renders the application for the text identified by $textid.
24
2376359f 25=cut
26
72874569 27sub index :Path :Args(0) {
28 my( $self, $c ) = @_;
581aee24 29 $c->stash->{'template'} = 'relate.tt';
30}
31
9ca77245 32=head2 help
33
34 GET relation/help
35
36Returns the help window HTML.
37
38=cut
39
40sub help :Local :Args(0) {
41 my( $self, $c ) = @_;
42 $c->stash->{'template'} = 'relatehelp.tt';
43}
44
72874569 45=head2 definitions
581aee24 46
1c0900ef 47 GET relation/definitions
581aee24 48
49Returns a data structure giving the valid types and scopes for a relationship.
50
51=cut
52
1c0900ef 53sub definitions :Local :Args(0) {
581aee24 54 my( $self, $c ) = @_;
72874569 55 my $valid_relationships = [ qw/ spelling orthographic grammatical meaning
56 lexical transposition / ];
581aee24 57 my $valid_scopes = [ qw/ local global / ];
58 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
59 $c->forward('View::JSON');
2376359f 60}
61
72874569 62=head2 text
581aee24 63
72874569 64 GET relation/$textid/
65
66 Runs the relationship mapper for the specified text ID.
67
581aee24 68=cut
69
72874569 70sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
71 my( $self, $c, $textid ) = @_;
93daee83 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;
72874569 94}
95
96sub main :Chained('text') :PathPart('') :Args(0) {
581aee24 97 my( $self, $c ) = @_;
93daee83 98 my $startseg = $c->req->param('start');
1c0900ef 99 my $tradition = delete $c->stash->{'tradition'};
100 my $collation = $tradition->collation;
93daee83 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 );
72874569 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';
581aee24 118}
119
120=head2 relationships
121
93daee83 122 GET relation/$textid/relationships
72874569 123
124Returns the list of relationships defined for this text.
581aee24 125
93daee83 126 POST relation/$textid/relationships { request }
72874569 127
128Attempts to define the requested relationship within the text. Returns 200 on
129success or 403 on error.
581aee24 130
93daee83 131 DELETE relation/$textid/relationships { request }
72874569 132
581aee24 133
134=cut
135
72874569 136sub relationships :Chained('text') :PathPart :Args(0) {
581aee24 137 my( $self, $c ) = @_;
bff7bb42 138 my $tradition = delete $c->stash->{'tradition'};
139 my $collation = $tradition->collation;
7c280843 140 my $m = $c->model('Directory');
72874569 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 );
9251a7be 146 next if $relobj->type eq 'collated'; # Don't show these
31aaf446 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 );
72874569 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,
31aaf446 161 'scope' => $scope };
162 $opts->{'annotation'} = $note if $note;
72874569 163
164 try {
165 my @vectors = $collation->add_relationship( $node, $target, $opts );
166 $c->stash->{'result'} = \@vectors;
7c280843 167 $m->save( $tradition );
72874569 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 );
7c280843 178 $m->save( $tradition );
72874569 179 $c->stash->{'result'} = \@vectors;
180 } catch( Text::Tradition::Error $e ) {
181 $c->response->status( '403' );
182 $c->stash->{'result'} = { 'error' => $e->message };
183 }
581aee24 184 }
581aee24 185 $c->forward('View::JSON');
186}
72874569 187
581aee24 188
2376359f 189=head2 end
190
191Attempt to render a view, if needed.
192
193=cut
194
195sub end : ActionClass('RenderView') {}
196
197=head1 AUTHOR
198
199Tara L Andrews
200
201=head1 LICENSE
202
203This library is free software. You can redistribute it and/or modify
204it under the same terms as Perl itself.
205
206=cut
207
208__PACKAGE__->meta->make_immutable;
209
2101;