Perl从字符串中删除StopWords

时间:2014-11-08 18:05:38

标签: regex perl stop-words

我正在使用这个脚本来删除Perl中的Stop-Words,我在Windows上运行而我找不到 兼容版本:

Lingua::EN::StopWordList
Lingua::StopWords qw(getStopWords)

我有一系列Stop-Words但是一旦我使用下面的REGEX,我就会丢失导致单词冲突的关键空格。 请注意,Stop-Word数组中的每个单词都有两个空格,一个在右边,一个在左边。

如何在不丢失关键空格的情况下有效删除停止词?

use strict;
use warnings;
use utf8;
use IO::File;
use String::Util 'trim';

my $inFile = "C:\\Users\\David\\Downloads\\InfoRet\\Explore the ways to get better grades.txt";
my $inFh = new IO::File $inFile, "r";
my $lineNum = 0;
my $line = undef;
my $loc = undef;
my $str = undef;

my @stopList = (" the ", " a ", " an ", " of ", " and ", " on ", " in ", " by ", " with ", " at ", " after ", " into ", " their ", " is ",  " that ", " they ", " for ", " to ", " it ", " them ", " which ");

for(my $i = 1; $i <= 4; $i++) {
    <$inFh>
}

while($line = <$inFh>) {
    $lineNum++;
    chomp $line;
    $line =~ s/[\$#@~!&*()\[\];.,:?^`\\\/]+//g;

    for my $planet (@stopList) {
        $loc = index($line, $planet);
        if($loc!=(-1)) {
            #$line =~ s/$str//g;
            $line =~ s/$planet//g;
        }
    }
    print "$line\n";
}

1 个答案:

答案 0 :(得分:3)

my @stopList = ("the", "a", "an", "of", ..);
my ($rx) = map qr/(?:$_)/, join "|", map qr/\b\Q$_\E\b/, @stopList;

以后,

$line =~ s/$rx//g;