1 package Text::Tradition::Language::Base;
5 use Encode qw/ encode_utf8 decode_utf8 /;
7 use vars qw/ @EXPORT_OK /;
8 use IPC::Run qw/ run /;
9 use Lingua::TagSet::Multext;
10 use Lingua::TagSet::TreeTagger;
12 use Text::Tradition::Collation::Reading::Lexeme;
13 use Text::Tradition::Collation::Reading::WordForm;
16 @EXPORT_OK = qw/ lemmatize_treetagger reading_lookup_treetagger /;
20 Text::Tradition::Language::Base - Base subroutines for lemmatization of words
24 Common routines for applying morphological tagging to a Text::Tradition. Used
25 with callbacks from the named language packages.
29 =head2 lemmatize_treetagger( $tradition )
31 Evaluates the tradition with the given options, and returns the results.
35 sub lemmatize_treetagger {
36 my( $tradition, %opts ) = @_;
38 # Given a tradition, lemmatize it witness by witness and see what we get.
39 my $c = $tradition->collation;
40 # First, clear out all existing lexemes from the readings.
41 my %witness_paths = _clear_reading_lexemes( $tradition );
43 foreach my $sig ( keys %witness_paths ) {
44 # Get the text as a sequence of readings and as a string
46 'path' => $witness_paths{$sig},
49 _lemmatize_treetagger_sequence( %witopts );
53 sub _clear_reading_lexemes {
54 my $tradition = shift;
55 my $c = $tradition->collation;
56 # Clear out all existing lexemes from the readings. Save the path as long
57 # as we went to the trouble of generating it.
59 foreach my $wit ( $tradition->witnesses ) {
60 my @sigla = ( $wit->sigil );
61 push( @sigla, $wit->sigil . $c->ac_label ) if $wit->is_layered;
62 foreach my $sig ( @sigla ) {
63 my @path = grep { !$_->is_meta }
64 $c->reading_sequence( $c->start, $c->end, $sig );
65 map { $_->clear_lexemes } @path;
66 $witness_paths{$sig} = \@path;
69 return %witness_paths;
72 =head2 reading_lookup( $rdg[, $rdg, ...] )
74 Looks up one or more readings using the Flemm package, and returns the
75 possible results. This uses the same logic as L<lemmatize> above for the
76 entire tradition, but can also be used to (re-)analyze individual readings.
80 sub reading_lookup_treetagger {
83 return _lemmatize_treetagger_sequence( %opts );
86 sub _lemmatize_treetagger_sequence {
88 my @path = @{$opts{'path'}};
89 my $tagresult = _treetag_string( _text_from_path( 1, @path ), $opts{'language'} );
91 # Map the tagged words onto the original readings, splitting
92 # them up into lexemes where necessary.
93 # NOTE we can have multiple lexemes in a reading, but not
94 # multiple readings to a lexeme.
95 my @tags = split( /\n/, $tagresult );
97 my $curr_rdg = shift @path;
100 foreach my $tag ( @tags ) {
101 # Get the original word
102 my( $lexeme, @rest ) = split( /\t/, $tag );
103 # Lemmatize the whole
104 # TODO error trap this
105 my @forms = $opts{'callback'}( $tag );
107 my $lexobj = Text::Tradition::Collation::Reading::Lexeme->new(
108 'string' => $lexeme, 'language' => $opts{'language'},
109 'wordform_matchlist' => \@forms );
110 # Find the next non-meta reading
111 while( $curr_rdg && $curr_rdg->is_meta ) {
112 $curr_rdg = shift @path;
114 unless( $curr_rdg ) {
115 warn "Ran out of readings in sequence at $lexeme";
118 my $curr_rdg_text = $curr_rdg->has_normal_form
119 ? $curr_rdg->normal_form : $curr_rdg->text;
120 if( $unused_rdg_part &&
121 $unused_rdg_part =~ /^\Q$lexeme\E(\s*)(.*)$/ ) {
122 # Nth part of curr_rdg
123 $unused_rdg_part = $2;
124 push( @curr_lexemes, $lexobj );
125 } elsif( $curr_rdg_text =~ /^\Q$lexeme\E(\s*)(.*)$/ ) {
126 # Flag an error if there is already an unused reading part.
127 warn "Skipped over unused text $unused_rdg_part at $curr_rdg"
129 $unused_rdg_part = $2; # will be empty if the whole reading matched
130 push( @curr_lexemes, $lexobj );
132 # We do not cope with the idea of a lexeme being
133 # spread across multiple readings.
134 warn "Word sequence changed unexpectedly in text";
135 # See if we can find a matching reading
138 while( my $nr = shift @path ) {
139 my $nrtext = $nr->has_normal_form ? $nr->normal_form : $nr->text;
140 if( $nrtext =~ /^\Q$lexeme\E/ ) {
141 $curr_rdg = $lookahead[-1] if @lookahead;
145 push( @lookahead, $nr );
148 # No match? Restore the state we had
150 unshift( @path, @lookahead );
153 $unused_rdg_part = '';
156 unless( $unused_rdg_part ) {
157 # Record the lexemes for the given reading.
158 #print STDERR sprintf( "Adding lexeme(s) %s to reading %s (%s)\n",
159 # join( ' ', map { $_->string } @curr_lexemes ),
160 # $curr_rdg->id, $curr_rdg->text );
161 _update_reading_lexemes( $opts{replace}, $curr_rdg, @curr_lexemes );
162 $curr_rdg = shift @path;
169 sub _update_reading_lexemes {
170 my( $replace, $reading, @lexemes ) = @_;
171 if( $reading->has_lexemes && !$replace ) {
172 # We need to merge what is in @lexemes with what we have already.
173 my @oldlex = $reading->lexemes;
174 my $cmp1 = join( '||', map { $_->string } @oldlex );
175 my $cmp2 = join( '||', map { $_->string } @lexemes );
176 if ( @oldlex == @lexemes && $cmp1 eq $cmp2 ) {
177 # The lexeme strings are the same, so merge the possible
178 # word forms from new to old.
179 foreach my $i ( 0 .. $#lexemes ) {
180 my $ol = $oldlex[$i];
181 my $nl = $lexemes[$i];
183 map { $ofw{$_->to_string} = 1 } $ol->matching_forms;
184 foreach my $form ( $nl->matching_forms ) {
185 unless( $ofw{$form->to_string} ) {
186 # print STDERR "Adding form " . $form->to_string .
187 # " to lexeme " . $nl->string . " at $reading\n";
188 $ol->add_matching_form( $form );
189 $ol->is_disambiguated(0);
194 warn "Lexeme layout for $reading changed; replacing the lot";
195 $reading->clear_lexemes;
196 $reading->add_lexeme( @lexemes );
199 $reading->clear_lexemes if $replace;
200 $reading->add_lexeme( @lexemes );
204 # Utility function so that we can cheat and use it when we need both the path
206 sub _text_from_path {
207 my( $normalize, @path ) = @_;
210 foreach my $r ( @path ) {
211 unless ( $r->join_prior || !$last || $last->join_next ) {
214 $pathtext .= ( $normalize && $r->has_normal_form )
215 ? $r->normal_form : $r->text;
221 # Utility function that actually calls the tree tagger.
222 sub _treetag_string {
223 my( $text, $lang ) = @_;
224 my $wittext = encode_utf8( $text );
225 # Then see if we have TreeTagger
227 load( 'Lingua::TreeTagger' );
229 warn "Cannot run TreeTagger without Lingua::TreeTagger module";
232 # OK, we can run it then.
233 # First upgrade to UTF8 for necessary languages.
234 my @utf8_supported = qw/ French /;
235 my %ttopts = ( 'language' => $lang, 'options' => [ qw/ -token -lemma / ] );
236 if( grep { $_ eq $lang } @utf8_supported ) {
237 $ttopts{'use_utf8'} = 1;
239 # Now instantiate and run the tagger.
240 my $tagger = Lingua::TreeTagger->new( %ttopts );
241 my $tagresult = $tagger->tag_text( \$text );
243 # TODO maybe send the tokens back rather than the interpreted string...
244 return $tagresult->as_text();
254 =item * Handle package dependencies more gracefully
260 This package is free software and is provided "as is" without express
261 or implied warranty. You can redistribute it and/or modify it under
262 the same terms as Perl itself.
266 Tara L Andrews E<lt>aurum@cpan.orgE<gt>