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