gpt4 book ai didi

regex - 使用 Perl,使用 HashMap 在数组中搜索单词

转载 作者:行者123 更新时间:2023-12-01 09:08:32 25 4
gpt4 key购买 nike

我正在尝试使用散列映射在数组中搜索单词,如下例所示,但没有找到。下面的代码会将一本书读入@bookArray。然后,我想搜索@bookArray 来找到某个词。书中的单词可以被其他单词无限制地包围。

open( SEARCHWORDS, "< $encoding", $_folder . "searchwords.txt" ) or die("Input file not found.\n");
open( BOOK, "< $encoding", $_folder . "book.txt" ) or die("Input file not found.\n");

while ( $_ = <BOOK> ) {
push @bookArray, $_;
}
my %thebook = map {$_ => 1} @bookArray;

while ( my $searchWords = <SEARCHWORDS> ) {

if (exists $thebook{$searchWords}) {
print "yeppie";
}
}

#example of words in Book "I want to go to the store andbuy some food";
#example of search words "buy";

最佳答案

更新 已阐明目标是识别具有给定单词的句子。


您的 %thebook 中的每个键hash 是书中的整行。因此,当您搜索一个单词的键时,它不存在。

用你已有的词来识别一行的简单方法

foreach my $line (@bookArray)
{
while (my $searchWords = <SEARCHWORDS>)
{
chomp $searchWords;
if ($line =~ /($searchWords)/) {
print "Found $1 in: $line\n";
}
}
}

这是低效的,因为在每一行中搜索所有单词,并且在书籍文件已经处理之后。但对于给定的目标,它可能仍然是可以接受的。

最好翻过来,在阅读该文件时在书中的每一行中查找单词。

use warnings 'all';
use strict;

my $words_file = '...';
my $book_file = '...';

open my $w_fh, '<', $words_file or die "Can't open $words_file: $!";
my @search_words = <$w_fh>;
close $w_fh;
chomp(@search_words);

open my $bk_fh, '<', $book_file or die "Can't open $book_file: $!";

while (my $line = <$bk_fh>)
{
chomp $line;
foreach my $word (@search_words)
{
if ($line =~ /$word/)
{
print "Found $word in line $.: $line\n";
}
}
}

每当找到一个单词时,它就会打印在它出现的每一行上。

您还可以构建书中单词的哈希值,其中每个单词都是一个键,其值是一个arrayref(数组引用),其中包含找到它的行号。

遍历行并将每一行分解为单词,将它们添加为键。对于每个键,将该行的编号 ($.) 添加到其 arrayref 值。即使在同一行中重复发现该单词,也会添加该数字,这是多行出现的合理记录。如果不需要,这很容易改变。我们还将行存储在数组中。

while (<$bk_fh>) { 
push @bookArray, $_;
push @{$book_word{$_}}, $. for split;
}

say "$_ => [ @{$freq{$_}} ]" for sort keys %freq; # print all (long!)

split默认拆分 $_通过空格,和 <>运算符默认分配给 $_ .所以for split遍历行中的单词,做 push ...对于每个。 $.是当前正在从 $bk_fh 读取的行的行号.

如果这个词已经被看到并且作为键存在那么push只需将此行的编号添加到作为该键值的 arrayref。

Perl 的 autovivification 使遇到新词时变得简单。当表达式 $book{$_}使用新词(在 $_ 变量中)自动添加为键。同样,@{$book{$_}} expression 引用一个 arrayref 作为新词键的值,因此它创建了 arrayref。然后 push$.到它上面。我们不必首先手动创建这些。

参见 perlreftut供引用和perldsc用于复杂的数据结构。

然后你可以使用exists来检查每个单词。 , 如果它确实存在,则使用该单词的值打印数组中的行,这是该行在数组中的索引。


原帖

你可以把书中的每一行分解成单词,然后将其输入一个散列,一个大散列

while ( <BOOK> ) {        
chomp;
push @bookArray, split;
}
my %thebook = map {$_ => 1} @bookArray; # potentially very big

默认拆分 $_通过空格,而 <>默认分配给 $_ .

或者,如果您还想保留整行的数组,请对哈希执行此操作

my @bookArray = <BOOK>;
chomp @bookArray;

my %thebook = map {$_ => 1} map { split } @bookArray; # potentially very big

然后是个别词$searchWords将(可能)成为 key 。此外,行必须chomp -ed 删除换行符。否则有些词会有它,有些则没有。

我想补充一点——你们为什么用这种方式打开文件?关于

my $bookfile = $_folder . 'book.txt';
open my $bk_fh, '<', $bookfile or die "Can't open $bookfile: $!";

此外,是 $_folder确实是一个包含文件夹名称的变量?对于变量名来说,这是一个冒险的选择。

关于regex - 使用 Perl,使用 HashMap 在数组中搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com