perl中提示的错误,求指导
我运行了以下一段Perl程序,用来提取词根,错误提示是:Undefinedsubroutine&main::stemcalledatstem1.plline13,line...
我运行了以下一段Perl程序,用来提取词根,错误提示是:
Undefined subroutine &main::stem called at stem1.pl line 13,line 1.
不知道怎么回事,求指导,求修改!!
#!/usr/bin/perl
use 5.010;
use Lingua::Stem::Snowball;
open IN, "</Users/Agnes/Documents/words.txt" or die "Cannot open file $!\n";
open OUT1, ">/Users/Agnes/Documents/Stem.txt";
sub stem_words
{
my @stems = stem( 'en', \@words );
return @stem;
}
sub main
{
while(<IN>) {
if ($_) {
my @words = stem_words($_);
foreach (@words)
{
$_ =~ s/^\s+|\s+$//g; #去除头尾的空格
#say $_;
print OUT1 "$_\n";
}
}
}
close IN;
close OUT1;
}
main 展开
Undefined subroutine &main::stem called at stem1.pl line 13,line 1.
不知道怎么回事,求指导,求修改!!
#!/usr/bin/perl
use 5.010;
use Lingua::Stem::Snowball;
open IN, "</Users/Agnes/Documents/words.txt" or die "Cannot open file $!\n";
open OUT1, ">/Users/Agnes/Documents/Stem.txt";
sub stem_words
{
my @stems = stem( 'en', \@words );
return @stem;
}
sub main
{
while(<IN>) {
if ($_) {
my @words = stem_words($_);
foreach (@words)
{
$_ =~ s/^\s+|\s+$//g; #去除头尾的空格
#say $_;
print OUT1 "$_\n";
}
}
}
close IN;
close OUT1;
}
main 展开
展开全部
首先
use Lingua::Stem::Snowball;
要改成
use Lingua::Stem::Snowball qw/stem/;
因为 stem 这涵数在 Snowball 这模块中并不是自动 export 出来给你用的
另外, 在 stem_words 这涵数中, 你要回传的是 @stems, 而不是 @stem
最後, 在 perl 中没必/需要写一个叫 main 的 sub ( 这也不是好习惯 ) , 直接写代码就行了, perl 也不需要像其他语言需要 "进入点"
追问
你好,非常感谢你的回答,我按照你说的修改之后没有错误提示了,但是输出文本里面没有结果。我输入的文本words.txt里面的内容是
hello
world
i
am
a
student
i'm
the
girls
how
are
you
hey
do
you
want
to
be
a
coder
想请问一下是什么原因呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/usr/bin/perl
use 5.010;
use Lingua::Stem::Snowball qw/stem/;
use Lingua::StopWords qw( getStopWords );
open IN, "<./sentence.txt" or die "Cannot open file $!\n";
open OUT1, ">./result.txt";
while(<IN>)
{
my $stopwords = getStopWords('en');
my @words=$_=~/(\S+)/g;
@words=grep { !$stopwords->{$_} } @words;
#print @words;
my $stemmer = Lingua::Stem::Snowball->new( lang => 'en' );
$stemmer->stem_in_place( \@words );
my @stems = stem( 'en', \@words );
print join " ",@stems;
print "\n";
print OUT1 join " ",@stems;
print OUT1 "\n";
}
use 5.010;
use Lingua::Stem::Snowball qw/stem/;
use Lingua::StopWords qw( getStopWords );
open IN, "<./sentence.txt" or die "Cannot open file $!\n";
open OUT1, ">./result.txt";
while(<IN>)
{
my $stopwords = getStopWords('en');
my @words=$_=~/(\S+)/g;
@words=grep { !$stopwords->{$_} } @words;
#print @words;
my $stemmer = Lingua::Stem::Snowball->new( lang => 'en' );
$stemmer->stem_in_place( \@words );
my @stems = stem( 'en', \@words );
print join " ",@stems;
print "\n";
print OUT1 join " ",@stems;
print OUT1 "\n";
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询