From: Tara L Andrews Date: Tue, 29 May 2012 03:05:29 +0000 (+0200) Subject: morphology piece now works for main submit, not for relemmatization yet X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0dcdd5ecd06664dd20f863dbc077dd19d869b801;p=scpubgit%2Fstemmaweb.git morphology piece now works for main submit, not for relemmatization yet --- diff --git a/lib/stemmaweb/Controller/Relation.pm b/lib/stemmaweb/Controller/Relation.pm index 4809dec..c980f80 100644 --- a/lib/stemmaweb/Controller/Relation.pm +++ b/lib/stemmaweb/Controller/Relation.pm @@ -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'); diff --git a/root/js/relationship.js b/root/js/relationship.js index b513197..8f308db 100644 --- a/root/js/relationship.js +++ b/root/js/relationship.js @@ -66,9 +66,14 @@ function node_dblclick_listener( evt ) { $('#reading_normal_form').attr( 'size', nfboxsize ) $('#reading_normal_form').val( normal_form ); // Now do the morphological properties. + morphology_form( reading_info['lexemes'] ); // and then open the dialog. + $('#reading-form').dialog("open"); +} + +function morphology_form ( lexlist ) { $('#morphology').empty(); - $.each( reading_info['lexemes'], function( idx, lex ) { + $.each( lexlist, function( idx, lex ) { var morphoptions = []; $.each( lex['wordform_matchlist'], function( tdx, tag ) { var tagstr = stringify_wordform( tag ); @@ -86,11 +91,11 @@ function node_dblclick_listener( evt ) { $('#morphology').append( el ); }); }); - $('#reading-form').dialog("open"); } function stringify_wordform ( tag ) { - return tag['lemma'] + ' // ' + tag['morphology']; + var elements = tag.split(' // '); + return elements[1] + ' // ' + elements[2]; } function morph_elements ( formtag, formtxt, currform, morphoptions ) { @@ -101,7 +106,8 @@ function morph_elements ( formtag, formtxt, currform, morphoptions ) { var formlabel = $('