Commit | Line | Data |
e92d4229 |
1 | package Text::Tradition::Language; |
2 | |
3 | use strict; |
4 | use warnings; |
332750fc |
5 | use Module::Load; |
e92d4229 |
6 | use Moose::Role; |
7 | |
332750fc |
8 | requires 'throw'; |
9 | |
e92d4229 |
10 | =head1 NAME |
11 | |
12 | Text::Tradition::Language - add-on role to enable language awareness and |
8943ff68 |
13 | morphology functions to a Text::Tradition object. Please see |
14 | L<Text::Tradition::Morphology> for more information on the morphology |
15 | add-on distribution. |
e92d4229 |
16 | |
17 | =head1 METHODS |
18 | |
19 | =head2 language |
20 | |
21 | Accessor for the primary language of the tradition. Must correspond to one |
22 | of the Text::Tradition::Language::* modules in this package. |
23 | |
24 | =cut |
25 | |
26 | has 'language' => ( |
27 | is => 'rw', |
28 | isa => 'Str', |
29 | predicate => 'has_language', |
30 | ); |
31 | |
32 | before 'language' => sub { |
33 | my $self = shift; |
34 | if( @_ && $_[0] ne 'Default' ) { |
35 | # We are trying to set the language; check that the corresponding |
36 | # module exists. |
37 | eval "require Text::Tradition::Language::".$_[0]; |
38 | if( $@ ) { |
39 | throw( "Cannot load language module for @_: $@" ); |
40 | } |
41 | } |
42 | }; |
43 | |
44 | =head2 lemmatize |
45 | |
46 | Calls the appropriate lemmatization function for the language of the |
47 | tradition. |
48 | |
49 | =cut |
50 | |
51 | sub lemmatize { |
52 | my $self = shift; |
53 | unless( $self->has_language ) { |
54 | throw( "Please set a language to lemmatize a tradition" ); |
55 | } |
56 | my $mod = "Text::Tradition::Language::" . $self->language; |
57 | load( $mod ); |
58 | $mod->can( 'lemmatize' )->( $self ); |
59 | } |
60 | |
61 | 1; |
62 | |