2012-05-22 19 views
1

如何匹配同一單詞的不同實例。匹配同一單詞的不同實例

例如:如果字符串是協同作用。如何使其與協同作用,協同作用,協同作用,協同作用相匹配。 我可以寫下:

while(<IN>) 
{ 
chomp $_; 
my $line= $_; 
$word="Synergy"; 
if($line=~m/$word(\w+)/i) 
{ 
    $line=~s/$word/<Effect>$word<\/Effect>/ig; 
} 
} 
+1

看起來像你需要一個大的數據庫,以查找與其他單詞共享同一個根的單詞。我不知道任何算法都可以在任何情況下正確地得出一個單詞的根,因此我認爲沒有解決這個問題的方法,只能通過編寫代碼才能實現。 – cdhowie

回答

0

你會想要使用正則表達式。目前還不清楚你想要匹配的標準是什麼,但在你的例子中,所有的單詞都以「synergi」開頭,因此if($string =~ \bsynergi\w*\b)會在其中的任何地方找到包含「synergi」的所有行。

+0

謝謝,你可以檢查上面編輯的代碼...我試圖用協同,協同,協同,與上述標籤協同作用 – Mary

0

你可能想看看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"; 

延伸閱讀:Perldoc Text::Soundex

+1

或者你可以使用metaphone,它比soundex更好地處理不同的語言,但也是較少模糊。例如perl的lib:http://search.cpan.org/dist/Text-DoubleMetaphone/ – devsnd

+0

我已經編輯了上面的代碼,如果我必須用接近於上面標記的協同作用替換Words。 – Mary

+0

我想你會需要一個單詞字典在一個數據庫表中的單詞列和soundex列(或兩個如果使用雙重metaphone,這可能會更好)。這將允許soundex列上的自聯接查詢來獲取類似單詞的列表。 –

3

你可能想要做的就是所謂的詞幹什麼。 但是,爲了達到這個目的,您必須將文本中的所有單詞加上您要搜索的單詞。希望你列出的所有單詞都有相同的結果。我還沒有測試過。

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. 

現在這取決於你想要什麼。如果你想用某些東西來交換所有這些詞,你必須得到匹配詞的索引,並在文本中的相同位置進行替換。

相關問題