重新排序括号内的文本

时间:2017-03-15 13:43:39

标签: perl

我有以下方式的形态解析树,其中前缀,词干和后缀是标签,其他部分是我需要以开头的前缀,词干和后缀的方式重新排序。例如,  (S (un:prefix) (sold:stem))需要转换为(S (prefix:un) (stem:sold))。同样,(S (S (in:prefix) (decipher:stem)) (able:suffix))(S (S (prefix:in) (stem:decipher)) (suffix:able))保持结构也很重要。

我的perl代码也是:

use strict;
use warnings 'all';

use List::Util 'reduce';

while ( <> ) {

    my ($word, $ss) = / \( ( [^()]* ) \) /gx;

    my @ss = split ' ', $ss;

    my $str = reduce { sprintf 'S (%s) (%s)', $a, $b } @ss;

    printf "%s (%s)\n", $str, $word;
}

它没有完成预期的任务。有什么问题?

1 个答案:

答案 0 :(得分:1)

如果我对你的问题理解正确,简单的正则表达式可以替换后缀和前缀。

my $str ="(S (un:prefix) (sold:stem))
(S (S (in:prefix) (decipher:stem)) (able:suffix)) ";

$str=~s/\(([^\(\)]*)\:([^\(\)]*)\)/\($2\:$1\)/g;

print "$str\n";

输出:

(S (prefix:un) (stem:sold))
(S (S (prefix:in) (stem:decipher)) (suffix:able))