在分隔符之间随机化文本 [英] Randomizing text between delimiters

查看:37
本文介绍了在分隔符之间随机化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的输入

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

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

I use Perl to identify patterns between two external brace delimiters { and }, and randomize the fields inside with the internal delimiter ;.

我得到这个输出

I have green fruit and cup of coffee

这是我的工作 Perl 脚本

This is my working Perl script

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

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

My task is to process this input format

I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.

<小时>

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


As I understood, the script should skip external closing braces { ... } in the first text part, which has text inside with opening and closing brackets:

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

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

It should choose a random part, like this

{red;green;orange} fruit

cup of {tea;coffee;juice}

然后更深入:

green fruit

<小时>

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


After all text is processed, the result may be any of the following

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.

<小时>

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


The script should parse and randomize the next text too. For example

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.

推荐答案

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

Nice challenge. What you need to do is to find a set of braces without interior braces, and pick a random item from in there. You need to do that globally. That will replace just the "level 1" braces. You need to loop over the string until no more matches are found.

use v5.18;
use strict;
use warnings;

sub rand_sentence {
    my $copy = shift;
    1 while $copy =~ s{ \{ ([^{}]+) \} } 
                      { my @words = split /;/, $1; $words[rand @words] }xsge;
    return $copy;
}

my $str = 'I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.';
say rand_sentence($str);
say '';

$str = <<'END';
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 }
END

say rand_sentence($str);

样本输出

I have  orange fruit  and fresh sandwich.

This beautiful photography, captured with the  ESA Hubble Space Telescope , is the
largest 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.  If you want to display the
whole image, you need to download more than 1 Tb.  traffic and use 800 HD
displays

这篇关于在分隔符之间随机化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆