use English module for lemmatizing English words, oops
[scpubgit/stemmatology.git] / lib / Text / Tradition / Language / English.pm
1 package Text::Tradition::Language::English;
2
3 use strict;
4 use warnings;
5 use Lingua::TagSet::TreeTagger::English;
6 use Text::Tradition::Language::Base qw/ lemmatize_treetagger reading_lookup_treetagger
7         lfs_morph_tags /;
8 use TryCatch;
9
10 =head1 NAME
11
12 Text::Tradition::Language::English - language-specific module for English
13
14 =head1 DESCRIPTION
15
16 Implements morphology lookup for English words in context.  This module
17 depends on the TreeTagger software
18 (L<http://www.ims.uni-stuttgart.de/projekte/corplex/TreeTagger/>), which is
19 (for now) expected to be installed in $MORPHDIR/TreeTagger.
20
21 =head1 SUBROUTINES
22
23 =head2 lemmatize( $text )
24
25 Evaluates the string using the TreeTagger, and returns the results.
26
27 =begin testing
28
29 binmode STDOUT, ':utf8';
30 use Text::Tradition;
31 use_ok( 'Text::Tradition::Language::English' );
32
33 =end testing
34
35 =cut
36
37 sub lemmatize {
38         my $tradition = shift;
39         my %opts = ( 
40                 'language' => 'English', 
41                 'callback' => sub { _parse_wordform( @_ ) } 
42                 );
43         return lemmatize_treetagger( $tradition, %opts );
44 }
45
46 =head2 reading_lookup( $rdg[, $rdg, ...] )
47
48 Looks up one or more readings using the Flemm package, and returns the
49 possible results.  This uses the same logic as L<lemmatize> above for the
50 entire tradition, but can also be used to (re-)analyze individual readings.
51
52 =cut
53
54 sub reading_lookup {
55         my( @path ) = @_;
56         my %opts = ( 
57                 'language' => 'English',
58                 'callback' => sub { _parse_wordform( @_ ) },
59                 'path' => \@path,
60                 );
61         return reading_lookup_treetagger( %opts );
62 }
63
64 =head2 morphology_tags
65
66 Return a data structure describing the available parts of speech and their attributes.
67
68 =cut
69
70 sub morphology_tags {
71         return lfs_morph_tags();
72 }
73
74 # Utility function to turn a TreeTagger result into a WordForm
75 sub _parse_wordform {
76         my $tagresult = shift;
77         my( $orig, $tag, $lemma ) = split( /\t/, $tagresult );
78         return () unless $tag =~ /\w/; # skip punct-only "tags"
79         my $morphobj = Lingua::TagSet::TreeTagger::English->tag2structure( $tag );
80         if( $morphobj ) {
81                 return ( Text::Tradition::Collation::Reading::WordForm->new(
82                         'language' => 'English',
83                         'lemma' => $lemma,
84                         'morphology' => $morphobj,
85                         ) );
86         } else {
87                 warn "No morphology found for word: $tagresult";
88                 return ();
89         }
90 }
91
92 1;
93
94 =head2 TODO
95
96 =over
97
98 =item * Tests!
99
100 =back
101
102 =head1 LICENSE
103
104 This package is free software and is provided "as is" without express
105 or implied warranty.  You can redistribute it and/or modify it under
106 the same terms as Perl itself.
107
108 =head1 AUTHOR
109
110 Tara L Andrews E<lt>aurum@cpan.orgE<gt>