4

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
Sian Jakey Ellis
  • 435
  • 1
  • 4
  • 13
  • that's certainly a little bit shorter. Just one point, it's not a good idea to instantiate lots of randoms in quick succession since they'll all have the same starting seed. That's why I passed in a random number generator instance, so that it wouldn't do that. – paxdiablo Nov 04 '11 at 08:15
  • I totally forgot about that! Thanks. – Sian Jakey Ellis Nov 04 '11 at 08:23

2 Answers2

9

Don't worry about the nesting, just do it iteratively as follows:

  1. 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.

  2. 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.

  3. There's your modified string.

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.

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.


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();
        }
    }
}

The output is, in one example run

Hello World! C# is an awesome language.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I never thought about it doing it iteratively. Thank you! As for {} and | in text, I think I'll make it so that anything that don't meet the syntax rules can be ignored. – Sian Jakey Ellis Nov 04 '11 at 03:16
  • I'm staring at your block of C# code and wondering what that   gap is doing there :) – BoltClock Nov 04 '11 at 05:43
  • @BoltClock: I'm not a big fan of the scroll bars on code that's only one or two lines too long. Said scrollbars are fine for 500-line behemoths so they don't clog up the page but I'd prefer to see all the code at a glance where possible. If it bothers enough people, I'll stop doing it - I've often wondered whether other people with less vertically-challenged laptops don't have that problem. In fact, stuff it, I'm going to stop doing that, starting now :-) – paxdiablo Nov 04 '11 at 06:01
  • Well, I wasn't bothered by it, but you made a very good point about those scrollbars. – BoltClock Nov 04 '11 at 06:17
  • @paxdiablo check my solution in the question, much simpler :p But I did base it off your psudeo code. Just need to add the logic checking. – Sian Jakey Ellis Nov 04 '11 at 08:06
  • @paxdiablo I think your output is rigged. – DidIReallyWriteThat Dec 26 '14 at 20:52
2

I'd be inclined to create a recursive method to handle the parsing. Write a method that takes a string, scans for first level braces, and makes the random selections from the enclosed options. The method would then call itself with the selected option string before inserting it into the final result.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • your recursion idea was implemented. My solution is in my answer above. Thank you. I don't know who to vote for now, both answers helped a lot. – Sian Jakey Ellis Nov 04 '11 at 08:08