| 1 | package Lingua::EN::Keywords::Yahoo; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use XML::Twig; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use base qw(Exporter); | ||
| 7 | |||
| 8 | our $VERSION = "0.5"; | ||
| 9 | our @EXPORT_OK = qw(keywords); | ||
| 10 | |||
| 11 | our $ua; | ||
| 12 | our $uri = "http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction"; | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | $ua = LWP::UserAgent->new(); | ||
| 16 | }; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Lingua::EN::Keywords::Yahoo - Automatically extracts keywords from text using the Yahoo! API | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use Lingua::EN::Keywords::Yahoo qw(keywords); | ||
| 25 | |||
| 26 | my @keywords = keywords($text); | ||
| 27 | |||
| 28 | or | ||
| 29 | |||
| 30 | my @keywords = keywords($text, $query); | ||
| 31 | |||
| 32 | Where C<$query> is an optional term to help with the extraction process. | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This uses the Yahoo! keywords API to extract keywords from | ||
| 37 | text. | ||
| 38 | |||
| 39 | To quote the Yahoo! page: "The Term Extraction Web Service provides a | ||
| 40 | list of significant words or phrases extracted from a larger content." | ||
| 41 | |||
| 42 | =head1 EXPORT | ||
| 43 | |||
| 44 | Can export the C<keywords> subroutine. | ||
| 45 | |||
| 46 | =head1 AUTHOR | ||
| 47 | |||
| 48 | Original code by Simon Cozens, <simon@cpan.org> | ||
| 49 | |||
| 50 | Packaged by Simon Wistow, <simon@thegestalt.org> | ||
| 51 | |||
| 52 | =head1 COPYRIGHT | ||
| 53 | |||
| 54 | Released under the same terms as Perl itself. | ||
| 55 | |||
| 56 | =head1 SEE ALSO | ||
| 57 | |||
| 58 | The Term Extraction API: | ||
| 59 | http://developer.yahoo.net/search/content/V1/termExtraction.html | ||
| 60 | |||
| 61 | L<Yahoo::Search> | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | sub keywords { | ||
| 68 | my $content = shift; | ||
| 69 | |||
| 70 | my $q = { | ||
| 71 | appid => "Perl::Lingua::EN::Keywords::Yahoo", | ||
| 72 | context => $content | ||
| 73 | }; | ||
| 74 | |||
| 75 | $q->{query} = shift if @_; | ||
| 76 | |||
| 77 | my $resp = $ua->post($uri, $q); | ||
| 78 | my @terms; | ||
| 79 | if ($resp->is_success) { | ||
| 80 | my $xmlt = XML::Twig->new( index => [ "Result" ] ); | ||
| 81 | $xmlt->parse($resp->content); | ||
| 82 | for my $result (@{ $xmlt->index("Result") || []}) { | ||
| 83 | push @terms, $result->text; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return @terms; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | |||