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