Spintax C # ... How can I handle this?

Spintax allows you to rotate various words and sentences, for example:

{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language. 

Text between braces will be randomly selected to form different sentences.

I may perhaps come up with a solution on my own, but the problem that I would have is breeding. Sometimes nesting can be very deep. What would be a possible solution for handling nesting?

I can not collect the necessary logic.

+4
source share
2 answers

Do not worry about nesting, just do iteratively as follows:

  • Find the first sequence in the line with {...} without any other curly braces inside. For your case, this is {Hello|Hi} . If this template is no longer available, go to step 3.

  • Take all the possibilities and choose random, replacing the bracket section with its value. Then return to step 1.

  • There is your modified line.

Let's say you have a slightly faulty random number generator that always returns zero. Then your line change history will look like this:

  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.

Please note in particular the transition from (c) to (d). Since we are looking for the first part of a brace that does not have brackets inside, we do {#|++|} to {C{#|++|}|Java} .

All you need to add is the possibility that you may have { , } or | in your actual text - you will need to somehow avoid them in order to protect them from your modification mechanism.


Here is a small C # program that shows this in action. This is probably not so impressive 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 in one example is

  Hello World!  C # is an awesome language.
+8
source

I would be inclined to create a recursive method to handle parsing. Write a method that takes a string, scans the first level parentheses, and makes random selections from private parameters. Then the method will call itself with the selected parameter string before inserting it into the final result.

+2
source

Source: https://habr.com/ru/post/1379582/


All Articles