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',
83 default => sub { [] },
86 has 'is_disambiguated' => (
94 isa => 'Text::Tradition::Collation::Reading::WordForm',
95 writer => '_set_form',
98 around BUILDARGS => sub {
101 my $args = @_ == 1 ? $_[0] : { @_ };
102 if( exists $args->{JSON} ) {
103 my $data = $args->{JSON};
104 if( exists $data->{'form'} && $data->{'form'} ) {
105 my $form = Text::Tradition::Collation::Reading::WordForm->new(
106 'JSON' => $data->{'form'} );
107 $data->{'form'} = $form;
109 if( exists $data->{'wordform_matchlist'} && $data->{'wordform_matchlist'} ) {
111 foreach my $wfjson ( @{$data->{'wordform_matchlist'}} ) {
112 push( @ml, Text::Tradition::Collation::Reading::WordForm->new(
113 'JSON' => $wfjson ) );
115 $data->{'wordform_matchlist'} = \@ml;
119 $class->$orig( $args );
122 # Do auto-disambiguation if we were created with a single wordform
126 if( $self->matches == 1 ) {
127 $self->disambiguate( 0 );
131 around 'add_matching_form' => sub {
135 foreach my $a ( @_ ) {
137 push( @realargs, $a );
139 # Make the wordform from the string
140 my $wf = Text::Tradition::Collation::Reading::WordForm->new(
142 push( @realargs, $wf );
145 return $self->$orig( @realargs );
148 =head2 disambiguate( $index )
150 Selects the word form at $index in the list of matching forms, and asserts
151 that this is the correct form for the lexeme.
156 my( $self, $idx ) = @_;
157 my $form = $self->matching_form( $idx );
158 throw( "There is no candidate wordform at index $idx" )
160 $self->_set_form( $form );
161 $self->is_disambiguated( 1 );
164 =head2 has_form( $rep )
166 Returns the index of the matching form whose string representation is in $rep,
167 or else undef if none is found.
172 my( $self, $rep ) = @_;
174 foreach my $mf ( $self->matching_forms ) {
175 my $struct = $mf->TO_JSON;
176 return $i if $struct eq $rep;
187 map { $hash->{$_} = $self->$_ if defined $self->$_ }
188 qw/ language string is_disambiguated form /;
189 $hash->{'wordform_matchlist'} = [ $self->matching_forms ] if $self->matches;
194 Text::Tradition::Error->throw(
195 'ident' => 'Lexeme error',
201 __PACKAGE__->meta->make_immutable;
207 This package is free software and is provided "as is" without express
208 or implied warranty. You can redistribute it and/or modify it under
209 the same terms as Perl itself.
213 Tara L Andrews E<lt>aurum@cpan.orgE<gt>