【发布时间】:2023-03-07 06:00:01
【问题描述】:
我已经将一些 Perl 脚本拼凑在一起,旨在从一批文档中提取每个单词,消除所有停用词,对剩余的词进行词干化,并创建一个包含每个词干化词及其出现频率的哈希。但是,在处理了几分钟后,我得到了“内存不足!”命令窗口中的消息。有没有更有效的方法来达到预期的效果,还是我只需要找到一种方法来访问更多的内存?
#!/usr/bin/perl
use strict;
use warnings;
use Lingua::EN::StopWords qw(%StopWords);
use Lingua::Stem qw(stem);
use Mojo::DOM;
my $path = "U:/Perl/risk disclosures/2006-28";
chdir($path) or die "Cant chdir to $path $!";
# This program counts the total number of unique sentences in a 10-K and enumerates the frequency of each one.
my @sequence;
my %sequences;
my $fh;
# Opening each file and reading its contents.
for my $file (<*.htm>) {
my $data = do {
open my $fh, '<', $file;
local $/; # Slurp mode
<$fh>;
};
my $dom = Mojo::DOM->new($data);
my $text = $dom->all_text();
for ( split /\s+/, $text ) {
# Here eliminating stop words.
while ( !$StopWords{$_} ) {
# Here retaining only the word stem.
my $stemmed_word = stem($_);
++$sequences{"$stemmed_word"};
}
}
}
【问题讨论】:
-
我认为您需要将
while (!$StopWords{$_}) { ... }更改为next if defined $StopWords{$_};。您已经使用for (split ...)一次检查一个词,因此该词要么是停用词,要么不是,不需要第二个循环。 -
是的,确实消除了“内存不足”错误消息,谢谢!
标签: performance perl memory stemming stop-words