2011-12-13 65 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"; 
} 
相关问题