File Coverage

blib/lib/WWW/Dict/TWMOE/Phrase.pm
Criterion Covered Total %
statement 33 68 48.5
branch 0 6 0.0
condition 0 5 0.0
subroutine 11 14 78.5
pod 3 3 100.0
total 47 96 48.9


line stmt bran cond sub pod time code
1             package WWW::Dict::TWMOE::Phrase;
2              
3 4     4   84973 use warnings;
  4         8  
  4         118  
4 4     4   23 use strict;
  4         7  
  4         117  
5 4     4   81 use v5.8.0;
  4         13  
  4         192  
6              
7 4     4   21 use base 'WWW::Dict';
  4         9  
  4         2881  
8              
9 4     4   4516 use HTML::TagParser;
  4         72965  
  4         145  
10 4     4   5955 use HTML::TableExtract;
  4         86440  
  4         182  
11              
12 4     4   6834 use WWW::Mechanize;
  4         757714  
  4         174  
13 4     4   45 use Encode;
  4         6  
  4         467  
14 4     4   3933 use Class::Field qw'field const';
  4         6853  
  4         1663  
15              
16             =head1 NAME
17              
18             WWW::Dict::TWMOE::Phrase - TWMOE Chinese Phrase Dictionary interface.
19              
20             =head1 VERSION
21              
22             Version 0.05
23              
24             =cut
25              
26             our $VERSION = '0.05';
27              
28             const dict_url => 'http://140.111.34.46/dict/';
29              
30             field ua => -init => 'WWW::Mechanize->new()';
31             field word => '';
32              
33             =head1 SYNOPSIS
34              
35             use WWW::Dict::TWMOE::Phrase;
36             use encoding 'utf8';
37             my $dict = WWW::Dict::TWMOE::Phrase->new();
38             my $definition = $dict->define("凡");
39              
40             =head1 METHODS
41              
42             =head2 define ($word)
43              
44             define() method look up the definition of $word from twmoe dict
45             server. The return value is an array of definitions, each definition
46             is a hash with 6 possible keys: "phrase", "zuin_form_1",
47             "zuin_form_2", "synonym", "antonym", "definition". The values to these
48             keys are directly copied from web server, except for "phrase", which
49             represent the actually phrase of this definition.
50              
51             =cut
52              
53             sub define {
54 0     0 1   my $self = shift;
55 0           my $word = shift;
56 0           my $def = [];
57 0           my $ua = $self->ua;
58              
59 0           $self->word($word);
60 0           $word = '^' . Encode::encode("big5",$word) . '$';
61              
62 0           $ua->get($self->dict_url);
63 0           $ua->submit_form( form_number => 1,
64             fields => {
65             QueryScope => "Name",
66             QueryCommand => "find",
67             GraphicWord => "yes",
68             QueryString => $word
69             }
70             );
71 0           my $content = $ua->content();
72 0           my $doc = HTML::TagParser->new( $ua->content() );
73 0           foreach my $elem ($doc->getElementsByTagName("a")) {
74 0           my $attr = $elem->attributes;
75 0 0         next unless ( $attr->{href} =~ /^GetContent.cgi/ );
76 0           $ua->get($attr->{href});
77 0           push @$def, $self->parse_content( Encode::decode('big5',$ua->content ));
78 0           $ua->back();
79             }
80 0           return $def;
81             }
82              
83             =head2 parse_content ($content)
84              
85             Parse the definition web page, with URI started with "GetContent.cgi".
86             The returned is a hash representing the word definition table no the
87             web page. This is intend to be used internally. You don't call this
88             function.
89              
90             =cut
91              
92             sub parse_content {
93 4     4   10458 use encoding 'utf8';
  4         10349  
  4         185  
94              
95 0     0 1   my $self = shift;
96 0           my $content = shift;
97 0           my $def = {};
98 0           my $te = HTML::TableExtract->new( keep_html => 0 );
99 0           $te->parse( $content );
100 0           for my $row ( $te->rows ) {
101             # The parsed result of HTML::TableExtract lost utf8 flag.
102 0   0       for(@$row) { Encode::_utf8_on($_||='') }
  0            
103 0 0         if ( $row->[0] =~ />(.*)
104 0           $row->[0] = $1;
105             }
106 0           $row->[0] = $self->inflect($row->[0]);
107 0 0         if ( $row->[0] =~ /【(.+)】/ ) {
108 0           $def->{phrase} = $1;
109             } else {
110 0           $def->{$row->[0]} = $row->[1];
111             }
112             }
113 0           return $def;
114             }
115              
116             =head2 inflect ($key)
117              
118             This is where the table field names converted to a proper ASCII name,
119             for it is easier to coding with. This is intend to be used internally.
120             You don't call this function.
121              
122             =cut
123              
124             sub inflect {
125 4     4   3542 use encoding 'utf8';
  4         9  
  4         16  
126              
127 0     0 1   my $self = shift;
128 0           my $key = shift;
129              
130             return {
131 0   0       "注音一式" => 'zuin_form_1',
132             "注音二式" => 'zuin_form_2',
133             "相似詞" => 'synonym',
134             "相反詞" => 'antonym',
135             "解釋" => 'definition'
136             }->{$key} || $key;
137             }
138              
139             =head1 AUTHOR
140              
141             Kang-min Liu, C<< >>
142              
143             =head1 BUGS
144              
145             Please report any bugs or feature requests to
146             C, or through the web
147             interface at
148             L.
149             I will be notified, and then you'll automatically be notified of
150             progress on your bug as I make changes.
151              
152             =head1 SUPPORT
153              
154             You can find documentation for this module with the perldoc command.
155              
156             perldoc WWW::Dict::TWMOE::Phrase
157              
158             You can also look for information at:
159              
160             =over 4
161              
162             =item * AnnoCPAN: Annotated CPAN documentation
163              
164             L
165              
166             =item * CPAN Ratings
167              
168             L
169              
170             =item * RT: CPAN's request tracker
171              
172             L
173              
174             =item * Search CPAN
175              
176             L
177              
178             =back
179              
180             =head1 COPYRIGHT & LICENSE
181              
182             Copyright 2006,2007 Kang-min Liu, all rights reserved.
183              
184             This program is free software; you can redistribute it and/or modify it
185             under the same terms as Perl itself.
186              
187             =cut
188              
189             1; # End of WWW::Dict::TWMOE::Phrase