在分隔符之间随机化文本

kem*_*ski 6 shell perl text-processing text-parsing

我有这个简单的输入

I have {red;green;orange} fruit and cup of {tea;coffee;juice}
Run Code Online (Sandbox Code Playgroud)

我使用Perl两个外部支架之间的分隔符标识模式{},和随机内部与内部分隔符的字段;.

我得到了这个输出

I have green fruit and cup of coffee
Run Code Online (Sandbox Code Playgroud)

这是我工作的Perl脚本

perl -plE 's!\{(.*?)\}!@x=split/;/,$1;$x[rand@x]!ge' <<< 'I have {red;green;orange} fruit and cup of {tea;coffee;juice}'
Run Code Online (Sandbox Code Playgroud)

我的任务是处理这种输入格式

I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.
Run Code Online (Sandbox Code Playgroud)

据我所知,脚本应跳过{ ... }第一个文本部分中的外部闭括号,其中包含开括号和右括号的文本:

{ {red;green;orange} fruit ; cup of {tea;coffee;juice} }
Run Code Online (Sandbox Code Playgroud)

它应该选择一个随机的部分,像这样

{red;green;orange} fruit
Run Code Online (Sandbox Code Playgroud)

要么

cup of {tea;coffee;juice}
Run Code Online (Sandbox Code Playgroud)

然后它更深入:

green fruit
Run Code Online (Sandbox Code Playgroud)

处理完所有文本后,结果可能是以下任何一种

I have red fruit and fresh burger.
I have cup of tea and nice sandwich
I have green fruit and nice burger.
I have cup of coffee and fresh burger.
Run Code Online (Sandbox Code Playgroud)

脚本也应解析并随机化下一个文本.例如

This {beautiful;perfect} {image;photography}, captured with the { {NASA;ESA} Hubble Telescope ; {NASA;ESA} Hubble Space Telescope} }, is the {largest;sharpest} image ever taken of the Andromeda galaxy { {— otherwise known as M31;— known as M31}; [empty here] }.
This is a cropped version of the full image and has 1.5 billion pixels. { You would need more than {600;700;800} HD television screens to display the whole image. ; If you want to display the whole image, you need to download more than {1;2} Tb. traffic and use 800 HD displays }
Run Code Online (Sandbox Code Playgroud)

示例输出可以是

This beautiful image, captured with the NASA Hubble Telescope, is the
sharpest image ever taken of the Andromeda galaxy — otherwise known as
M31.
This is a cropped version of the full image and has 1.5 billion
pixels. You would need more than 700 HD television screens to display
the whole image.
Run Code Online (Sandbox Code Playgroud)

gle*_*man 2

很好的挑战。您需要做的是找到一组没有内部支架的支架,然后从其中随机选择一个项目。你需要在全球范围内这样做。这将仅替换“1 级”大括号。您需要循环该字符串,直到找不到更多匹配项。

\n\n
use v5.18;\nuse strict;\nuse warnings;\n\nsub rand_sentence {\n    my $copy = shift;\n    1 while $copy =~ s{ \\{ ([^{}]+) \\} } \n                      { my @words = split /;/, $1; $words[rand @words] }xsge;\n    return $copy;\n}\n\nmy $str = \'I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.\';\nsay rand_sentence($str);\nsay \'\';\n\n$str = <<\'END\';\nThis {beautiful;perfect} {image;photography}, captured with the { {NASA;ESA}\nHubble Telescope ; {NASA;ESA} Hubble Space Telescope }, is the\n{largest;sharpest} image ever taken of the Andromeda galaxy { {\xe2\x80\x94 otherwise\nknown as M31;\xe2\x80\x94 known as M31}; [empty here] }. This is a cropped version of the\nfull image and has 1.5 billion pixels. { You would need more than {600;700;800}\nHD television screens to display the whole image. ; If you want to display the\nwhole image, you need to download more than {1;2} Tb.  traffic and use 800 HD\ndisplays }\nEND\n\nsay rand_sentence($str);\n
Run Code Online (Sandbox Code Playgroud)\n\n

样本输出

\n\n
I have  orange fruit  and fresh sandwich.\n\nThis beautiful photography, captured with the  ESA Hubble Space Telescope , is the\nlargest image ever taken of the Andromeda galaxy  \xe2\x80\x94 otherwise\nknown as M31. This is a cropped version of the\nfull image and has 1.5 billion pixels.  If you want to display the\nwhole image, you need to download more than 1 Tb.  traffic and use 800 HD\ndisplays\n
Run Code Online (Sandbox Code Playgroud)\n