morphology piece now works for main submit, not for relemmatization yet
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Relation.pm
index 4809dec..c980f80 100644 (file)
@@ -214,23 +214,33 @@ Returns the list of readings defined for this text along with their metadata.
 
 =cut
 
+my %read_write_keys = (
+       'id' => 0,
+       'text' => 0,
+       'is_meta' => 0,
+       'grammar_invalid' => 1,
+       'is_nonsense' => 1,
+       'normal_form' => 1,
+);
+
 sub _reading_struct {
        my( $reading ) = @_;
        # Return a JSONable struct of the useful keys.  Keys meant to be writable
        # have a true value; read-only keys have a false value.
-       my %read_write_keys = (
-               'id' => 0,
-               'text' => 0,
-               'is_meta' => 0,
-               'grammar_invalid' => 1,
-               'is_nonsense' => 1,
-               'normal_form' => 1,
-               'lexemes' => 1,  # special case?
-       );
        my $struct = {};
        map { $struct->{$_} = $reading->$_ } keys( %read_write_keys );
        # Special case
        $struct->{'lexemes'} = [ $reading->lexemes ];
+       # Look up any words related via spelling or orthography
+       my $sameword = sub { 
+               my $t = $_[0]->type;
+               return $t eq 'spelling' || $t eq 'orthographic';
+       };
+       my @variants;
+       foreach my $sr ( $reading->related_readings( $sameword ) ) {
+               push( @variants, $sr->text );
+       }
+       $struct->{'variants'} = \@variants;
        return $struct;
 }
 
@@ -266,13 +276,44 @@ sub reading :Chained('text') :PathPart :Args(1) {
        my( $self, $c, $reading_id ) = @_;
        my $tradition = delete $c->stash->{'tradition'};
        my $collation = $tradition->collation;
+       my $rdg = $collation->reading( $reading_id );
        my $m = $c->model('Directory');
        if( $c->request->method eq 'GET' ) {
-               my $rdg = $collation->reading( $reading_id );
                $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
                        : { 'error' => "No reading with ID $reading_id" };
        } elsif ( $c->request->method eq 'POST' ) {
-               # TODO Update the reading if we can.
+               # Are we re-lemmatizing?
+               if( $c->request->param('relemmatize') ) {
+                       my $nf = $c->request->param('normal_form');
+                       # TODO throw error unless $nf
+                       $rdg->normal_form( $nf );
+                       $rdg->lemmatize();
+               } else {
+                       # Set all the values that we have for the reading.
+                       # TODO error handling
+                       foreach my $p ( keys %{$c->request->params} ) {
+                               if( $p =~ /^morphology_(\d+)$/ ) {
+                                       # Set the form on the correct lexeme
+                                       my $midx = $1;
+                                       $c->log->debug( "Fetching lexeme $midx" );
+                                       my $lx = $rdg->lexeme( $midx );
+                                       my $strrep = $rdg->language . ' // ' 
+                                               . $c->request->param( $p );
+                                       my $idx = $lx->has_form( $strrep );
+                                       unless( defined $idx ) {
+                                               # Make the word form and add it to the lexeme.
+                                               $c->log->debug("Adding new form for $strrep");
+                                               $idx = $lx->add_matching_form( $strrep ) - 1;
+                                       }
+                                       $lx->disambiguate( $idx );
+                               } elsif( $read_write_keys{$p} ) {
+                                       $rdg->$p( $c->request->param( $p ) );
+                               }
+                       }               
+               }
+               $m->save( $tradition );
+               $c->stash->{'result'} = _reading_struct( $rdg );
+
        }
        $c->forward('View::JSON');