Perl 重新排列括号内的文本

Perl 重新排列括号内的文本,perl,Perl,我有以下的形态解析树,其中前缀、词干和后缀是标签,其他部分是变化,我需要重新排序的方式,前缀、词干和后缀在开始。例如, (S(un:prefix)(sell:stem))需要转换为(S(prefix:un)(stem:sell))。类似地,(S(S(in:prefix)(decipher:stem))(able:suffix))到(S(S(prefix:in)(stem:decipher))(suffix:able))保持结构也很重要。 我的perl代码还包括: use strict; use

我有以下的形态解析树,其中前缀、词干和后缀是标签,其他部分是变化,我需要重新排序的方式,前缀、词干和后缀在开始。例如,
(S(un:prefix)(sell:stem))
需要转换为
(S(prefix:un)(stem:sell))
。类似地,
(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;
}
使用严格;
使用“全部”警告;
使用列表::Util'reduce';
而(){
我的($word,$ss)=/\([^()]*)\)/gx;
my@ss=拆分“”,$ss;
my$str=reduce{sprintf'S(%S)(%S)',$a,$b}@ss;
printf“%s(%s)\n”,$str,$word;
}

它不能完成预期的任务。有什么问题吗?

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

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))

$str=~s/\([^\(\)]*)\:([^\(\)]*)\)/\($2\:$1\)/g
perl-pi-e的/\([^(]*):([^)]*)\/\($2:$1\)/g'input.txt
其中input.txt是您的文件,其中包含这些行suse file::Slurp$str=读取_文件('test.ptb')$str=~s/([^()]*)\:([^()]*)/($2\:$1)/g;打印“$str\n”