如何使用 Perl 匹配句子中的连续单词?

发布于 2024-12-20 17:36:20 字数 779 浏览 0 评论 0原文

除了此方法之外,是否有更好的方法来匹配单词,我正在尝试查找数组中出现在任何句子中的单词。

 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

请帮助我更快的方法。

谢谢。

Is there better way to match words other than this method, im trying to find the word in the array that occur in any of the sentences.

 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;
 }

Output:

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

please help me with a faster way.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-12-27 17:36:20

另一种方法可能是使用散列来匹配单词:

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。

Another approach could be to use hash to match the words:

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";
}

For data you provided this method is ~ 7% faster. But keep in mind that the data set is very small, so YMMV.

撑一把青伞 2024-12-27 17:36:20

“智能匹配”运算符怎么样?

foreach 我的 $elem (@neurot){
if(/$elem/i ~~ @strings){
打印“找到$ elem \ n”;
}
}

what about the 'smart-match' operator?

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

猫瑾少女 2024-12-27 17:36:20

与 bvr 答案相同,但可能更清晰

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";
}

The same as bvr answer, but perhaps cleaner

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文