| 1 | package Lingua::ZH::Summary; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Net::YASA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Lingua::ZH::Summary - Extract summary from Chinese text | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.03 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Given a Chinese text, it will return the summary. Unlike Lingua-ZH-Summarize, | ||||||||||||||||||||||||||||
| 24 | this module depends on term frequency instead of knowledge. If knowledge | ||||||||||||||||||||||||||||
| 25 | analysis is required, use L<Lingua::ZH::Summarize> instead. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Perhaps a little code snippet. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use Lingua::ZH::Summary; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $foo = Lingua::ZH::Summary->new(); | ||||||||||||||||||||||||||||
| 32 | my $summary = $foo->summary(<FH> or $text); | ||||||||||||||||||||||||||||
| 33 | ... | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | my %punct = map { $_ => $_ } qw(。 ? ! ; …); | |||||||||||||||||||||||||||
| 38 | 3 | $punct{$_} = '。' for qw(, -); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | my %key = map { $_ => 1 } qw(是 會 曾 將 能 有); | |||||||||||||||||||||||||||
| 41 | my %stop = ( | ||||||||||||||||||||||||||||
| 42 | 3 | %key, map { $_ => 1 } qw( | |||||||||||||||||||||||||||
| 43 | 的 裡 和 與 及 年 月 日 時 分 秒 可 對 於 但 也 且 或 中 而 為 叫 | ||||||||||||||||||||||||||||
| 44 | ) | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 new | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 55 | 1 | my $self = { | |||||||||||||||||||||||||||
| 56 | yasa => undef | ||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my %arg = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | foreach (keys %arg) { | |||||||||||||||||||||||||||
| 62 | 0 | $self->{lc($_)} = $arg{$_}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 1 | $self->{yasa} = Net::YASA->new (minlength => 2); | |||||||||||||||||||||||||||
| 66 | 1 | bless($self, $class); | |||||||||||||||||||||||||||
| 67 | 1 | return($self); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 summary | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=14 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 1 | my ($text, %options) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | return unless $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Strip whitespace and formatting out of the text. | ||||||||||||||||||||||||||||
| 83 | 1 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 84 | 1 | $text =~ s/\s+/ /sg; | |||||||||||||||||||||||||||
| 85 | 1 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | unless (exists $options{maxlength} and $options{maxlength} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $options{"maxlength"} and $options{"maxlength"} > 0
MC/DC0%0/2
exists $options{"maxlength"} and $options{"maxlength"} > 0
exists $options{"maxlength"}
$options{"maxlength"} > 0
Truth table33%1/3
A and B
A= exists $options{"maxlength"}B= $options{"maxlength"} > 0
| |||||||||||||||||||||||||||||
| 88 | 1 | $options{maxlength} = log(length($text)) * 15; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | my $re = "(".join ("|",keys %punct).")"; | |||||||||||||||||||||||||||
| 92 | 1 | my @textlines = split /$re/,$text; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # First we get the meaningful terms with respect to their term frequency | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | my $summary = ''; | |||||||||||||||||||||||||||
| 97 | 1 | my $flag = 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $termset = $self->{yasa}->extract($text); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Get top 5 | ||||||||||||||||||||||||||||
| 102 | 0 | my @terms = map {s/\t.*//; $_;} (@{$termset}[0..4]); | |||||||||||||||||||||||||||
| 103 | 0 | $re = "(?:".join ("|",@terms).")"; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $i=0; | |||||||||||||||||||||||||||
| 106 | 0 | my $chunk; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | while (($chunk, my $punct) = (shift @textlines, shift @textlines) and defined($chunk)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
($chunk, my $punct) = (shift @textlines, shift @textlines) and defined $chunk
MC/DC0%0/2
($chunk, my $punct) = (shift @textlines, shift @textlines) and defined $chunk
($chunk, my $punct) = (shift @textlines, shift @textlines)
defined $chunk
Truth table0%0/3
A and B
A= ($chunk, my $punct) = (shift @textlines, shift @textlines)B= defined $chunk
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | ($flag = $punct{$punct}, next) unless $flag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $flag = $punct{$punct} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | next unless length($chunk) > 5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | next unless $chunk =~ /.+(?:\Q是\E|\Q會\E|\Q曾\E|\Q將\E|\Q能\E|\Q有\E|\Q為\E)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | next unless $chunk =~ /$re/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | next if $stop{substr($chunk, 0, 1)} or $stop{substr($chunk, -1)}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$stop{substr($chunk, 0, 1)} or $stop{substr $chunk, -1}
MC/DC0%0/2
$stop{substr($chunk, 0, 1)} or $stop{substr $chunk, -1}
$stop{substr($chunk, 0, 1)}
$stop{substr $chunk, -1}
Truth table0%0/3
A or B
A= $stop{substr($chunk, 0, 1)}B= $stop{substr $chunk, -1}
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $summary .= $chunk . $punct{$punct}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | last if length($summary) >= $options{maxlength}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | ### Done! Do any necessary postprocessing before returning. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return $summary; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | L<Lingua::ZH::Toke>, L<Lingua::ZH::Wrap>, L<Lingua::EN::Summary> | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Cheng-Lung Sung, C<< <clsung at cpan.org> >> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 BUGS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 137 | C<bug-lingua-zh-summary at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 138 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lingua-ZH-Summary>. | ||||||||||||||||||||||||||||
| 139 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||
| 140 | your bug as I make changes. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | perldoc Lingua::ZH::Summary | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | You can also look for information at: | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =over 4 | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | L<http://annocpan.org/dist/Lingua-ZH-Summary> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | L<http://cpanratings.perl.org/d/Lingua-ZH-Summary> | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-ZH-Summary> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | L<http://search.cpan.org/dist/Lingua-ZH-Summary> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =back | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Reference to the L<Lingua::ZH::Summarize> module from | ||||||||||||||||||||||||||||
| 173 | Audrey Tang E<lt>cpan@audreyt.orgE<gt>. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Copyright 2007-2009 Cheng-Lung Sung, all rights reserved. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 180 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | 1; # End of Lingua::ZH::Summary | |||||||||||||||||||||||||||