如何匹配同一單詞的不同實例。匹配同一單詞的不同實例
例如:如果字符串是協同作用。如何使其與協同作用,協同作用,協同作用,協同作用相匹配。 我可以寫下:
while(<IN>)
{
chomp $_;
my $line= $_;
$word="Synergy";
if($line=~m/$word(\w+)/i)
{
$line=~s/$word/<Effect>$word<\/Effect>/ig;
}
}
如何匹配同一單詞的不同實例。匹配同一單詞的不同實例
例如:如果字符串是協同作用。如何使其與協同作用,協同作用,協同作用,協同作用相匹配。 我可以寫下:
while(<IN>)
{
chomp $_;
my $line= $_;
$word="Synergy";
if($line=~m/$word(\w+)/i)
{
$line=~s/$word/<Effect>$word<\/Effect>/ig;
}
}
你會想要使用正則表達式。目前還不清楚你想要匹配的標準是什麼,但在你的例子中,所有的單詞都以「synergi」開頭,因此if($string =~ \bsynergi\w*\b)
會在其中的任何地方找到包含「synergi」的所有行。
謝謝,你可以檢查上面編輯的代碼...我試圖用協同,協同,協同,與上述標籤協同作用 – Mary
你可能想看看Text :: Soundex。例如,
use Text::Soundex;
# The following all return S562
print soundex("synergizes"), "\n";
print soundex("synergism"), "\n";
print soundex("synergically"), "\n";
print soundex("synergistic"), "\n";
你可能想要做的就是所謂的詞幹什麼。 但是,爲了達到這個目的,您必須將文本中的所有單詞加上您要搜索的單詞。希望你列出的所有單詞都有相同的結果。我還沒有測試過。
use Lingua::Stem;
my $stemmer = Lingua::Stem->new(-locale => 'EN-UK');
# first convert text to list of words
my @words;
while(<IN>) {
push @words, split(/\b/, $_); # you can do better here
}
# now stem all words.
my $stemmed_words = $stemmer->stem(@words);
# results in an array ref of stems in the same order as the words have been.
# now stem your search
my $stemmed_search = $stemmer->stem($word);
# and do the search from above inside stemmed array.
現在這取決於你想要什麼。如果你想用某些東西來交換所有這些詞,你必須得到匹配詞的索引,並在文本中的相同位置進行替換。
看起來像你需要一個大的數據庫,以查找與其他單詞共享同一個根的單詞。我不知道任何算法都可以在任何情況下正確地得出一個單詞的根,因此我認爲沒有解決這個問題的方法,只能通過編寫代碼才能實現。 – cdhowie