| 1 | package Lingua::JA::Summarize::Extract; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw( Class::Accessor::Fast ); | ||
| 5 | __PACKAGE__->mk_accessors(qw/ text rate /); | ||
| 6 | |||
| 7 | use Carp (); | ||
| 8 | use UNIVERSAL::require; | ||
| 9 | |||
| 10 | our $VERSION = '0.02'; | ||
| 11 | |||
| 12 | use Lingua::JA::Summarize::Extract::ResultSet; | ||
| 13 | |||
| 14 | my %DefaultPlugins = ( | ||
| 15 | scoring => 'Scoring::Base', | ||
| 16 | parse => 'Parser::Ngram', | ||
| 17 | sentence => 'Sentence::Base', | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my $self = $class->SUPER::new(@_); | ||
| 23 | |||
| 24 | for my $plugin (@{ $self->{plugins} }) { | ||
| 25 | $self->add_plugin($plugin); | ||
| 26 | } | ||
| 27 | |||
| 28 | for my $method (keys %DefaultPlugins) { | ||
| 29 | $self->add_plugin($DefaultPlugins{$method}) unless $self->can($method); | ||
| 30 | } | ||
| 31 | |||
| 32 | $self->{rate} ||= 1; | ||
| 33 | |||
| 34 | $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub add_plugin { | ||
| 38 | my($self, $plugin) = @_; | ||
| 39 | my $class = ref $self; | ||
| 40 | |||
| 41 | my $package = ($plugin =~ /^\+(.+)$/) ? $1 : | ||
| 42 | sprintf '%s::Plugin::%s', $class, $plugin; | ||
| 43 | { | ||
| 44 | no strict 'refs'; | ||
| 45 | $package->require or Carp::croak($@); | ||
| 46 | unshift @{"$class\::ISA"}, $package; | ||
| 47 | } | ||
| 48 | $package->init($self); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub extract { | ||
| 52 | my($class, $text, @opt) = @_; | ||
| 53 | my $self = ref $class ? $class : $class->new(@opt); | ||
| 54 | |||
| 55 | utf8::decode($text); | ||
| 56 | $self->text($text) if $text; | ||
| 57 | |||
| 58 | Lingua::JA::Summarize::Extract::ResultSet->new({ | ||
| 59 | %{ $self }, | ||
| 60 | summary => $self->summarize || [], | ||
| 61 | sentences => $self->sentence || [], | ||
| 62 | }); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub summarize { | ||
| 66 | my($self, $text) = @_; | ||
| 67 | $self->text($text) if $text; | ||
| 68 | $self->scoring($self->parse); | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ |