add error handling for reading morphology update
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading / WordForm.pm
CommitLineData
cca4f996 1package Text::Tradition::Collation::Reading::WordForm;
2
70745e70 3use Lingua::Features::Structure;
7604424b 4use JSON ();
5use Moose;
a7f4020a 6use Text::Tradition::Error;
7use TryCatch;
cca4f996 8
9=head1 NAME
10
11Text::Tradition::Collation::Reading::WordForm - represents a
12language/lemma/morphology triplet that can be associated with a Reading.
13
14=head1 DESCRIPTION
15
16Text::Tradition is a library for representation and analysis of collated
17texts, particularly medieval ones. A word form is used for the analysis of
18Reading objects; it consists of a lemma, a language, and a code to
19represent its part of speech. In general the word forms for a particular
20language should be read from / written to some morphological database.
21
22=head1 METHODS
23
24=head2 new
25
26Creates a new word form from the passed options.
27
28=head2 language
29
30Returns the language to which this word form belongs.
31
32=head2 lemma
33
34Returns the lemma for the word form.
35
36=head2 morphology
37
38Returns an array representing this word's morphology. The contents of the
39array depend on the language being used.
40
41=cut
42
43has 'language' => (
44 is => 'ro',
45 isa => 'Str',
46 required => 1,
47 );
48
49# TODO do we need this?
50has 'form' => (
51 is => 'ro',
52 isa => 'Str',
53 # required => 1,
54 );
55
56has 'lemma' => (
57 is => 'ro',
58 isa => 'Str',
59 required => 1,
60 );
61
62has 'morphology' => (
63 is => 'ro',
6ad2ce78 64 isa => 'Lingua::Features::Structure',
cca4f996 65 required => 1,
66 );
67
70745e70 68around BUILDARGS => sub {
69 my $orig = shift;
70 my $class = shift;
71 my $args = @_ == 1 ? $_[0] : { @_ };
7604424b 72 if( exists $args->{'JSON'} ) {
da83693e 73 my @data = split( / \/\/ /, $args->{'JSON'} );
75ae2b25 74 # print STDERR "Attempting to parse " . $data[2] . " into structure";
a7f4020a 75 my $morph;
76 try {
77 $morph = Lingua::Features::Structure->from_string( $data[2] );
78 } catch {
79 throw("Could not parse string " . $data[2] . " into morphological structure");
80 }
da83693e 81 $args = { 'language' => $data[0], 'lemma' => $data[1],
82 'morphology' => $morph };
70745e70 83 }
84 $class->$orig( $args );
85};
86
6ad2ce78 87=head2 to_string
88
89Returns a string combination of language/lemma/morphology that can be used
90in equivalence testing.
91
92=cut
93
94sub to_string {
cca4f996 95 my $self = shift;
7604424b 96 return JSON->new->convert_blessed(1)->encode( $self );
97}
98
da83693e 99# Rather than spitting it out as a JSON hash, encode it as a string so that
100# the XML serialization doesn't become insane.
7604424b 101sub TO_JSON {
102 my $self = shift;
da83693e 103 return sprintf( "%s // %s // %s", $self->language, $self->lemma,
104 $self->morphology->to_string() );
cca4f996 105}
6ad2ce78 106
a7f4020a 107sub throw {
108 Text::Tradition::Error->throw(
109 'ident' => 'Wordform error',
110 'message' => $_[0],
111 );
112}
113
cca4f996 114no Moose;
115__PACKAGE__->meta->make_immutable;
116
1171;
118
119=head1 LICENSE
120
121This package is free software and is provided "as is" without express
122or implied warranty. You can redistribute it and/or modify it under
123the same terms as Perl itself.
124
125=head1 AUTHOR
126
127Tara L Andrews E<lt>aurum@cpan.orgE<gt>