1

我创建了一个 Perl 文件来加载“停用词”数组。

然后我加载一个包含“.ner”文件的目录。每个文件都被打开,每个单词都被拆分并与停止文件中的单词进行比较。如果单词与单词匹配,则将其更改为“”(什么都没有-并被删除),然后我将文件复制到另一个位置。所以我可以区分有停用词的文件和没有停用词的文件。但这会将文件更改为现在不包含停用词还是会恢复为原始文件?

#!/usr/bin/perl

#use strict;
#use warnings;

my @stops;
my @file;

use File::Copy;

open( STOPWORD, "/Users/jen/stopWordList.txt" ) or die "Can't Open: $!\n";

@stops = <STOPWORD>;
while (<STOPWORD>)    #read each line into $_
{
    chomp @stops;     # Remove newline from $_
    push @stops, $_;  # add the line to @triggers
}

close STOPWORD;

$dirtoget="/Users/jen/temp/";

opendir(IMD, $dirtoget) || die("Cannot open directory");

@thefiles= readdir(IMD);

foreach $f (@thefiles){
    if ($f =~ m/\.ner$/){
        print $f,"\n";

        open (FILE, "/Users/jen/temp/$f")or die"Cannot open FILE"; 

        if ( FILE eq "" ) {
            close FILE;
        }
        else{
            while (<FILE>) {

               foreach $word(split(/\|/)){

                    foreach $x (@stops) {
                       if  ($x =~ m/\b\Q$word\E\b/) {
                            $word = '';   
             copy("/Users/jen/temp/$f","/Users/jen/correct/$f")or die "Copy failed: $!";
                    close FILE;
                    } 
                    }
                }
            }
        }
    }
}
closedir(IMD);
exit 0;

我正在拆分和比较的文件格式如下:

'<title>|NN|O Woman|NNP|O jumped|VBD|O for|IN|O life|NN|O after|IN|O firebomb|NN|O attack|NN|O -|:|O National|NNP|I-ORG News|NNP|I-ORG ,|,|I-ORG Frontpage|NNP|I-ORG -|:|I-ORG Independent.ie</title>|NNP|'

我应该概述应该在哪里拆分单词,即:split(/|/)?

4

3 回答 3

2

你应该总是使用 : use strict; 使用警告;

使用三个 args 打开并测试打开是否失败。

正如 codaddict 所说A split with no arguments is equivalent to split(' ', $_).

这是完成这项工作的建议(据我所知,您想要什么)。

#!/usr/bin/perl
use strict;
use warnings;
use 5.10.1;

my @stops = qw(put here your stop words);
my %stops = map{$_ => 1} @stops;

my @thefiles;

my $path = '/Users/jen/temp/';
my $out = $path.'outputfile';
open my $fout, '>', $out or die "can't open '$out' for writing : $!";

foreach my $file(@thefiles) {
    next unless $file =~ /\.ner$/;
    open my $fh, '<', $path.$file or die "can't open '$file' for reading : $!";
    my @lines = <$file>;
    close $fh;
    foreach my $line(@lines) {
        my @words = split/\|/,$line;
        foreach my $word(@words) {
            $word = '' if exists $stops{$word};
        }
        print $fout join '|',@words;
    }
}
close $out;
于 2011-03-02T13:05:32.130 回答
1

split不带参数的A等价于split(' ', $_)

由于您希望将行拆分,因此|您需要执行以下操作:

split/\|/
于 2011-03-02T11:58:54.563 回答
0

@jenniem001,

open FILE, ("<$fh")||die("cant");undef $/;my $whole_file = <FILE>;foreach my $word (@words){$whole_file=~s/\b\Q$word\E\b//ig;}open FILE (">>$duplicate")||die("cant");print FILE $whole_file;

这将从您的文件中删除停靠点并创建一个副本。只需打电话给 $duplicate 一个名字 :)

于 2011-03-08T16:46:57.503 回答