0
我正在研究将句子拆分为单个单词的代码,然后使用散列键搜索单词以查找它们的存在。我的代码返回100%相同的条件,匹配后,我使用与匹配键相对应的值标记句子中的单词。问题是代码标签术语,但随机值不符合我的预期。此外,在某些情况下,术语和散列键相似但不是100%相同,我如何编写正则表达式来匹配我的术语和键。 注意:我已经将哈希键设置为它们的根形式。我提供了一些例子:如果句子中的术语是协同或反协同的,而我的哈希键是Synerg,那么我怎样才能将上述术语与Synerg相匹配。Perl哈希和正则表达式
我的代码如下:
open IN, "C:\\Users\\Desktop\\TM\\clean_cells.txt" or die "import file absent";
my %hash=();
use Tie::IxHash;
tie %hash => "Tie::IxHash";
while(<IN>)
{
chomp $_;
$line=lc $_;
@Organs=split/\t/, $line;
$hash{$Organs[0]}=$Organs[1];
}
$Sentence="Lymphoma is Lymph Heart and Lung";
@list=split/ /,$Sentence;
@array=();
foreach $term(@list)
{
chomp $term;
for $keys(keys %hash)
{
if($hash{$term})
{
$cell="<$hash{$keys}>$term<\/$hash{$keys}>";
push(@array, $cell);
}
elsif($term=~m/\b\Q$keys(\w+)\E\b/)
{
$cell="<$hash{$keys}>$term<\/$hash{$keys}>";
push(@array, $cell);
}
elsif($term=~m/\b\Q(\w+)$keys\E\b/)
{
$cell="<$hash{$keys}>$term<\/$hash{$keys}>";
push(@array, $cell);
}
elsif($term=~m/\b\Q(\w+)$keys(\w+)\E\b/)
{
$cell="<$hash{$keys}>$term<\/$hash{$keys}>";
push(@array, $cell);
}
}
}
print @array;
for example: hash looks like this: %hash={
TF1 => Lymph
Thoracic_duct => Lymph
SK-MEL-1 => Lymph
Brain => Brain
Cerebellum => Brain
};
So if the term TF1 is found it should be substituted to Lymph TF1 /Lymph