1 package Text::Tradition::Collation::Reading::Lexeme;
9 Text::Tradition::Collation::Reading::Lexeme - represents the components of
14 Text::Tradition is a library for representation and analysis of collated
15 texts, particularly medieval ones. A word form is used for the analysis of
16 Reading objects; it consists of a lemma, a language, and a code to
17 represent its part of speech. In general the word forms for a particular
18 language should be read from / written to some morphological database.
24 Creates a new lexeme from the passed options.
28 Returns the language to which this lexeme belongs.
32 Returns the canonical string version of this lexeme.
36 Returns the number of possible word forms for this lexeme, as drawn from
37 the appropriate database.
41 Returns an array of the possible word forms for this lexeme.
43 =head2 matching_form( $index )
45 Returns the form at $index in the list of matching forms.
47 =head2 is_disambiguated
49 Returns true if a single wordform has been picked as 'correct' for this
50 lexeme in its context.
54 Returns the correct word form (if any has been selected) for the lexeme in
59 # TODO need to be able to populate this from DB
72 has 'wordform_matchlist' => (
73 isa => 'ArrayRef[Text::Tradition::Collation::Reading::WordForm]',
77 'matching_forms' => 'elements',
78 'matching_form' => 'get',
79 'add_matching_form' => 'push',
83 has 'is_disambiguated' => (
91 isa => 'Text::Tradition::Collation::Reading::WordForm',
92 writer => '_set_form',
95 around BUILDARGS => sub {
98 my $args = @_ == 1 ? $_[0] : { @_ };
99 if( exists $args->{JSON} ) {
100 my $data = $args->{JSON};
101 if( exists $data->{'form'} && $data->{'form'} ) {
102 my $form = Text::Tradition::Collation::Reading::WordForm->new(
103 'JSON' => $data->{'form'} );
104 $data->{'form'} = $form;
106 if( exists $data->{'wordform_matchlist'} && $data->{'wordform_matchlist'} ) {
108 foreach my $wfjson ( @{$data->{'wordform_matchlist'}} ) {
109 push( @ml, Text::Tradition::Collation::Reading::WordForm->new(
110 'JSON' => $wfjson ) );
112 $data->{'wordform_matchlist'} = \@ml;
116 $class->$orig( $args );
119 # Do auto-disambiguation if we were created with a single wordform
123 if( $self->matches == 1 ) {
124 $self->disambiguate( 0 );
128 around 'add_matching_form' => sub {
132 foreach my $a ( @_ ) {
134 push( @realargs, $a );
136 # Make the wordform from the string
137 my $wf = Text::Tradition::Collation::Reading::WordForm->new(
139 push( @realargs, $wf );
142 return $self->$orig( @realargs );
145 =head2 disambiguate( $index )
147 Selects the word form at $index in the list of matching forms, and asserts
148 that this is the correct form for the lexeme.
153 my( $self, $idx ) = @_;
154 my $form = $self->matching_form( $idx );
155 throw( "There is no candidate wordform at index $idx" )
157 $self->_set_form( $form );
158 $self->is_disambiguated( 1 );
161 =head2 has_form( $rep )
163 Returns the index of the matching form whose string representation is in $rep,
164 or else undef if none is found.
169 my( $self, $rep ) = @_;
171 foreach my $mf ( $self->matching_forms ) {
172 my $struct = $mf->TO_JSON;
173 return $i if $struct eq $rep;
184 map { $hash->{$_} = $self->$_ if defined $self->$_ }
185 qw/ language string is_disambiguated form /;
186 $hash->{'wordform_matchlist'} = [ $self->matching_forms ] if $self->matches;
190 __PACKAGE__->meta->make_immutable;
196 This package is free software and is provided "as is" without express
197 or implied warranty. You can redistribute it and/or modify it under
198 the same terms as Perl itself.
202 Tara L Andrews E<lt>aurum@cpan.orgE<gt>