微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl从网页分析出英文单词

 1 #!/usr/bin/perl
 2 use Inline CPP;
 3 package MyParser;
 4 use base "HTML::Parser";
 5
 6 my $src='src.htm';
 7 my $allow=0;
 8 my %hashwords;
 9 my @arraywords;
10
11 sub trim
12 {
13         my $str = $_[0];
14         $str =~ s/^/s+//o;
15         $str =~ s//s+$//o;
16         return $str;
17 }
18
19 sub start
20 {
21         my ($self,$tagname,$attr,$attrseq,$text) = @_;
22         if($tagname eq 'script' or $tagname eq 'a')
23         {
24                 $allow=0;
25         }
26         else
27         {
28                 $allow=1;
29         }
30 }
31
32 sub text
33 {
34         my ($self,$text) =@_;
35         $text=trim($text);
36         if($allow and $text)
37         {
38                 my @words=split(/[/s,/./)/(/':;/"]+/o,$text);
39                 foreach my $word(@words)
40                 {
41                         $word=lc($word);
42                         if($word=~m/^[a-z]+$/o)
43                         {
44                                 if(not exists %hashwords->{$word})
45                                 {
46                                         %hashwords->{$word}=0;
47                                         push(@arraywords,$word);
48                                 }
49                         }
50                 }
51         }
52 }
53
54 %hashwords=();
55 my $p = MyParser->new;
56 $p->parse_file($src);
57 @arraywords=sort(@arraywords);
58 foreach my $word(@arraywords)
59 {
60         print "$word/n";
61 }
62

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐