我匹配字符串中的多個模式來填充一個數組。輸入文件看起來是這樣的:當這個字符串與一個句子的一部分匹配時從數組中刪除字符串 - 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