问题描述
Spintax允许您旋转各种单词和句子,例如:
Spintax allows you to spin various words and sentences such as:
{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.
括号之间的文本将被随机选择以形成不同的句子.
The text between the braces would be selected at random to form different sentences.
我自己可以提出解决方案,但是我要解决的问题是嵌套.有时嵌套可能很深.处理嵌套的可能解决方案是什么?
I am able to possibly come up with a solution myself, but the problem I would have is the nesting. Sometimes the nesting can be very deep. What would be a possible solution to handling the nesting?
我无法收集所需的逻辑.
I can't gather the logic needed.
推荐答案
不用担心嵌套,只需按以下步骤进行迭代即可:
Don't worry about the nesting, just do it iteratively as follows:
-
查找具有
{...}
且没有其他大括号的字符串中的第一个序列.您的情况是{Hello | Hi}
.如果没有该模式,请转到步骤3.
Find the first sequence in the string that has
{...}
with no other braces inside. For your case, that's{Hello|Hi}
. If there are no more of that pattern, go to step 3.
从那里获取所有可能性,然后选择一个随机可能性,用其值替换大括号部分.然后返回到步骤1.
Grab all the possibilities out of there and choose a random one, replacing the brace section with its value. Then go back to step 1.
有您修改过的字符串.
比方说,您的随机数生成器有一点故障,该生成器始终返回零.这样,您的字符串修改历史记录将为:
Let's say you have a slightly faulty random number generator that always returns zero. Your string modification history would then be:
a/ {Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.
b/ Hello {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.
c/ Hello World! {C{#|++|}|Java} is an {awesome|amazing} language.
d/ Hello World! {C#|Java} is an {awesome|amazing} language.
e/ Hello World! C# is an {awesome|amazing} language.
f/ Hello World! C# is an awesome language.
尤其要注意从(c)到(d)的过渡.因为我们要查找的第一个大括号部分中没有 ,所以我们在 {C之前执行
. {#| ++ |}
{#| ++ |} | Java}
Note particularly the transition from (c) to (d). Because we're looking for the first brace section that doesn't have braces inside it, we do the {#|++|}
before the {C{#|++|}|Java}
.
您现在需要添加的只是您的实际文本中可能包含 {
,}
或 |
的可能性-这些将需要可以通过某种方式逃脱,以保护它们免受修改引擎的侵害.
All you need to add now is the possibility that you may have {
, }
or |
within your actual text - these will need to be escaped somehow to protect them from your modification engine.
这是一个小小的C#程序,它在实际中显示了这一点.鉴于我对语言的相对经验不足,它的编写可能并不那么令人印象深刻,但它说明了这一过程.
Here's a little C# program which shows this in action. It's probably not that impressively written, given my relative inexperience with the language, but it illustrates the process.
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static string spintax(Random rnd, string str) {
// Loop over string until all patterns exhausted.
string pattern = "{[^{}]*}";
Match m = Regex.Match(str, pattern);
while (m.Success) {
// Get random choice and replace pattern match.
string seg = str.Substring(m.Index + 1, m.Length - 2);
string[] choices = seg.Split('|');
str = str.Substring(0, m.Index) + choices[rnd.Next(choices.Length)] + str.Substring(m.Index + m.Length);
m = Regex.Match(str, pattern);
}
// Return the modified string.
return str;
}
static void Main(string[] args) {
Random rnd = new Random();
string str = "{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.";
Console.WriteLine(spintax(rnd, str));
Console.ReadLine();
}
}
}
在一个示例中,输出为
Hello World! C# is an awesome language.
这篇关于Spintax C#...我该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!