2011-12-13 62 views
1

有沒有更好的方法來匹配除這種方法以外的單詞,即時通訊試圖找到任何句子中出現的數組中的單詞。如何用Perl匹配一個句子中的順序詞?

my $count = 0; 
my @strings = (
    "i'm going to find the occurrence of two words going if possible", 
    "i'm going to find the occurrence of two words if impossible", 
    "to find a solution to this problem", 
    "i will try my best for a way to match this problem" 
); 
@neurot = qw(going match possible); 

my $com_neu = '\b'.join('\b|\b', @neurot).'\b'; 

foreach my $sentence (@string){ 

@l = $sentence =~ /($com_neu)/gi; 

foreach my $list (@l){ 
    if($list =~ m/\w['\w-]*/){ 
      print $list; 
     $count++; 
    } 
} 

print $count; 
} 

輸出:

String 1: going going possible 
String 2: going 
String 3: 
String 4: match 

,請幫助我更快的方法。

謝謝。

+2

對於初學者來說,你不需要'\ B'周圍的每一個字,就在括號:'\ B($ com_neu)\ B'。 – TLP

+1

你應該提供一些關於你的數據和句子的更多信息('@ neurot'中有多少單詞,句子多長......)。 – bvr

+1

m/\ w /將匹配所有與m/\ w ['\ w - ] */will相同的字符串。那麼['\ w - ] *部分的重點是什麼? – tadmc

回答

1

另一種方法可以是使用哈希來匹配的話:

my %neurot_hash = map { lc($_) => 1 } qw(going match possible); 

for my $sentence (@strings) { 
    for my $found (grep { $neurot_hash{ lc($_) } } $sentence =~ /\w['\w-]*/gi) { 
     print $found, " "; 
    } 
    print "\n"; 
} 

對於數據您提供的這種方法是約7%的速度。但請記住,數據集非常小,所以YMMV。

1

'智能匹配'運算符呢?

foreach my $elem (@neurot){ if(/$elem/i ~~ @strings){ print "Found $elem\n"; } }

+0

這使得不可能告訴哪個字符串包含什麼元素,對count沒有任何說法。另外,如果'@ neurot'是一種字典,這可能是無效的。 – bvr

+0

@bvr:你是對的'@神經'是字典,它將無效。 – aliocee

0

同爲超視距的答案,但也許更清潔

my %neurot_hash = map { lc($_) => 1 } qw(going match possible); 

for my $sentence (@strings) { 
    my @words = split /[^\w']/, $sentence; 
      #I am not sure if you want to take "i'm" as a separate word. 
      #Apparently, stackoverflow does not like '. 

    my @found = grep { exists $neurot_hash{ lc($_) } } @words; 
    print join (" ", @found); 
    print "\n"; 
} 
相關問題