0

我正在尝试检查某些术语是否出现在哈希键中,如果我用哈希值标记它们。我的代码识别出完全匹配但对于略有不同的术语失败。同样对于完全匹配,代码用所有值而不是相关值标记匹配的术语。

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($Drugs{$word})
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
    foreach $Bio(@array)
    {
        print "$Bio\n";
    }

我想要输出句子:

<tablet>tamoxifen</tablet> <tablet>4-hydroxytamoxifen</tablet> <tablet>tamoxifen-incoded<tablet> <capsule>pre-docotaxel<capsule> Drugs.(Here Drugs didn't match and hence it is left untagged)  
4

1 回答 1

2

您正在检查是否完全匹配,但根据您的预期,您必须针对 $key 正则表达式您的 $word

试试这个->

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }

更新


根据新要求

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    my $flag= 0; #using a flag, I am sure there are PLENTY of ways better than this :)
    for $keys(keys %Drugs)
    {

        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }

    }
    push (@array,$word) if $flag==0;
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }
于 2012-10-24T21:01:29.930 回答