allow wordforms to be cleared out
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading / Lexeme.pm
index 220d5d9..fa564ee 100644 (file)
@@ -3,6 +3,8 @@ package Text::Tradition::Collation::Reading::Lexeme;
 use Moose;
 use JSON ();
 use Module::Load;
+use Text::Tradition::Collation::Reading::WordForm;
+use Text::Tradition::Error;
 
 =head1 NAME
 
@@ -77,7 +79,9 @@ has 'wordform_matchlist' => (
                'matching_forms' => 'elements',
                'matching_form' => 'get',
                'add_matching_form' => 'push',
+               'clear_matching_forms' => 'clear',
                },
+       default => sub { [] },
        );
 
 has 'is_disambiguated' => (
@@ -125,6 +129,23 @@ sub BUILD {
        }       
 }
 
+around 'add_matching_form' => sub {
+       my $orig = shift;
+       my $self = shift;
+       my @realargs;
+       foreach my $a ( @_ ) {
+               if( ref( $a ) ) {
+                       push( @realargs, $a );
+               } else {
+                       # Make the wordform from the string
+                       my $wf = Text::Tradition::Collation::Reading::WordForm->new(
+                               'JSON' => $a );
+                       push( @realargs, $wf );
+               }
+       }
+       return $self->$orig( @realargs );
+};
+
 =head2 disambiguate( $index )
 
 Selects the word form at $index in the list of matching forms, and asserts
@@ -141,6 +162,25 @@ sub disambiguate {
        $self->is_disambiguated( 1 );   
 }
 
+=head2 has_form( $rep ) 
+
+Returns the index of the matching form whose string representation is in $rep, 
+or else undef if none is found.
+
+=cut
+
+sub has_form {
+       my( $self, $rep ) = @_;
+       my $i = 0;
+       foreach my $mf ( $self->matching_forms ) {
+               my $struct = $mf->TO_JSON;
+               return $i if $struct eq $rep;
+               $i++;
+       }
+       return undef;
+}
+               
+
 sub TO_JSON {
        my $self = shift;
        my $hash = {};
@@ -150,6 +190,14 @@ sub TO_JSON {
        $hash->{'wordform_matchlist'} = [ $self->matching_forms ] if $self->matches;
        return $hash;
 }
+
+sub throw {
+       Text::Tradition::Error->throw( 
+               'ident' => 'Lexeme error',
+               'message' => $_[0],
+               );
+}
+
 no Moose;
 __PACKAGE__->meta->make_immutable;