使用正则表达式Perl匹配单词的不同变体

时间:2012-10-25 20:50:30

标签: regex perl

我在单个空格字符处拆分句子,然后将这些术语与哈希键匹配。我只是在条款100%相似的情况下获得匹配,并且我正在努力寻找一个完美的正则表达式,可以匹配同一个单词的多次出现。例如。让我们考虑我有一个术语'拮抗',现在它与术语'拮抗'完全匹配,但不能与拮抗剂,拮抗剂或前拮抗剂,水拮抗剂等匹配。另外,我需要一个正则表达式来匹配像MCF这样的单词的出现-7用MCF7或MC-F7消除特殊字符等的影响。

这是我到现在为止的代码; thr评论部分是我在努力的地方。

(注意:散列中的术语源于单词的根形式。)

    use warnings;
    use strict;
    use Drug;
    use Stop;
    open IN,  "sample.txt"   or die "cannot find sample";
    open OUT, ">sample1.txt" or die "cannot find sample";

    while (<IN>) {
        chomp $_;
        my $flag = 0;
        my $line = lc $_;
        my @full = ();
        if ( $line =~ /<Sentence.*>(.*)<\/Sentence>/i ) {
            my $string = $1;
            chomp $string;
            $string =~ s/,/ , /g;
            $string =~ s/\./ \. /g;
            $string =~ s/;/ ; /g;
            $string =~ s/\(/ ( /g;
            $string =~ s/\)/ )/g;
            $string =~ s/\:/ : /g;
            $string =~ s/\::/ :: )/g;
            my @array = split / /, $string;

            foreach my $word (@array) {
                chomp $word;
                if ( $word =~ /\,|\;|\.|\(|\)/g ) {
                    push( @full, $word );
                }
                if ( $Stop_words{$word} ) {
                    push( @full, $word );
                }

                if ( $Values{$word} ) {
                    my $term = "<Drug>$word<\/Drug>";
                    push( @full, $term );
                }
                else {
                    push( @full, $word );
                }

                # if($word=~/.*\Q$Values{$word}\E/i)#Changed this
                # {
                # $term="<Drug>$word</$Drug>";
                # print $term,"\n";
                # push(@full,$term);
                # }
            }
        }
        my $mod_str = join( " ", @full );
        print OUT $mod_str, "\n";
    }

3 个答案:

答案 0 :(得分:3)

  

我需要一个正则表达式来匹配像MCF-7这样的单词出现与MCF7或   MC-F7

最直接的方法就是删除连字符,即

my $ignore_these = "[-_']"
$word =~ s{$ignore_these}{}g;

我不确定您的Value哈希中存储了什么,因此很难说出您期望发生的事情

if($word=~/.*\Q$Values{$word}\E/i)

然而,我想要你想要的东西是(简化你的代码)

#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use 5.10.0;
use Data::Dumper;

while (<>) {
    chomp $_;
    my $flag = 0;
    my $line = lc $_;
    my @full = ();
    if ( $line =~ /<Sentence.*>(.*)<\/Sentence>/i ) {
        my $string = $1;
        chomp $string;
        $string =~ s/([,\.;\(\)\:])/ $1 /g; # squished these together 
        $string =~ s/\:\:/ :: )/g;          # typo in original
        my @array = split /\s+/, $string;   # split on one /or more/ spaces

        foreach my $word (@array) {
            chomp $word;
                        my $term=$word;
                        my $word_chars = "[\\w\\-_']";
                        my $word_part  = "antagon";
                        if ($word =~ m{$word_chars*?$word_part$word_chars+}) {
                            $term="<Drug>$word</Drug>";
                        }
                        push(@full,$term); # push 

        }
    }
    my $mod_str = join( " ", @full );
        say "<Sentence>$mod_str</Sentence>";
}

这给了我以下输出,这是我对你期望的最佳猜测:

$ cat tmp.txt 
<Sentence>This in antagonizing the antagonist's antagonism pre-antagonistically.</Sentence>
$ cat tmp.txt | perl x.pl
<Sentence>this in <Drug>antagonizing</Drug> the <Drug>antagonist's</Drug> <Drug>antagonism</Drug> <Drug>pre-antagonistically</Drug> .</Sentence>
$ 

答案 1 :(得分:2)

perl -ne '$things{$1}++while s/([^ ;.,!?]*?antagon[^ ;.,!?]++)//;END{print "$_\n" for sort keys %things}' FILENAME

如果文件包含以下内容:

he was an antagonist
antagonize is a verb
why are you antagonizing her?
this is an alpha-antagonist

这将返回:

alpha-antagonist
antagonist
antagonize
antagonizing

以下是常规(非单行)版本:

#!/usr/bin/perl
use warnings;
use strict;
open my $in, "<", "sample.txt" or die "could not open sample.txt for reading!";
open my $out, ">", "sample1.txt" or die "could not open sample1.txt for writing!";

my %things;

while (<$in>){
    $things{$1}++ while s/([^ ;.,!?]*?antagon[^ ;.,!?]++)//
}

print $out "$_\n" for sort keys %things;

答案 2 :(得分:1)

您可能想要再看看您对方法的假设。听起来对我来说,你正在寻找与单词列表相距一定距离的单词。看看Levenshtein distance公式,看看这是否是你想要的。但请注意,计算这可能需要指数时间。