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