我匹配字符串中的多个模式来填充一个数组。输入文件看起来是这样的:当这个字符串与一个句子的一部分匹配时从数组中删除字符串 - Perl
I love cat [chats;chaton;chatterie] and rabbit [lapins;lapereau] # J'aime les chats et les lapins # 2.8
My father [père;parent;papa] lives in New-York # Mon père vit à New-York # 1.8
我用这个代码:
use strict;
use warnings;
use Data::Dump;
open(TEXT, "<", "$ARGV[0]")
or die "cannot open < $ARGV[0]: $!";
while(my $text = <TEXT>)
{
my @lines = split /\n/, $text;
foreach my $line (@lines) {
if ($line =~ /(^(.+)\t(.+)\t(.+)$)/){
my $english_sentence = $2;
my $french_sentence = $3;
my $score = $4;
print $english_sentence."#".$french_sentence."";
my @data = map [ split /;/ ], $line =~/\[ ([^\[\]]+) \] /xg;
dd \@data;
}
print "\n";
}
}
close TEXT;
这里是输出:
I love cat [chats;chaton;chatterie] and rabbit [lapins;lapereau] # J'aime les chats et les lapins
Array==>[["chats", "chaton", "chatterie"], ["lapins", "lapereau"]]
My father [père;parent;papa] lives in New-York # Mon père vit à New-York
Array==>[["père", "parent", "papa"]]
我需要在此字符串删除数组中的字符串与句子的一部分相匹配。最后,我想要得到这样的结果:
I love cat [chats;chaton;chatterie] and rabbit [lapins;lapereau] # J'aime les chats et les lapins
[["chats"], ["lapins"]]
My father [père;parent;papa] lives in New-York # Mon père vit à New-York
[["père"]]
回复“我需要删除的字符串数组中,当此字符串匹配的句子的一部分。”,你的输出似乎表明您反其道而行? – ikegami 2014-11-21 21:08:19
1.对于每个数组,创建一个散列,其中的键是数组值。 (散列元素的值无关紧要。)2.将句子拆分为单词。 3.对于每个单词,对于每个散列,从散列中删除单词。 4.对于每个哈希,从哈希的关键字创建一个数组。 – ikegami 2014-11-21 21:12:12