0

ファイルからストップワードを削除する方法について多くのフォームを読みました。私のコードは他の多くのものを削除しますが、ストップワードも含めたいと思います。これは私が到達した距離ですが、私は何が欠けているのかわかりません。ご意見をお聞かせください

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

chdir("c:/perl/input");
@files = <*>;

foreach $file (@files) 
  {
    open (input, $file);

    while (<input>) 
      {
        open (output,">>c:/perl/normalized/".$file);
    chomp;
    #####What should I write here to remove the stop words#####
    $_ =~s/<[^>]*>//g;
    $_ =~ s/\s\.//g;
    $_ =~ s/[[:punct:]]\.//g;
    if($_ =~ m/(\w{4,})\./)
    {
    $_ =~ s/\.//g;
    }
    $_ =~ s/^\.//g;
    $_ =~ s/,/' '/g;
    $_ =~ s/\(||\)||\\||\/||-||\'//g;

    print output "$_\n";

      }
   }

close (input);
close (output);
4

2 に答える 2

2

ストップワードは%$stopwords、値が1のキーです。

@stopwords = grep { $stopwords->{$_} } (keys %$stopwords);

ストップワードがのキーにすぎないこともあります%$stopwordsが、ドキュメントによるLingua::StopWordsと、キーに関連付けられている値も確認する必要があります。

ストップワードを取得したら、次のようなコードでストップワードを削除できます。

# remove all occurrences of @stopwords from $_

for my $w (@stopwords) {
  s/\b\Q$w\E\b//ig;
}

\Q...\Eストップワードに表示される可能性のある正規表現のメタ文字を引用するためにを使用していることに注意してください。ストップワードにメタ文字が含まれる可能性はほとんどありませんが、正規表現でリテラル文字列を表現する場合は、これに従うことをお勧めします。

\bまた、単語の境界を一致させるために使用します。これにより、別の単語の途中でストップワードが発生しないようにすることができます。うまくいけば、これはあなたのために働くでしょう-それはあなたの入力テキストがどのようなものであるかに大きく依存します-すなわちあなたは句読文字などを持っていますか?

于 2012-11-11T15:32:58.407 に答える
0
# Always use these in your Perl programs.
use strict;
use warnings;

use File::Basename qw(basename);
use Lingua::StopWords qw(getStopWords);

# It's often better to build scripts that take their input
# and output locations as command-line arguments rather than
# being hard-coded in the program.
my $input_dir   = shift @ARGV;
my $output_dir  = shift @ARGV;
my @input_files = glob "$input_dir/*";

# Convert the hash ref of stop words to a regular array.
# Also quote any regex characters in the stop words.
my @stop_words  = map quotemeta, keys %{getStopWords('en')};

for my $infile (@input_files){
    # Open both input and output files at the outset.
    # Your posted code reopened the output file for each line of input.
    my $fname   = basename $infile;
    my $outfile = "$output_dir/$fname";
    open(my $fh_in,  '<', $infile)  or die "$!: $infile";
    open(my $fh_out, '>', $outfile) or die "$!: $outfile";

    # Process the data: you need to iterate over all stop words
    # for each line of input.
    while (my $line = <$fh_in>){
        $line =~ s/\b$_\b//ig for @stop_words;
        print $fh_out $line;
    }

    # Close the files within the processing loop, not outside of it.
    close $fh_in;
    close $fh_out;
}
于 2012-11-11T15:58:52.940 に答える