gpt4 book ai didi

string - 连接字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:29 25 4
gpt4 key购买 nike

我的算法设计有一个大问题,因为我使用了大文本文件。我有一个包含单词序列的文本文件。例如

  1. 我的 friend 们
  2. 你好我的 friend 们
  3. 世界

第二个文件很大(千兆字节),包含句子。该程序的目标是逐字逐句地查看单词(第一个文件)并在第二个文件中查找连接符号“+”

例如“hello my friends of the world”作为输入”成为“你好 + 我的 + friend 们 the+world

有什么想法吗?我想用 Perl 编写它,它可以处理文本

我已经在 Perl 中完成了这个脚本,但是它太慢了,因为它多次读取文件..:(这是一个 Perl 程序的例子,它可以工作,但是太慢了

use strict;
use warnings;
use utf8;
use feature qw(:5.10);
my ($in, $dico) = @ARGV;
die "Bad infile $in" if !-r $in;
die "Bad dicofile $dico" if !-r $dico;

# load dico
my @dico;
open(FICHIERNOUVELLES, ">resultat7.txt");
open my $DICO, "<", $dico or die "Can't open $dico for reading: $!\n";
# For all lines in the Dico
foreach my $line (<$DICO>) {
chomp($line);
# extract words
if (my @word = split /\s+/, $line) {

my $re = q{(^\s*|\s+)(}.(join q(\s+), map quotemeta, @word).q{)(\s+|\s*$)};

push @dico, qr/$re/;
}
}

open my $IN, "<", $in or die "Can't open $in for reading: $!\n";
my @word;

foreach my $line (<$IN>) {

foreach my $dico (@dico) {

while (my (undef, $sequence) = $line =~ /$dico/) {

$sequence =~ s/\s+/+/g;
$line =~ s/$dico/$1$sequence$3/;
}
}
print FICHIERNOUVELLES "$line";

}
close(FICHIERNOUVELLES);

最佳答案

不多次读取第二个文件的解决方案是先从文件1中读取单词集,并存储在数据结构中。

use File::Slurp;
my @lines = read_file($filename1);
my %replacements = map { my $c = $_; $c =~ s/ / + /g; ( $_ => $c ) } @lines;

open (my $file2, "<", $filename2) or die "$!";
while (<$file2>) {
chomp;
foreach my $replacement (keys %replacements) {
s/$replacement/$replacements{$replacement}/g;
}
print $_;
}

关于string - 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15659839/

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