簡體   English   中英

如何將其打印到我的文件而不是我的Perl程序中的屏幕?

[英]How can I get this to print to my file instead of the screen in my Perl program?

我使用Lingua :: StopWords中的以下示例:

use Lingua::StopWords qw( getStopWords );
my $stopwords = getStopWords('en');

my @words = qw( i am the walrus goo goo g'joob );

# prints "walrus goo goo g'joob"
print join ' ', grep { !$stopwords->{$_} } @words;

如何讓它使用我的$document ,刪除停用詞並將結果打印到文件? 在這里查看我的代碼:

open(FILESOURCE, "sample.txt") or die("Unable to open requested file.");
my $document = <FILESOURCE>;
close (FILESOURCE);

open(TEST, "results_stopwords.txt") or die("Unable to open requested file.");

use Lingua::StopWords qw( getStopWords );
my $stopwords = getStopWords('en');

print join ' ', grep { !$stopwords->{$_} } $document;

我試過這些變化:

print join ' ', grep { !$stopwords->{$_} } TEST;


print TEST join ' ', grep { !$stopwords->{$_} } @words;

基本上,我如何讀取文檔,刪除停用詞然后將結果寫入新文件?

在您的程序中,您忘記將輸入文本標記為單詞。 Lingua::EN::Splitter::words的簡單替代方法是將空格上的一行split為單詞列表(大約)。

考慮到tchrist的評論,這個程序適合作為Unix過濾器。

use strictures;
use Lingua::StopWords qw(getStopWords);
use Lingua::EN::Splitter qw(words);
my $stopwords = getStopWords('en');
while (defined(my $line = <>)) {
    print join ' ', grep { !$stopwords->{$_} } @{ words $line };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM