| 1 | package Lingua::JA::Summarize::Extract::ResultSet; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw( Class::Accessor::Fast ); | ||
| 5 | __PACKAGE__->mk_accessors(qw/ length summary sentences /); | ||
| 6 | |||
| 7 | use overload ( | ||
| 8 | q("") => \&as_string, | ||
| 9 | fallback => 1, | ||
| 10 | ); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $self = $class->SUPER::new(@_); | ||
| 15 | $self->{length} ||= 255; | ||
| 16 | $self->init; | ||
| 17 | $self; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub init { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | my @ranking = (); | ||
| 24 | for my $line (@{ $self->sentences }) { | ||
| 25 | next unless $line; | ||
| 26 | my $score = 0; | ||
| 27 | for my $term (@{ $self->summary }) { | ||
| 28 | my @words = split /\s/, $term->{term}; | ||
| 29 | my %tmp; | ||
| 30 | my $tmp_score = 0; | ||
| 31 | for my $word (@words) { | ||
| 32 | for ($line->{text} =~ /(\Q$word\E)/) { | ||
| 33 | $tmp_score += $term->{score}; | ||
| 34 | $tmp{$word} = 1; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | $score += $tmp_score if scalar(@words) == scalar(keys %tmp); | ||
| 38 | } | ||
| 39 | push @ranking, +{ %{ $line }, score => $score, A => 'II' }; | ||
| 40 | } | ||
| 41 | @ranking = sort { $b->{score} <=> $a->{score} } @ranking; | ||
| 42 | $self->sentences(\@ranking); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub as_string { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | my $text; | ||
| 49 | my @result; | ||
| 50 | for my $line (@{ $self->sentences }) { | ||
| 51 | push @result, $line; | ||
| 52 | $text .= "$line->{text}\n"; | ||
| 53 | last if length $text > $self->length; | ||
| 54 | } | ||
| 55 | |||
| 56 | if ($self->{sort} ne 'score') { | ||
| 57 | $text = ''; | ||
| 58 | for my $line (sort { $a->{line} <=> $b->{line} } @result) { | ||
| 59 | $text .= "$line->{text}\n"; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | my $len = $self->length; | ||
| 64 | $text =~ s/^(.{$len}).*$/$1\n/s; | ||
| 65 | $text; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; |