2014-11-21 59 views
0

我匹配字符串中的多个模式来填充一个数组。输入文件看起来是这样的:当这个字符串与一个句子的一部分匹配时从数组中删除字符串 - 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"]] 
+0

回复“我需要删除的字符串数组中,当此字符串匹配的句子的一部分。”,你的输出似乎表明您反其道而行? – ikegami 2014-11-21 21:08:19

+0

1.对于每个数组,创建一个散列,其中的键是数组值。 (散列元素的值无关紧要。)2.将句子拆分为单词。 3.对于每个单词,对于每个散列,从散列中删除单词。 4.对于每个哈希,从哈希的关键字创建一个数组。 – ikegami 2014-11-21 21:12:12

回答

1

这会照你的要求去做。它只是使用grep和一个正则表达式来将每个列表缩小为仅出现在法语句子中的那些单词。

use utf8; 
use strict; 
use warnings; 
use 5.010; 
use autodie; 

use open qw/ :std :encoding(UTF-8) /; 

use Data::Dump; 

open my $fh, '<', 'sentences.txt'; 

while (<$fh>) { 

    my @sentences = split /\s*#\s*/; 
    next unless @sentences == 3; 

    print join(' # ', @sentences[0,1]), "\n"; 

    my @data = map [ split /;/ ], $sentences[0] =~/\[ ([^\[\]]+) \] /xg; 
    $_ = [ grep { $sentences[1] =~ /\b\Q$_\E\b/ } @$_ ] for @data; 

    dd \@data; 
    print "\n"; 
} 

输出

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\xE8re"]] 

更新

按照要求,该代码将修改这些词列表就地,使它们包含出现的话在翻译。

use utf8; 
use strict; 
use warnings; 
use 5.010; 
use autodie; 

use open qw/ :std :utf8 /; 

open my $fh, '<', 'sentences.txt'; 

while (<$fh>) { 

    my @sentences = split /\s*#\s*/; 
    next unless @sentences == 3; 

    print join(' # ', @sentences[0,1]), "\n"; 

    $sentences[0] =~ s{ \[ ([^\[\]]+) \] }{ 
    my @words = split /;/, $1; 
    @words = grep { $sentences[1] =~ /\b\Q$_\E\b/ } @words; 
    sprintf "[%s]", join ';', @words; 
    }exg; 

    print join(' # ', @sentences[0,1]), "\n\n"; 

} 

输出

I love cat [chats;chaton;chatterie] and rabbit [lapins;lapereau] # J'aime les chats et les lapins 
I love cat [chats] and rabbit [lapins] # J'aime les chats et les lapins 

My father [père;parent;papa] lives in New-York # Mon père vit à New-York 
My father [père] lives in New-York # Mon père vit à New-York 
+0

它运作良好。你认为我可以直接输出这个输出吗?我的父亲住在纽约纽约# – 2014-11-22 12:48:12

+0

@ChesterMcAllister:我已经加入了我的解决方案。如果你想为自己做出这些改变,那将是一个更加鼓舞人心的举动。与您可以期待自定义响应的论坛不同,Stack Overflow会将您的解决方案视为最不重要的读者。 – Borodin 2014-11-22 17:13:52

0

您还可以通过创建的法文句子的单词的哈希做到这一点。
这可能会更快,因为它避免了第三个正则表达式。

use strict; 
use warnings; 

while (<DATA>) { 
    my ($English, $French, $repl, %FrWords); 
    if (($English, $French) = m/^([^#]*)\#([^#]*)\#/) { 
     @FrWords{ split /\h+/, $French } = undef; 
     $English =~ s{ \[ ([^\[\]]*) \] }{ 
       $repl = join(';', grep { exists $FrWords{$_} } split /;/, $1); 
       '['. (length($repl) ? $repl : '') .']'; 
      }xeg; 
     print $English, '#', $French, "\n"; 
    } 
} 
__DATA__ 

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 

输出

I love cat [chats] and rabbit [lapins] # J'aime les chats et les lapins 
My father [père] lives in New-York # Mon père vit à New-York  
+0

它适用于我的示例数据,但在我的完整文件中,我可以将一个单词对应2个或更多单词。例如:'young ==> plus jeune' – 2014-11-24 11:56:01

+0

实际上,代码的确如下:'Younger [plus; jeune] father [père; parent; papa]#plus jeunepère#1.8 ==> Younger [plus; jeune] father [pΦre]#加jeunepΦre'。你的问题的真实性在于确定单词开始和结束的位置。除非你掌握了自然语言,否则你很难获得成功。 – sln 2014-11-24 17:07:57

相关问题