1 package Text::Tradition::Collation::Reading::Lexeme;
6 use Text::Tradition::Collation::Reading::WordForm;
7 use Text::Tradition::Error;
11 Text::Tradition::Collation::Reading::Lexeme - represents the components of
16 Text::Tradition is a library for representation and analysis of collated
17 texts, particularly medieval ones. A word form is used for the analysis of
18 Reading objects; it consists of a lemma, a language, and a code to
19 represent its part of speech. In general the word forms for a particular
20 language should be read from / written to some morphological database.
26 Creates a new lexeme from the passed options.
30 Returns the language to which this lexeme belongs.
34 Returns the canonical string version of this lexeme.
38 Returns the number of possible word forms for this lexeme, as drawn from
39 the appropriate database.
43 Returns an array of the possible word forms for this lexeme.
45 =head2 matching_form( $index )
47 Returns the form at $index in the list of matching forms.
49 =head2 is_disambiguated
51 Returns true if a single wordform has been picked as 'correct' for this
52 lexeme in its context.
56 Returns the correct word form (if any has been selected) for the lexeme in
61 # TODO need to be able to populate this from DB
74 has 'wordform_matchlist' => (
75 isa => 'ArrayRef[Text::Tradition::Collation::Reading::WordForm]',
79 'matching_forms' => 'elements',
80 'matching_form' => 'get',
81 'add_matching_form' => 'push',
82 'clear_matching_forms' => 'clear',
84 default => sub { [] },
87 has 'is_disambiguated' => (
95 isa => 'Text::Tradition::Collation::Reading::WordForm',
96 writer => '_set_form',
99 around BUILDARGS => sub {
102 my $args = @_ == 1 ? $_[0] : { @_ };
103 if( exists $args->{JSON} ) {
104 my $data = $args->{JSON};
105 if( exists $data->{'form'} && $data->{'form'} ) {
106 my $form = Text::Tradition::Collation::Reading::WordForm->new(
107 'JSON' => $data->{'form'} );
108 $data->{'form'} = $form;
110 if( exists $data->{'wordform_matchlist'} && $data->{'wordform_matchlist'} ) {
112 foreach my $wfjson ( @{$data->{'wordform_matchlist'}} ) {
113 push( @ml, Text::Tradition::Collation::Reading::WordForm->new(
114 'JSON' => $wfjson ) );
116 $data->{'wordform_matchlist'} = \@ml;
120 $class->$orig( $args );
123 # Do auto-disambiguation if we were created with a single wordform
127 if( $self->matches == 1 ) {
128 $self->disambiguate( 0 );
132 around 'add_matching_form' => sub {
136 foreach my $a ( @_ ) {
138 push( @realargs, $a );
140 # Make the wordform from the string
141 my $wf = Text::Tradition::Collation::Reading::WordForm->new(
143 push( @realargs, $wf );
146 return $self->$orig( @realargs );
149 =head2 disambiguate( $index )
151 Selects the word form at $index in the list of matching forms, and asserts
152 that this is the correct form for the lexeme.
157 my( $self, $idx ) = @_;
158 my $form = $self->matching_form( $idx );
159 throw( "There is no candidate wordform at index $idx" )
161 $self->_set_form( $form );
162 $self->is_disambiguated( 1 );
165 =head2 has_form( $rep )
167 Returns the index of the matching form whose string representation is in $rep,
168 or else undef if none is found.
173 my( $self, $rep ) = @_;
175 foreach my $mf ( $self->matching_forms ) {
176 my $struct = $mf->TO_JSON;
177 return $i if $struct eq $rep;
188 map { $hash->{$_} = $self->$_ if defined $self->$_ }
189 qw/ language string is_disambiguated form /;
190 $hash->{'wordform_matchlist'} = [ $self->matching_forms ] if $self->matches;
195 Text::Tradition::Error->throw(
196 'ident' => 'Lexeme error',
202 __PACKAGE__->meta->make_immutable;
208 This package is free software and is provided "as is" without express
209 or implied warranty. You can redistribute it and/or modify it under
210 the same terms as Perl itself.
214 Tara L Andrews E<lt>aurum@cpan.orgE<gt>