Spintax {任务|问题|问题}


19

带有Spintax的文章旋转

文章旋转是一种搜索引擎优化技术,通过该技术搜索引擎优化器可以在文章目录,Web 2.0网站或其他反向链接源上发布相关内容的唯一版本。

网站作者偶尔会在自己的网站上使用它,但通常会避免使用,因为旋转内容的质量将低于手写内容。旋转通过重写现有文章或文章的某些部分,并用任意数量的替代版本替换特定的单词,短语,句子甚至整个段落来进行,从而为每次旋转提供稍有不同的变化。

此过程可以完全自动化或手动重写。今天的工作是创建一个程序或函数,该程序或函数解释一个包含Spintax的字符串,该语法将使程序读取可用的不同选项。以下是使用Spintax的示例字符串:

{Hello|Hi there|Bonjour}, user! {What's up?|How are you?|Feeling alright?|I caught a fish!}

如您所见,字符串中要“旋转”的部分用花括号括起来,其中各个选项按|字符分开。这是一些可能产生的例句。

Hi there, user! What's up?
Hello, user! How are you?
Bonjour, user! Feeling alright?
Hello, user! What's up?
Hi there, user! I caught a fish!

你的工作

您的任务(如果您选择接受)是创建一个程序或函数,该程序或函数采用可能包含或可能不包含Spintax的输入字符串,然后打印该字符串的5个版本,并从每个选项块中随机选择一个选项。您可以做一些假设:

  1. 您将不需要处理嵌套的Spintax({This|is {an|one}|example})。
  2. |{并且}字符不会超过Spintax块内的其他地方的其他进行。
  3. 只要代码使用您语言的随机数功能选择选项,就无需检查5个输出语句是否不同。
  4. 您可以假设输入以换行符结尾。

规则

  1. 没有标准漏洞。

测验

1. {A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}.
-------------------------------------------
One cool brown wolf consumed my pastry.
...(3 more here)...
The charming yellow beaver ate my cake.
-------------------------------------------
2. This sentence is valid, and happens to have no Spintax!
-------------------------------------------
This sentence is valid, and happens to have no Spintax!
...(3 more here)...
This sentence is valid, and happens to have no Spintax!
-------------------------------------------
3. {beg|get|ton|sun|bun|can|top|big}{2|3|4|5}{A|B|C|D|E|F|G}{2|3|4|5|6|7}{at|as|be|ex|hi|}{!|#|%}
-------------------------------------------
top2F2hi%
...(3 more here)...
big3E4ex#

这里可以找到第四个测试


这是,因此最少的字节数获胜。


2
现在,有了一点变化...疯狂的库!
Geobits,2015年

我们可以在输入中期望哪些类型的字符?
Sp3000

@ Sp3000 ASCII 32-127中的所有内容。其中唯一的地方{|}会在输入字符串被发现是哪里有Spintax。
卡德2015年

Answers:


8

珀斯,18岁

V5sOMcR\|scR\{cz\}

在这里在线尝试

说明:

V5sOMcR\|scR\{cz\}                     : z = input()
V5                                     : do this 5 times
              cz\}                     : split z on the } character
          cR\{                         : split each element of the resulting list on {
         s                             : join the list of lists back into a list of strings
     cR\|                              : split these strings on the | character
   OM                                  : Choose a random element from each inner list
  s                                    : join those choices, and print them

1
天哪,那太快了!
卡德2015年

7

Ruby,46岁

使用命令行标志-p,运行

($_*=5).gsub!(/{(.+?)}/){$1.split(?|).sample}

用结尾的换行符读入字符串。将其自身连接5次,进行适当的变异,然后输出结果。/{(.+?)}/是带有懒惰量词的正则表达式:如果没有?,它将匹配最左边{到最右边,}而不是最接近的那个。$1是一个魔术变量,它表示每次比赛中的第一个捕获组,而?|字面量是指|字符。sample是一个返回随机元素的数组方法。


6

CJam,25 22 19 18字节

q5*'}/{'{/~'|/mR}/

此代码要求输入包含尾随换行符。

CJam解释器中在线尝试。

怎么运行的

q     e# Read from STDIN.
5*    e# Repeat the input five times.
'}/   e# Split at right curly brackets.
{     e# For each chunk:
  '{/ e#   Split at left curly brackets.
  ~   e#   Dump both chunks on the stack.
  '|/ e#   Split the right chunk at vertical bars.
  mR  e#   Select a chunk, pseudo-randomly.
}/    e#

在每次迭代中,将在堆栈上保留两个块(一个常数,一个变量),并在程序退出时将其打印出来。


6

Python 3,110个字节

import re,random
exec('print("".join(random.choice(s.split("|"))for s in re.split("{(.*?)}",%r)));'%input()*5)

正则表达式分裂,就像其他人一样。

Python 3,121 116 114字节

from random import*
exec(("print(%r[:-1]);"%(input()+'"')).translate({123:"'+choice(['",124:"','",125:"'])+'"})*5)

这将执行适当的替换,然后进行评估。如果不是为了转义反斜杠和撇号,那么结果会更短。

例如,假设我们有Spintax

I {like|hate} {Python|C}.

.translate执行一些转换得到:

print('I '+choice(['like','hate'])+' '+choice(['Python','C'])+'.')

4

Perl,50岁

$_=$_ x5;s/{(.*?)}/@r=split"\\|",$1;$r[rand@r]/ge

49字符+1-p

首先设置$_5个句子即可。然后进行全局搜索并替换,我们在其中搜索每个{ | | }组。它将在上拆分组|,并选择一个随机元素进行替换。

运行:

echo '{A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}.' | perl -pe'$_=$_ x5;s/{(.*?)}/@r=split"\\|",$1;$r[rand@r]/ge'

$_ x=5保存一些字节。
丹尼斯

4

鸵鸟v0.7.0,27个字符

{`{.*?}`{);(;"|/.,R*F=}X}5*

说明:

{...}5*  repeat 5 times
  `{.*?}`  regex to match "spintax" sections
  {...}X   replace with...
    );(;     remove first and last characters (the curly brackets)
    "|/      split on |
    .,R*     multiply length of that array by a random number
    F=       take the floor of that and get the nth element of the array

(注意:0.7.0版本是在发布此问题后制作的,但答案仍然有效,因为构成此版本的所有提交都在发布此问题之前已全部推送。)


4

点,25 22 20字节

晚会晚了,但是这是我的。将输入作为命令行参数,可能需要将其引起来。

L5P_@RR#_Ma^`}|{`^'|

说明:

L5                    Loop 5 times:
          a^`}|{`     Split cmdline arg on curly braces using regex
                 ^'|  Split each item of the result on pipe (if there isn't one,
                        the result is a single-item list)
         M            Map this function to each item a in the result:
   _@RR#_             Calculate a random number between 0 and len(item)-1; use it to
                        index into item
  P                   Print the resulting list, concatenating elements together

17个字节(但不是有效的提交)

L5PRC_Ma^`}|{`^'|

需要最新版本的Pip,此问题发布后已更新。RC(随机选择)运营商已经计划了一段时间,但直到现在我才开始实施它。:^(

了解有关点子的更多信息


1
认真吗 您用自己的语言赢得高尔夫比赛?
克洛伊(Chloe)

3
@Chloe:CJamGS2OstrichpygPythRetinars都是该网站用户发明的高尔夫语言。
丹尼斯

3

的JavaScript ES6,86 84个字节

f=s=>s.repeat(5).replace(/{(.+?)}/g,(_,e)=>(p=e.split`|`)[Math.random()*p.length|0])

这期望输入具有尾随换行符。首先重复输入5次,然后用其中的一个随机单词替换每个Spintax字符串,这是通过拆分|字符,然后在0和所得数组长度减去1之间选择一个随机数而获得的。情况下,|0这只是一种较短的方法Math.floor。感谢vihan1086提醒我有关带标签的模板字符串的信息。

下面的堆栈摘录包含未经测试和易于测试的代码。

f=function(s){
  return s.repeat(5).replace(/{(.+?)}/g,function(_,e){
    return (p=e.split('|'))[Math.random()*p.length|0]
  })
}

run=function(){document.getElementById('output').innerHTML=f(document.getElementById('input').value+'\n')};document.getElementById('run').onclick=run;run()
<input type="text" id="input" value="{A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}." style="width: 400px;" /><button id="run">Run</button><br />
<pre id="output"></pre>


|0等于Math.floor没有Math.round。尽管这是您想要的,但Math.round会导致分布不均。
乔治·赖斯

@GeorgeReith你是对的,我的意思是放地板,但无意间放了圆。谢谢
NinjaBearMonkey 2015年

2

Perl,82个字节

while($o=<>){for(0..4){$_=$o;s/{(.*?)}/@x=split\/\|\/,$1 and$x[rand@x]/ge;print;}}

2

Python 2,139字节

在输入字符串周围添加了两个字节用于引号。如果不需要这些,请告诉我。

在这里尝试

import re,random
s=input()
exec"print''.join(t*(t!=''and(t[0]!='{'))or random.choice(t[1:].split('|'))for t in re.split('({.*?)}',s));"*5

1

Java,243 215 242 234字节

int i,k;void f(String s){String a,b[],c=s;for(;k++<5;System.out.println(c),c=s)while((i=c.indexOf("{"))>=0){a=c.substring(i,c.indexOf("}")+1);b=a.replaceAll("\\{|\\}","").split("\\|");c=c.replace(a,b[(int)(Math.random()*b.length)]);}}

在大括号内查找字符串,{}并用按竖线字符分割创建的字符串数组中的随机元素替换它们。(我意识到为时已晚,必须打印五个句子:P)


1

击:144个 138字符

IFS={} read -ap
w()(for o in "${p[@]}";{
[[ $o =~ \| ]]&&{
IFS=\| read -aa<<<"$o"
o=${a[RANDOM%${#a[@]}]}
}
echo -n "$o"
}
echo)
w
w
w
w
w

样品运行:

bash-4.3$ bash spintax.sh <<< "Look {ma'|daddy|mr. president}! No {bin|core|doc|find|mail}utils tools nor {Awk|Sed|jq|XML Starlet}!"
Look ma'! No docutils tools nor Awk!
Look daddy! No binutils tools nor XML Starlet!
Look mr. president! No docutils tools nor XML Starlet!
Look ma'! No docutils tools nor Awk!
Look mr. president! No binutils tools nor Sed!

1
您可以使用w()(...)代替w(){...}(允许消除空白),printf代替echo -n和使用大括号代替do和来节省一些字节done
丹尼斯2015年

谢谢@丹尼斯。再次。(这不是我第一次忘记这些技巧。)关于printf,除非我误解了某些内容,否则在包含“%”的输入字符串上将失败。
manatwork 2015年

1

Javascript,143142字节

a=prompt(b=5);for(c=[];b--;c.push(a.replace(/{(.+?)}/g,function(_,j){return (d=j.split("|"))[d.length*Math.random()|0]})));alert(c.join("\n"))

1

Python 3,97个字节

在正则表达式替换中使用lambda函数。这有点类似于我在Spintax模块https://github.com/AceLewis/spintax中进行的操作,但是它不处理嵌套的Spintax或转义字符。

import re,random
print(re.sub("{(.*?)}",lambda x:random.choice(x.group(1).split('|')),input()*5))

如果您不假设输入以换行符结尾,则它将为104个字节。

import re,random
print(re.sub("{(.*?)}",lambda x:random.choice(x.group(1).split('|')),(input()+'\n')*5))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.