1 package Text::Tradition::Collation::Reading::Lexeme;
6 use Text::Tradition::Collation::Reading::WordForm;
10 Text::Tradition::Collation::Reading::Lexeme - represents the components of
15 Text::Tradition is a library for representation and analysis of collated
16 texts, particularly medieval ones. A word form is used for the analysis of
17 Reading objects; it consists of a lemma, a language, and a code to
18 represent its part of speech. In general the word forms for a particular
19 language should be read from / written to some morphological database.
25 Creates a new lexeme from the passed options.
29 Returns the language to which this lexeme belongs.
33 Returns the canonical string version of this lexeme.
37 Returns the number of possible word forms for this lexeme, as drawn from
38 the appropriate database.
42 Returns an array of the possible word forms for this lexeme.
44 =head2 matching_form( $index )
46 Returns the form at $index in the list of matching forms.
48 =head2 is_disambiguated
50 Returns true if a single wordform has been picked as 'correct' for this
51 lexeme in its context.
55 Returns the correct word form (if any has been selected) for the lexeme in
60 # TODO need to be able to populate this from DB
73 has 'wordform_matchlist' => (
74 isa => 'ArrayRef[Text::Tradition::Collation::Reading::WordForm]',
78 'matching_forms' => 'elements',
79 'matching_form' => 'get',
80 'add_matching_form' => 'push',
82 default => sub { [] },
85 has 'is_disambiguated' => (
93 isa => 'Text::Tradition::Collation::Reading::WordForm',
94 writer => '_set_form',
97 around BUILDARGS => sub {
100 my $args = @_ == 1 ? $_[0] : { @_ };
101 if( exists $args->{JSON} ) {
102 my $data = $args->{JSON};
103 if( exists $data->{'form'} && $data->{'form'} ) {
104 my $form = Text::Tradition::Collation::Reading::WordForm->new(
105 'JSON' => $data->{'form'} );
106 $data->{'form'} = $form;
108 if( exists $data->{'wordform_matchlist'} && $data->{'wordform_matchlist'} ) {
110 foreach my $wfjson ( @{$data->{'wordform_matchlist'}} ) {
111 push( @ml, Text::Tradition::Collation::Reading::WordForm->new(
112 'JSON' => $wfjson ) );
114 $data->{'wordform_matchlist'} = \@ml;
118 $class->$orig( $args );
121 # Do auto-disambiguation if we were created with a single wordform
125 if( $self->matches == 1 ) {
126 $self->disambiguate( 0 );
130 around 'add_matching_form' => sub {
134 foreach my $a ( @_ ) {
136 push( @realargs, $a );
138 # Make the wordform from the string
139 my $wf = Text::Tradition::Collation::Reading::WordForm->new(
141 push( @realargs, $wf );
144 return $self->$orig( @realargs );
147 =head2 disambiguate( $index )
149 Selects the word form at $index in the list of matching forms, and asserts
150 that this is the correct form for the lexeme.
155 my( $self, $idx ) = @_;
156 my $form = $self->matching_form( $idx );
157 throw( "There is no candidate wordform at index $idx" )
159 $self->_set_form( $form );
160 $self->is_disambiguated( 1 );
163 =head2 has_form( $rep )
165 Returns the index of the matching form whose string representation is in $rep,
166 or else undef if none is found.
171 my( $self, $rep ) = @_;
173 foreach my $mf ( $self->matching_forms ) {
174 my $struct = $mf->TO_JSON;
175 return $i if $struct eq $rep;
186 map { $hash->{$_} = $self->$_ if defined $self->$_ }
187 qw/ language string is_disambiguated form /;
188 $hash->{'wordform_matchlist'} = [ $self->matching_forms ] if $self->matches;
192 __PACKAGE__->meta->make_immutable;
198 This package is free software and is provided "as is" without express
199 or implied warranty. You can redistribute it and/or modify it under
200 the same terms as Perl itself.
204 Tara L Andrews E<lt>aurum@cpan.orgE<gt>