gpt4 book ai didi

perl - 在分隔符之间随机化文本

转载 作者:行者123 更新时间:2023-12-04 15:09:30 25 4
gpt4 key购买 nike

我有这个简单的输入

I have {red;green;orange} fruit and cup of {tea;coffee;juice}

我使用 Perl 来识别两个外部大括号分隔符之间的模式 {} ,并使用内部分隔符 ; 随机化里面的字段.

我得到这个输出
I have green fruit and cup of coffee

这是我的工作 Perl 脚本
perl -plE 's!\{(.*?)\}!@x=split/;/,$1;$x[rand@x]!ge' <<< 'I have {red;green;orange} fruit and cup of {tea;coffee;juice}'

我的任务是处理这种输入格式
I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.

据我了解,脚本应该跳过外部右大括号 { ... }在第一个文本部分中,其中包含带有左括号和右括号的文本:
{ {red;green;orange} fruit ; cup of {tea;coffee;juice} }

它应该选择一个随机部分,像这样
{red;green;orange} fruit

或者
cup of {tea;coffee;juice}

然后更深入:
green fruit

处理完所有文本后,结果可能是以下任何一种
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.

脚本也应该解析和随机化下一个文本。例如
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 }

一个示例输出可能是
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.

最佳答案

不贪婪是一个好主意,但并不能完全奏效。你可以添加一个循环:

perl -plE 'while(s!\{([^{}]*)\}!@x=split/;/,$1;$x[rand@x]!ge){}'

请注意,您的示例输入具有不匹配的大括号,因此这似乎输出了一个虚假的 '}'

关于perl - 在分隔符之间随机化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34453227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com