1

我在单个空格字符处拆分句子,然后将这些术语与哈希键进行匹配。只有当术语 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";
    }
4

3 回答 3

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>
$ 
于 2012-10-25T22:06:42.717 回答
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;
于 2012-10-25T21:18:50.483 回答
1

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

于 2012-10-25T20:59:25.483 回答