remove unconditional dep on Morph::Perseus
[scpubgit/stemmatology.git] / lib / Text / Tradition / Language / Latin.pm
index 9f11767..7873c54 100644 (file)
@@ -3,7 +3,8 @@ package Text::Tradition::Language::Latin;
 use strict;
 use warnings;
 use Module::Load;
-use Text::Tradition::Language::Base qw/ lemmatize_treetagger treetagger_struct /;
+use Text::Tradition::Language::Base qw/ lemmatize_treetagger treetagger_struct 
+       lfs_morph_tags /;
 use TryCatch;
 
 =head1 NAME
@@ -86,6 +87,22 @@ sub reading_lookup {
        return map { _perseus_lookup_str( $_ ) } @words;
 }
 
+=head2 morphology_tags
+
+Return a data structure describing the available parts of speech and their attributes.
+
+=cut
+
+sub morphology_tags {
+       try {
+               load 'Morph::Perseus::Structure';
+       } catch {
+               warn "Not using Perseus Latin tags";
+       }
+       return lfs_morph_tags();
+}
+
+
 {
        my $morph;
        
@@ -145,6 +162,7 @@ sub reading_lookup {
        sub _perseus_lookup_tt {
                my( $orig, $pos, $lemma ) = split( /\t/, $_[0] );
                _morph_connect();
+               return unlesss $morph;
                my $result = $morph->lookup( $orig );
                # Discard results that don't match the lemma, unless lemma is unknown
                my @orig = @{$result->{'objects'}};
@@ -172,9 +190,10 @@ sub reading_lookup {
                }
                @ret = @orig unless @ret;
                
-               my @wordforms;
+               my %unique_wordforms;
                foreach my $obj ( @ret ) {
-                       push( @wordforms, _wordform_from_row( $obj ) );
+                       my $wf = _wordform_from_row( $obj );
+                       $unique_wordforms{$wf->to_string} = $wf;
                }
                ## TODO Use TreeTagger info - requires serious hacking of Lingua::TagSet
                # Discard results that don't match the given TreeTagger POS, unless
@@ -186,12 +205,13 @@ sub reading_lookup {
 #                      @ttmatch = @wordforms;
 #              }
 #              return @ttmatch;
-               return @wordforms;
+               return values( %unique_wordforms );
        }
        
        sub _perseus_lookup_str {
                my( $orig ) = @_;
                _morph_connect();
+               return unless $morph;
                # Simple morph DB lookup, and return the results.
                my $result = $morph->lookup( $orig );
                return map { _wordform_from_row( $_ ) } @{$result->{'objects'}};
@@ -203,12 +223,12 @@ sub _wordform_from_row {
        my( $rowobj ) = @_;
        my $mpstruct;
        try {
+               # M::P::St will be loaded already if we got here
                $mpstruct = Morph::Perseus::Structure->from_tag( $rowobj->code );
        } catch {
                warn "Could not create morphology structure from "
                        . $rowobj->code . ": $!";
        }
-       $DB::single = 1 unless $mpstruct;
        my $lemma = $rowobj->lemma;
        $lemma =~ s/^(\D+)\d*$/$1/;
        my $wf = Text::Tradition::Collation::Reading::WordForm->new(